Type Here to Get Search Results !

Difference of use Stream Api and without Stream Api Code

0

 These streams are related to the collection frameworks. these streams are very different from the io stream, io streams are the sequence of data.

These streams were introduced in the java 1.8 version.

Streams API basically performs bulk operations and processes the objects of collection.

Streams Reduced the code length.

With Stream and without stream API



 public static void main(String[] args) {  
           SpringApplication.run(SpringbootFieldInject1Application.class, args);  
           //Create list and and filter all the even numbers  
           //This List cannot be change(Immutable)  
           List<Integer> list1=List.of(1,2,3,3,4,5,6);  
           //This List can be change(Muttable)  
           List<Integer> list2=new ArrayList<>();  
           list2.add(23);  
           list2.add(45);  
           list2.add(54);  
           list2.add(87);  
           List<Integer> list3=Arrays.asList(22,34,34,35,36);  
           //Without stream find only even number from list one  
           List<Integer> Listeven=new ArrayList<>();  
           for(Integer i:list1) {  
                if(i%2==0) {  
                     Listeven.add(i);  
                }  
           }  
           System.out.println(list1);  
           System.out.println(Listeven);  
           //Using Stream  
           Stream<Integer> stream=list1.stream();  
           List<Integer> newlist=stream.filter(i->i%2==0).collect(Collectors.toList());  
 System.out.println(newlist);  
      }  
Result
[1, 2, 3, 3, 4, 5, 6]
[2, 4, 6]
[2, 4, 6]

Tags

Post a Comment

0 Comments

Recent-post