Java8 - Differet ways to use streams-Collectors

Category : Java | Sub Category : Java 8 Features | By Runner Dev Last updated: 2020-10-22 14:49:45 Viewed : 231


Java8 - Differet ways to use streams

Stream<UserDTO> java.util.Collection.stream():

Returns a sequential Stream with this collection as its source. 

Example: Java8Streams.java 

  

package java8features;


import java.util.ArrayList;

import java.util.List;

import java.util.Map;

import java.util.Set;

import java.util.stream.Collectors; 

public class Java8Streams {

     public static void main(String[] args) {

           List<UserDTO> userDTOList = new ArrayList<>();

           UserDTO userDTO1 = new UserDTO(1, "Ram", "Bang");

           UserDTO userDTO2 = new UserDTO(2, "Sathya", "Hy.d");

           UserDTO userDTO3 = new UserDTO(3, "Bruce", "Malay");

           userDTOList.add(userDTO1)

           userDTOList.add(userDTO2);

           userDTOList.add(userDTO3);


             /** Collecting data into a Set**/

   

           Set<String> userNamesList = userDTOList.stream().filter(p -> p.getUserName() != null).map(x -> x.getUserName())

                      .collect(Collectors.toSet());

           System.out.println("userNamesList " + userNamesList);

               /** Collecting data into a Map**/

           Map<Integer, String> usertDataMap = userDTOList.stream().collect(Collectors.toMap(x -> x.getUserId(), x -> x.getUserName()));

           System.out.println("usertDataMap " + usertDataMap);


            /** Collecting data into a List **/

           List<String> userNameList = userDTOList.stream().map(UserDTO::getUserName).collect(Collectors.toList());

           System.out.println("userNameList " + userNameList);

           String commaSeparated = userNameList.stream().collect(Collectors.joining(",")).toUpperCase();

           System.out.println("commaSeparatedUserList " + commaSeparated);

     }

}

 

Output:

userNamesList [Bruce, Sathya, Ram]

usertDataMap {1=Ram, 2=Sathya, 3=Bruce}

userNameList [Ram, Sathya, Bruce]

commaSeparatedUserList RAM,SATHYA,BRUCE


UserDTO.java

 

 class UserDTO {

 

     private int userId;

     private String userName;

     private String userEmailId;

     private String userAddr;

 

     /**

      * @param userId

      * @param userName

      * @param userAddr

      */

     public UserDTO(int userId, String userName, String userAddr) {

           super();

           this.userId = userId;

           this.userName = userName;

           this.userAddr = userAddr;

     }

 

     public UserDTO() {

 

     }

 

     /**

      * @return the userId

      */

     public int getUserId() {

           return userId;

     }

 

     /**

      * @param userId the userId to set

      */

     public void setUserId(int userId) {

           this.userId = userId;

     }

 

     /**

      * @return the userName

      */

     public String getUserName() {

           return userName;

     }

 

     /**

      * @param userName the userName to set

      */

     public void setUserName(String userName) {

           this.userName = userName;

     }

 

     /**

      * @return the userEmailId

      */

     public String getUserEmailId() {

           return userEmailId;

     }

 

     /**

      * @param userEmailId the userEmailId to set

      */

     public void setUserEmailId(String userEmailId) {

           this.userEmailId = userEmailId;

     }

 

     /**

      * @return the userAddr

      */

     public String getUserAddr() {

           return userAddr;

     }

 

     /**

      * @param userAddr the userAddr to set

      */

     public void setUserAddr(String userAddr) {

           this.userAddr = userAddr;

     }

 

}

 

 

Search
Related Articles

Leave a Comment: