美文网首页
jdk8 lambda表达式与stream流

jdk8 lambda表达式与stream流

作者: kk爱自由 | 来源:发表于2019-03-03 21:57 被阅读0次

lambda表达式

  • 首先创建两个java Bean
public class Man {
    private String name;
    private String id;
    private List<Card> cards;

    public Man() {
    }

    public Man(String id, String name, List<Card> cards) {
        this.name = name;
        this.id = id;
        this.cards = cards;
    }

    @Override
    public String toString() {
        return "Man{" +
                "name='" + name + '\'' +
                ", id='" + id + '\'' +
                ", cards=" + cards +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public List<Card> getCards() {
        return cards;
    }

    public void setCards(List<Card> cards) {
        this.cards = cards;
    }
}
public class Card {
    private String name;
    private String cardId;

    public Card() {
    }

    public Card(String name, String cardId) {
        this.name = name;
        this.cardId = cardId;
    }

    @Override
    public String toString() {
        return "Card{" +
                "name='" + name + '\'' +
                ", cardId='" + cardId + '\'' +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCardId() {
        return cardId;
    }

    public void setCardId(String cardId) {
        this.cardId = cardId;
    }
}
  • 编写测试类
public class LambdaDemo {
    public static void main(String[] args) {
        List<Man> mans = new ArrayList<>();
        mans.add(new Man("002", "李四", Arrays.asList(new Card("招商银行", "6225800002"), new Card("建设银行", "6227035248"))));
        mans.add(new Man("003", "王五", Arrays.asList(new Card("建设银行", "6227056547"), new Card("中国银行", "6013832547"), new Card("民生银行", "4074058542"))));
        mans.add(new Man("004", "赵六", Arrays.asList(new Card("工商银行", "9558832458"), new Card("工商银行", "9558832547"), new Card("建设银行", "6227032578"))));
        mans.add(new Man("005", "孙七", Arrays.asList(new Card("中国银行", "6013825847"), new Card("农业银行", "6228836547"), new Card("招商银行", "6225014582"))));
        mans.add(new Man("006", "张三", Arrays.asList(new Card("工商银行", "9558832587"), new Card("交通银行", "6222814578"), new Card("工商银行", "9558865427"))));
        mans.add(new Man("007", "张三", Arrays.asList(new Card("招商银行", "1158832587"), new Card("交通银行", "6344814578"), new Card("工商银行", "2131133133"))));
        /**需求1 : 打印所有客户的名字*/
        // for (Man man : mans) {
        //     System.out.println(man.getName());
        // }
        //lambda表达式
        /*mans.forEach(man ->
                System.out.println(man.getName()));*/
        /**需求2 : 获得所有客户名字的 list 集合*/
        /*List<String> names = new ArrayList<>();
        for (Man man : mans) {
            names.add(man.getName());
        }
        System.out.println(names.size());
        for (String name : names) {

            System.out.println(name);
        }*/

        //lambda 做法
        /*List<String> names = mans.stream().map(man -> man.getName()).collect(Collectors.toList());
        System.out.println(names.size());
        names.forEach(name -> System.out.println(name));*/

        /**需求3 : 获取名字是张三的客户:*/
        /*List<Man> zhangsanLists = mans.stream().filter(man -> man.getName().equals("张三")).collect(Collectors.toList());

        zhangsanLists.forEach(man -> System.out.println(man.toString()));*/
        /**需求4: 返回客户id 是007的 对象:*/
        /*Man man =  mans.stream().filter(man1 -> "007".equals(man1.getId())).findFirst().orElse(null);
        System.out.println(man.toString());*/
        /*for (Man man : mans) {
            if(man.getId().equals("007")){
                System.out.println(man.toString());

            }
        }*/
        /**需求5: 获得名字为 “张三” 客户的 银行卡 cards 集合*/
        /*List<Card> cards = mans.stream().filter(man -> "张三".equals(man.getName())).flatMap(s -> s.getCards()
                .stream()).collect(Collectors.toList());
        cards.forEach(card -> System.out.println(card.toString()));*/
        /**需求6 :循环遍历一个双列集合:*/
        HashMap<Object, Object> map = new HashMap<>();
        map.put("1223","223");
        map.put("1323","223");
        map.forEach((o, o2) -> System.out.println(o+":"+o2));

    }
}
  • 自己总结:
    lambda表达式的使用就是将对象(集合、数组...)转换为stream()对象后再根据自己想要的结果过滤或者构造相应的结构。

相关文章

网友评论

      本文标题:jdk8 lambda表达式与stream流

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