Panadol Chong
Panadol Chong

Reputation: 1903

How to print content in Collection in Java

Good day All,

Normally, I will print all the content in List by look the list.size() and assign it to an object and print the object value. The following is my example code:

List ccUserList = new ArrayList(); // Here I declare a List
Collection ccGroupCol = new ArrayList(); // Here I declare a collection

CCuserBO bo = null;
ccUserList = getSummaryList();
for(int i = 0, i < ccUserList.size() , i++){
    bo = ( CCUserBO ) ccUserList.get(i);
    System.out.println(bo.userName);
}

I would like to ask about the way to print content in Collection. Since Collection no have .get() function.

The following in the code that I try in Collection:

    CCuserBO newBo = null;    
    ccGroupCol = getSummaryList();
    Iterator iterator = ccGroupCol.iterator();
                while ( iterator.hasNext()){
                newBo = iterator.next(); //error here, Type mismatch: cannot convert from Object to //Object[]
                    System.out.println("....");
                }

Upvotes: 2

Views: 21773

Answers (4)

Jan
Jan

Reputation: 1042

As statet in comment, a faster solution for your own answer:

Collection<CCGroupBO> ccGroupCol = new ArrayList<CCGroupBO>()
…
CCGroupBO[] boArray = ccGroupCol.toArray();
for(CCGroupBO newBo : boArray){
    System.out.println(newBo.getGroupName());
}

or even more direct:

Collection<CCGroupBO> ccGroupCol = new ArrayList<CCGroupBO>()
…
for(CCGroupBO newBo : ccGroupCol){
    System.out.println(newBo.getGroupName());
}

depending on other circumstances there is even a nicer method:

class CCGroupBO {
  …
  public String toString() {
    return getGroupName();
  }
}
…
Collection<CCGroupBO> ccGroupCol = new ArrayList<CCGroupBO>()
…
System.out.println(ccGroupCol);

Upvotes: 0

Pradeep Kr Kaushal
Pradeep Kr Kaushal

Reputation: 1546

You can use the for loop to iterate the collection.

Collection collection= new ArrayList();
for (Object obj : collection) {
//Here you can type cast the obj then you can print.

}

Upvotes: 0

Shailesh Saxena
Shailesh Saxena

Reputation: 3512

If you simply want to print all elements of a Collection just sysout Collection directly it will provide you the following form in output: [element1, element2, ....] because toString() method is overrided and implemented to provide such output for all Collection classses.

By using Iterator you can get the element one by one:

    Iterator iterator = ccGroupCol.iterator();
            while ( iterator.hasNext()){
         newBo = (**type cast here to particular newBo object type**)iterator.next(); 
                System.out.println(newBo);//here whatever you implemented in toString() method
    // in newBo type class(if you did so), you will get that type of output, if you do not override
//toString() to provide your implementation,you will get default implementation in
//which it will show <the object class>@<its hash code>
        }

Note: the return type of iterator.next() is Object type, so you must type cast it to avoid incompatible type exception. Or use Generics.

Upvotes: 1

Panadol Chong
Panadol Chong

Reputation: 1903

I found the solution. Here is the example code:

CCGroupBO newBo;
        for(int i = 0 ; i < ccGroupCol.size() ; i++){
            newBo = ( CCGroupBO ) ccGroupCol.toArray()[i];
            System.out.println(newBo.getGroupName());
        }

Thanks for all your help.

Upvotes: 0

Related Questions