There are four ways to collection output in Java, such as the following:
A) iterator output. This method applies to all subclasses of the collection.
Public classhello{ Public Static voidMain (string[] args)throwsException {Set<Person> javaprogramers =NewHashset<person>(); Javaprogramers.add (NewPerson ("Aaron", 25)); Javaprogramers.add (NewPerson ("Larry", 32)); Javaprogramers.add (NewPerson ("Jesse", 33)); Javaprogramers.add (NewPerson ("Mike", 65)); Javaprogramers.add (NewPerson ("Jesse", 33)); Iterator<Person> iter =Javaprogramers.iterator (); while(Iter.hasnext ()) {System.out.println (Iter.next ()); } }}
Two) the foreach output. This method applies to all subclasses of the collection.
Public classhello{ Public Static voidMain (string[] args)throwsException {Set<Person> javaprogramers =NewHashset<person>(); Javaprogramers.add (NewPerson ("Aaron", 25)); Javaprogramers.add (NewPerson ("Larry", 32)); Javaprogramers.add (NewPerson ("Jesse", 33)); Javaprogramers.add (NewPerson ("Mike", 65)); Javaprogramers.add (NewPerson ("Jesse", 33)); for(person item:javaprogramers) {System.out.println (item); } }}
III) Listiterator. Applies only to subclasses of the list interface.
Public classhello{ Public Static voidMain (string[] args)throwsException {List<Person> javaprogramers =NewArraylist<person>(); Javaprogramers.add (NewPerson ("Aaron", 25)); Javaprogramers.add (NewPerson ("Larry", 32)); Javaprogramers.add (NewPerson ("Jesse", 33)); Javaprogramers.add (NewPerson ("Mike", 65)); Javaprogramers.add (NewPerson ("Jesse", 33)); Listiterator<Person> iter =Javaprogramers.listiterator (); System.out.println ("Positive sequence output:"); while(Iter.hasnext ()) {System.out.println (Iter.next ()); } System.out.println ("Reverse-order output:"); while(Iter.hasprevious ()) {System.out.println (iter.previous ()); } }}
Four) enumeration output. Applies only to vector classes.
Public classhello{ Public Static voidMain (string[] args)throwsException {Vector<Person> javaprogramers =NewVector<person>(); Javaprogramers.add (NewPerson ("Aaron", 25)); Javaprogramers.add (NewPerson ("Larry", 32)); Javaprogramers.add (NewPerson ("Jesse", 33)); Javaprogramers.add (NewPerson ("Mike", 65)); Javaprogramers.add (NewPerson ("Jesse", 33)); Enumeration<Person> enumeration =javaprogramers.elements (); while(Enumeration.hasmoreelements ()) {System.out.println (enumeration.nextelement ()); } }}
Four ways to set up output in Java