美文网首页
lamda for collection

lamda for collection

作者: 一个骚骚的码农 | 来源:发表于2021-10-27 14:44 被阅读0次
package cn.sh.ideal.jdk8;
 
import cn.sh.ideal.bean.UserBean;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
 
/**
 * Lambda表达式写法
 *
 * @author gilbert
 */
public class LambdaDemo {
 
    /**
     * 功能描述 无参无返回值
     *
     * @param list
     * @return void
     * @author gilbert
     * @date 2019/6/12
     */
    public static void lambdaWithParamAndNoReturn(List<UserBean> list) {
        list.forEach(userBean -> System.out.println("hello," + userBean.getUsername()));
    }
 
    /**
     *功能描述 重新封装集合数据
     * @author gilbert
     * @date 2019/6/12
     * @param [list]
     * @return void
     */
    public static void initList(List<UserBean> list){
        List<UserBean> userList = list.stream().map(userBean -> new UserBean(userBean.getUserid(), userBean.getWorkid(), userBean.getUsername())).collect(Collectors.toList());
        userList.forEach(userBean -> System.out.println("new list:" + userBean.getUsername()));
    }
 
    /**
     *功能描述 集合过滤
     * @author gilbert
     * @date 2019/6/12
     * @param [list]
     * @return void
     */
    public static void filterList(List<UserBean> list){
        //List<UserBean> filterList = list.stream().filter(userBean -> userBean.getUsername().contains("g")).collect(Collectors.toList());
        List<UserBean> filterList = list.stream().filter(userBean -> Integer.valueOf(userBean.getUserid()) > 6).collect(Collectors.toList());
        filterList.forEach(userBean -> System.out.println("filter list:" + userBean.getUsername()));
    }
 
    /**
     *功能描述 排序
     * @author gilbert
     * @date 2019/6/12
     * @param [list]
     * @return void
     */
    public static void sortList(List<UserBean> list){
        //按userid排序
        List<UserBean> sortList = list.stream().sorted((userBean1,userBean2) -> userBean1.getUsername().compareTo(userBean2.getUsername())).collect(Collectors.toList());
        sortList.forEach(userBean -> System.out.println("sortList:" + userBean.getUserid() + "," + userBean.getUsername()));
    }
 
    /**
     *功能描述 多条件排序
     * @author gilbert
     * @date 2019/6/12
     * @param [list]
     * @return void
     */
    public static void multiSortList(List<UserBean> list){
        list.sort(Comparator.comparing(UserBean::getUserid).thenComparing(UserBean::getUsername));
        list.forEach(userBean -> System.out.println("multiSortList:" + userBean.getUserid() + "," + userBean.getUsername()));
    }
 
    /**
     *功能描述 倒序
     * @author gilbert
     * @date 2019/6/12
     * @param [list]
     * @return void
     */
    public static void reversedSortList(List<UserBean> list){
        //第一种写法
        Comparator<UserBean> comparator = (userBean1,userBean2) -> userBean1.getUsername().compareTo(userBean2.getUsername());
        list.sort(comparator.reversed());
        //第二种写法
        //list.sort(Comparator.comparing(UserBean::getUserid).reversed());
 
        list.forEach(userBean -> System.out.println("reverseSortList:" + userBean.getUserid() + "," + userBean.getUsername()));
    }
 
    /**
     *功能描述 多条件倒序
     * @author gilbert
     * @date 2019/6/12
     * @param [list]
     * @return void
     */
    public static void multiReversedSortList(List<UserBean> list){
        list.sort(Comparator.comparing(UserBean::getUserid).thenComparing(UserBean::getUsername).reversed());
        list.forEach(userBean -> System.out.println("multiReversedSortList:" + userBean.getUserid() + "," + userBean.getUsername()));
    }
 
    /**
     *功能描述 集合分组
     * @author gilbert
     * @date 2019/6/12
     * @param [list]
     * @return void
     */
    public static void groupByList(List<UserBean> list){
        Map<String,List<UserBean>> groupByMap = list.stream().collect(Collectors.groupingBy(UserBean::getWorkid));
        groupByMap.forEach((k,v) -> System.out.println(k+"," + v));
    }
 
    /**
     *功能描述 求和
     * @author gilbert
     * @date 2019/6/12
     * @param [list]
     * @return void
     */
    public static void sumByList(List<UserBean> list){
        System.out.println("sum="+ list.stream().mapToInt(UserBean::getUserid).sum());
    }
 
    /**
     *功能描述 最大值
     * @author gilbert
     * @date 2019/6/12
     * @param [list]
     * @return void
     */
    public static void maxByList(List<UserBean> list){
        OptionalInt optional = list.stream().mapToInt(UserBean::getUserid).max();
        System.out.println("max=" + optional.getAsInt());
    }
 
    /**
     *功能描述 最小值
     * @author gilbert
     * @date 2019/6/12
     * @param [list]
     * @return void
     */
    public static void minByList(List<UserBean> list){
        OptionalInt optional = list.stream().mapToInt(UserBean::getUserid).min();
        System.out.println("min=" + optional.getAsInt());
    }
 
    /**
     *功能描述 平均值
     * @author gilbert
     * @date 2019/6/12
     * @param [list]
     * @return void
     */
    public static void averageByList(List<UserBean> list){
        OptionalDouble optionalDouble = list.stream().mapToInt(UserBean::getUserid).average();
        System.out.println("average=" + optionalDouble.getAsDouble());
    }
 
    /**
     *功能描述 List转map
     * @author gilbert
     * @date 2019/6/12
     * @param [list]
     * @return void
     */
    public static void listToMap(List<UserBean> list){
        //用 (k1,k2)->k1 来设置,如果有重复的key,则保留key1,舍弃key2
        Map<Integer,UserBean> map = list.stream().collect(Collectors.toMap(UserBean::getUserid,userBean -> userBean, (k1, k2) -> k1));
        map.forEach((k,v) -> System.out.println("k=" + k + ",v=" + v));
    }
 
    /**
     *功能描述 map转list
     * @author gilbert
     * @date 2019/6/12
     * @param [map]
     * @return void
     */
    public static void mapToList(Map<Integer,String> map){
        List<UserBean> list = map.entrySet().stream().sorted(Comparator.comparing(key -> key.getKey())).map(key -> new UserBean(Integer.valueOf(key.getKey()),key.getValue(),key.getValue())).collect(Collectors.toList());
        list.forEach(userBean -> System.out.println(userBean.getUserid() + "," + userBean.getUsername()));
    }
 
    /**
     *功能描述 字符串转list
     * @author gilbert
     * @date 2019/6/12
     * @param [str]
     * @return void
     */
    public static void stringToList(String str){
        //不需要处理
        //<String> list = Arrays.asList(str.split(","));
        //需要处理
        List<String> list = Arrays.asList(str.split(",")).stream().map(string -> String.valueOf(string)).collect(Collectors.toList());
        list.forEach(string -> System.out.println(string));
    }
 
    /**
     *功能描述 姓名以逗号拼接
     * @author gilbert
     * @date 2019/6/12
     * @param [list]
     * @return void
     */
    public static void joinStringValueByList(List<UserBean> list){
        System.out.println(list.stream().map(UserBean::getUsername).collect(Collectors.joining(",")));
    }
 
    /**
     *功能描述 分组统计
     * @author gilbert
     * @date 2019/6/12
     * @param [list]
     * @return void
     */
    public static void countByList(List<UserBean> list){
        Map<String, Long> map = list.stream().collect(Collectors.groupingBy(UserBean::getWorkid,Collectors.counting()));
        map.forEach((k,v) -> System.out.println("key=" + k + ",value=" + v));
    }
 
    public static void main(String[] args) {
        //List<UserBean> list = Arrays.asList(new UserBean(1, "AKB001", "gilbert"), new UserBean("2", "AKB002", "apple"), new UserBean("3", "AKB003", "cat"));
        List<UserBean> list = Stream.of(new UserBean(1, "AKB001", "gilbert"),
                                        new UserBean(2, "AKB002", "apple"),
                                        new UserBean(4, "AKB004", "dog"),
                                        new UserBean(5, "AKB005", "egg"),
                                        new UserBean(6, "AKB006", "frog"),
                                        new UserBean(6, "AKB006", "banana"),
                                        new UserBean(7, "AKB007", "google"),
                                        new UserBean(3, "AKB003", "cat"))
                .collect(Collectors.toList());
        lambdaWithParamAndNoReturn(list);
        initList(list);
        filterList(list);
        sortList(list);
        reversedSortList(list);
        multiSortList(list);
        multiReversedSortList(list);
        groupByList(list);
        sumByList(list);
        maxByList(list);
        minByList(list);
        averageByList(list);
        listToMap(list);
        String str = "apple,banana,cat,dog";
        stringToList(str);
        Map<Integer,String> map =new HashMap<Integer, String>() {
            {
                put(1, "apple");
                put(2, "banana");
                put(3, "cat");
                put(4, "dog");
                put(5, "frog");
            }
        };
        mapToList(map);
        joinStringValueByList(list);
        countByList(list);
    }
}



相关文章

  • lamda for collection

  • Java8 One---- Lamda表达式

    1 Lamda表达式 lamda表达式构成, 参数,箭头,方法体.为什么使用lamda表达式, 最只直观的就是简洁...

  • 函数指针

    lamda传入函数指针 如果带捕获参数则不能传入。 lamda [&] 和 [=] reuslt: 113 所以一...

  • Java8(二)之走进Stream

    本篇文章是基于上一篇Lamda表达式的基础上的内容,因为其中涉及了较多的Lamda语法。如果对于Lamda表达式没...

  • JDK1.8之Lamda表达式(匿名内部类优化)

    1.Lamda表达式的作用 例如一个匿名内部类的使用,使用Lamda表达式可以简化。 图一Lamda表达式和图二的...

  • matlab解特征值偏微分方程

    -Lu=lamda*u,这个解有无数个,u=a*sin(pi*m*x)*sin(pi*ny),lamda=pi^2...

  • 关于lamda、inline和reified

    一、关于 lamda 1.1、kotlin中的一个lamda声明,对应Java的一个FunctionN的接口 顶级...

  • cpp insight2

    lamda值传递 lamda引用传递 throw传递值时的搬运 默认构造/默认拷贝构造/默认移动构造 一旦自定义了...

  • java lamda

    一、lambda初探 lambda可以是java_8出来新特性,可以使代码更加紧凑以及简洁,增加可读性,下面简单的...

  • lamda in Kotlin

    Kotlin中,lamda只有一个参数时,可以用默认参数名称it代替命名参数。所以下面两种方式的代码,含义是一样的...

网友评论

      本文标题:lamda for collection

      本文链接:https://www.haomeiwen.com/subject/wzftaltx.html