title: Reflection , java , iterate on collection
body: <p>I'm writing a function that will iterate on some collection , I don't know what collection I'm getting , so I wrote this till now , the PrintFieldsOfClass will get some object or collection of objects and I will have to print the fields of the class , I have it working for some class , want to add collection support.</p>
  
  <pre><code>void PrintFieldsOfClass(Object obj){
      if(obj ==null){
          return;
      }
      Class&lt;?&gt; mainClass = obj.getClass(); // create instance of the class
      Class&lt;?&gt;[] interfaces =null;
      Object collection = null;
      int isCollection = 0; //1 - for List , 2 -  For Set , 3 - Map
      if(mainClass !=null ){
          interfaces = mainClass.getInterfaces();
      }
      try {
          if(interfaces!=null ){
              for(Class&lt;?&gt; interface1 : interfaces ){
  
  
  
                  if(interface1.getName().toString().equals("java.util.List"))                     {
                      isCollection = 1;
                      collection = new ArrayList&lt;Object&gt;();
                  }
                  else            if(interface1.getName().toString().equals("java.util.Set")){
                      isCollection=2;
                      collection = new HashSet&lt;Object&gt;();
                  }
              }
          }
  </code></pre>
  
  <p>`</p>
  
  <p>After I know what collection I got , how do I cast the object to that collection ? How do I iterate over it ? 
  Thanks in advance !</p>