java8 之 forEach遍历 (遍历)

**  *  * @Author : Wukn  * @Date : 2018/6/7  *  * java8 List forEach  */ public class ListTest {

    public static void main(String[] args) {
        forEach03();
    }


    public static void forEach01() {
        List<User> list = getList();
        list.sort(comparing(User::getName)
        .thenComparing(  User::getId).reversed());
        list.forEach( ll -> System.out.println(ll));
    }


    /**  * map 遍历  */  public static void forEach02() {
        Map<String,Object> map = new HashMap <>(  );
        map.put( "1","a" );
        map.put( "2","b" );
        map.put( "3","c" );
        map.put( "4","d" );
        map.put( "5","e" );

        //传统遍历map方式
        for (Map.Entry mm : map.entrySet()) {
            System.out.println(mm.getKey()+""+mm.getValue());
        }

        // java8  forEach遍历
        map.forEach( (k,v) -> {
                if("a".equals( v )) {
                    System.out.println("sssss");
                }
                });
    }


    /**  * list遍历  */  public static void forEach03() {

        // java8  forEach遍历
        List<User> list = getList();
        list.forEach( ll -> {
            System.out.println(ll);
        } );
    }
    原文作者:托尼吴
    原文地址: https://blog.csdn.net/weixin_41404773/article/details/80608723
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞