Using List.stream () method to do processing List

List.steam () in the power of this is not repeated here.

Directly to the theme:

1. Filter Bean List Some fields are equal to the specified value for all rows

        List<Member> list = itemsArray.toJavaList(Member.class);

        List<String> roleList = new ArrayList<>();
        roleList.add(roleName);

        List<Member> filterResult = list.stream().filter((Member m)->roleList.contains(m.getProjectRole())).collect(Collectors.toList());

 

2. Bean List in judgment, whether there is an attribute value equal to the specified value

            // embodiment 1. lamda expression directly determines whether to include filterResult personEmail, this method is recommended 
            IF (filterResult.stream (). Filter (w to> String.valueOf (w.getPersonEmail ()). The equals (personEmail)). findAny (). isPresent ()) {
                log.info("filterResult包含"+personEmail);
            }
            
            // embodiment 2, the filter on the basis of the results, the value on line personEmail a regenerated List 
            List <String> = personEmailList new new the ArrayList <> ();
            personEmailList.add(personEmail);
            List<Member> emailResult = filterResult.stream().filter((Member mem)->personEmailList.contains(mem.getPersonEmail())).collect(Collectors.toList());
            log.info (emailResult.size () + "after filtration contains" + personEmail);

 

3. Bean List, all the value of the specified attribute into the column Set (Set properties, depeaked)

Set<Long> result = list.stream().map(siteAssign->siteAssign.getVENDOR_ID()).collect(Collectors.toSet());

 

4. Bean List, all the value of the specified attribute into the column List (not to weight)

List<Long> result = list.stream().map(siteAssign->siteAssign.getVENDOR_ID()).collect(Collectors.toList());

 

5. The attributes specified in the Distinct Bean List, line thus obtained is a complete row, taking any row in the table in the same row VENDOR_ID.

// https://blog.csdn.net/weixin_30451709/article/details/96156020 
        // by treeset to re-acquire all non-employee vendor ID suppliers, and is not repeated 
        List <SuppSiteAssign> result1 = siteAssignList.stream ( ) .filter (w -!> "the EMPLOYEE" .equals (w.getVENDOR_TYPE_LOOKUP_CODE ()).) the collect (
                        Collectors.collectingAndThen(
                        Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(SuppSiteAssign::getVENDOR_ID))), ArrayList::new));

        // Ibid completed by other methods 
        List <SuppSiteAssign> result2 =   siteAssignList.parallelStream () filter (StreamUtil.distinctByKey (SuppSiteAssign :: getVENDOR_ID)) the collect (Collectors.toList ())..;


StreamUtil
public class StreamUtil {

    /**
     * https://stackoverflow.com/questions/23699371/java-8-distinct-by-property
     * https://www.cnblogs.com/woshimrf/p/java-list-distinct.html
     * @return
     */
    public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
        Set<Object> seen = ConcurrentHashMap.newKeySet();
        return t -> seen.add(keyExtractor.apply(t));
    }
}

About parallelStream (), I have not understood it. Specific to be verified.

 

6. Since there are distinct methods, there are bound to group by methods

https://blog.csdn.net/u013218587/article/details/74600585

        // by user, role groups seeking the number 
        the Map <String, the Map <String, Long >> tradeNumMap = dataAccessBeans.stream (). The collect (Collectors.groupingBy (DataAccessBean :: getUserName,
                                                                                                         Collectors.groupingBy(DataAccessBean::getROLE_NAME_DISPLAY,
                                                                                                                         Collectors.counting())));


        for(String userName : tradeNumMap.keySet()){
            Map<String, Long> map = tradeNumMap.get(userName);
            for(String roleName : map.keySet()){
                    Long count = map.get(roleName);
                    if(count>10){
                        log.info(userName+"-"+roleName+"-"+count);
                }
            }
        }

 

 

 

// embodiment 1. directly lamda expression determines filterResult whether to include personEmail, Recommended method of 
IF (filterResult.stream (). Filter (w to> String. ValueOf (w.getPersonEmail ()). The equals ( personEmail )). . findAny () isPresent ()) {
log .info ( "filterResult comprising " + personEmail);
}

// embodiment 2 , the filter on the basis of the results, the value of personEmail row regenerating a List
List <String> = personEmailList new new the ArrayList <> ();
personEmailList.add (personEmail);
List <Member> emailResult = filterResult.stream () filter ((Member MEM) ->.personEmailList .Contains (mem.getPersonEmail ())) the collect (Collectors.. toList ());
log .info (emailResult.size () + " after filtration contains " + personEmail);

Guess you like

Origin www.cnblogs.com/huanghongbo/p/12020129.html