Lambda表达式遍历Map,List;并且进行自然排序。

1.测试类

package io.renren.ceshi;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * @author youzo
 */
public class LambdaDemo {
    public static void main(String[] args) {
        Map<String,Object> map = Maps.newHashMap();
        map.put("a1","a");
        map.put("b1","b");
        map.put("c1","c");
        map.put("d1","d");
        map.put("e1","e");
        map.put("f1","f");
        map.forEach((k,v)-> System.out.println("k="+k+":"+"v="+v));
        System.out.println("===========================");
        List<String> list = Lists.newArrayList();
        list.add("aaa");
        list.add("ccc");
        list.add("bbb");
        //用 Lambda 排序
        list.sort(Comparator.naturalOrder());
        list.forEach((v)-> System.out.println("v"+v));
    }
}

2.输出结果

k=a1:v=a
k=f1:v=f
k=e1:v=e
k=d1:v=d
k=c1:v=c
k=b1:v=b
===========================
v=aaa
v=bbb
v=ccc
    原文作者:尤宗旭
    原文地址: https://blog.csdn.net/weixin_41793807/article/details/84745867
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞