i am having the list myEmpls
List myEmpls = new ArrayList();
In this list i have added used defined objects.
LogConf e = getLogs(el);
//add it to list
myEmpls.add(e);
Now how to iterate the list of objects and get the values from this objects. How to do this?
An Iterator is an object that can be used to loop through collections, like ArrayList and HashSet. It is called an "iterator" because "iterating" is the technical term for looping. To use an Iterator, you must import it from the java.util package.
By using this iterator object, you can access each element in the collection, one element at a time. Obtain an iterator to the start of the collection by calling the collection's iterator() method. Set up a loop that makes a call to hasNext(). Have the loop iterate as long as hasNext() returns true.
Since Java 8, we can use the forEach() method to iterate over the elements of a list. This method is defined in the Iterable interface, and can accept Lambda expressions as a parameter.
You could just google this and you would find tons of solutions.. But here is the code:
for(LogConf element : myEmpls) {
System.out.println(element.getValue());
}
You should also get used to define the type of the elements in the list:
List<LogConf> myEmpls = new ArrayList<LogConf>();
I know its been some time since this post was made. You can also use the Iterator.
List myEmpls = new ArrayList();
Iterator itr = myEmpls.iterator();
while(itr.hasNext()) {
LogConf Logobj = (LogConf) itr.next();
System.out.println(Logobj.getterName());
}
Hope this helps someone.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With