Thursday 31 August 2017

How to convert lower alpha list to upper alpha list using Java 8 streams | Streams in Java 8


Click here to watch in Youtube : 
https://www.youtube.com/watch?v=CFF9FfrAMpk&list=UUhwKlOVR041tngjerWxVccw

NonStreamDemo.java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class NonStreamDemo
{
    public static void main(String[] args)
    {

        List<String> lowerAlphaList = Arrays.asList("a", "b", "c ");
        System.out.println("lowerAlphaList = "+lowerAlphaList); // [a, b, c, d]

        // Before Java8
        List<String> upperAlphaList = new ArrayList<>();
        for (String str : lowerAlphaList)
        {
            upperAlphaList.add(str.toUpperCase());
        }

        System.out.println("upperAlphaList = "+upperAlphaList); // [A, B, C, D]

    }

}
Output
lowerAlphaList = [a, b, c ]
upperAlphaList = [A, B, C ]
StreamDemo1.java
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class StreamDemo1
{
    public static void main(String[] args)
    {

        List<String> lowerAlphaList = Arrays.asList("a", "b", "c ");
        System.out.println("lowerAlphaList = "+lowerAlphaList); // [a, b, c, d]
        // Java 8
        List<String> upperAlphaList = lowerAlphaList.stream().map(String::toUpperCase)
                                                             .collect(Collectors.toList());
        System.out.println("upperAlphaList = "+upperAlphaList); // [A, B, C, D]     
        
    }

}
Output
lowerAlphaList = [a, b, c ]
upperAlphaList = [A, B, C ]
StreamDemo2.java
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class StreamDemo2
{
    public static void main(String[] args)
    {

        // streams apply to any data type.
        List<Integer> numberList = Arrays.asList(1, 2, 3, 4, 5);
        
        List<Integer> resultList = numberList.stream().map(n -> n * 2)
                                               .collect(Collectors.toList());
        
        System.out.println(resultList); // [2, 4, 6, 8, 10]
    }

}
Output
[2, 4, 6, 8, 10]

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/StreamDemo_LToU_list_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/StreamDemo_LToU_list_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/53e39342c1c41555c6e548783b3cf96ab7bd9ee6/BasicJava/StreamDemo_LToU_list_App/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment