简体   繁体   中英

Java 8 - Lambda Expression Convert List<String> to List<DTO>

I have a List like these. Want to perform some filteration and conversion of List of String to List of DTO :

    public static void main(String[] args) {

        String[] te = {"a","B_1","c","d","e"};      
        String[] con = {"a","b","ca"};

        List<String> S  = Arrays.asList(te).parallelStream()
                .map(x -> x.replace(ISConstants.domainName, ""))
                .filter(x -> (!x.contains("_"))? ArrayUtils.contains(con, x.toLowerCase()): ArrayUtils.contains(con, x.split("_")[0].toLowerCase()))
                .filter(x -> !x.equalsIgnoreCase("b"))
                .collect(Collectors.toList());

//          List<UserDTO> ls = l.forEach;
//          System.out.println(l);

    }

Here You can see, I got a list S now I want to convert that list S to List of UserDTO where I have a method setUserName and want to set string from S to userName into ls.

Anyone know how can i perform this with Existing lambda expression or with new expression.

By using another map operation such as:

List<UserDTO> users  = Arrays.asList(te).parallelStream()
        .map(x -> x.replace(ISConstants.domainName, ""))
        .filter(x -> (!x.contains("_"))? ArrayUtils.contains(con, x.toLowerCase()): ArrayUtils.contains(con, x.split("_")[0].toLowerCase()))
        .filter(x -> !x.equalsIgnoreCase("b"))
        .map(s -> new UserDTO(s)) // or 'UserDTO::new' by using a constructor with 'name' being set
        .collect(Collectors.toList());

(based on Holger's comment ) and further improving it with a use of regex such as:

List<UserDTO> users = Arrays.asList(te).parallelStream()
        .map(x -> x.replace(ISConstants.domainName, ""))
        .filter(x -> !x.equalsIgnoreCase("b")) // filter in using simpler condition first
        .filter(x -> ArrayUtils.contains(con, x.replaceFirst("_.*", "").toLowerCase())) // simplified using regex
        .map(s -> new UserDTO(s)) // or 'UserDTO::new' by using a constructor with 'name' being set
        .collect(Collectors.toList());

Assuming DTO has a constructor, that takes one String , you can map it:

List<DTO> S  = Arrays.asList(te).parallelStream()
            .map(x -> x.replace(ISConstants.domainName, ""))
            .filter(x -> (!x.contains("_"))? ArrayUtils.contains(con, x.toLowerCase()): ArrayUtils.contains(con, x.split("_")[0].toLowerCase()))
            .filter(x -> !x.equalsIgnoreCase("b"))
            .map(x -> new DTO(x))
            .collect(Collectors.toList());

or easier with method reference:

List<DTO> S  = Arrays.asList(te).parallelStream()
            .map(x -> x.replace(ISConstants.domainName, ""))
            .filter(x -> (!x.contains("_"))? ArrayUtils.contains(con, x.toLowerCase()): ArrayUtils.contains(con, x.split("_")[0].toLowerCase()))
            .filter(x -> !x.equalsIgnoreCase("b"))
            .map(DTO::new)
            .collect(Collectors.toList());

As other answers show, you could use a constructor that receives a String and sets the user name to the DTO.

Or, if you can't add such constructor, here's what you can do:

List<UserDTO> result = Arrays.asList(te).stream()
        .map(x -> x.replace(ISConstants.domainName, ""))
        .filter(x -> !x.contains("_") ? 
                     ArrayUtils.contains(con, x.toLowerCase()) : 
                     ArrayUtils.contains(con, x.split("_")[0].toLowerCase()))
        .filter(x -> !x.equalsIgnoreCase("b"))
        .map(x -> {
                     UserDTO u = new UserDTO();
                     u.setUserName(x);
                     return u;
         })
        .collect(Collectors.toList());

Here I'm using Stream.map to convert the string into a UserDTO with that string set as the user name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM