Wednesday, August 15, 2018

[Java] Java example to print all list values


import java.util.ArrayList;
import java.util.List;

public class Example {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("c");
       
        System.out.print("\nMethod 1 :");
        for(int i=0;i< list.size();i++) {
            System.out.print(list.get(i));
        }
       
        System.out.print("\nMethod 2 :");
        for(String s : list){
            System.out.print(s);
        }
       
        System.out.print("\nMethod 3 (SE8) :");
        list.stream().forEach(
            s->System.out.print(s)
        );
       
        System.out.print("\nMethod 4 (SE8) :");
        list.stream().forEach(System.out::print);
    }
}
Result :
Method 1 :abc
Method 2 :abc
Method 3 (SE8) :abc
Method 4 (SE8) :abc

No comments :

Post a Comment