2019-08-09 andIn 和 Collectors的一些用法

AndIn

andin返回的是一个list 传进去一个list 然后根据这个list放到example中转成一个查询语句,返回一个和之前list相对应的目标list

比如item表中的id和agent表中的id相互对应 如果用for循环 就得交互很多次 和数据库 用andIn只需要交互一次就可以了

List<Integer> itemIds  = itemDTOS.stream().map(item.->item.getId()).collect(Collector.toList);

AgentExample agentExample = new AgentExample();

agentExample.createCriteria().andIdIn(itemIds);

List<Agent> agents = agentMapper.selectByExample(agentExample);

这样我就拿到了item表和agent表中id相对应的agentList;

groupingBy 

  id          student           class           name            age

1            a                    1                    a                12

2            b                    2                    b                22

3            c                    3                    c                 33

groupingBy就是根据那个去分组

map<id,List<Student>> idAndStudentMap = 

    StudentList.Stream().collect(Collectors.groupingBy(Student::getId());

这样拿到了一个以 student id为key  studentList为value的 一一对应的map

Collectors.groupingBy( Function.identity(), Collectors.counting()  )

Function.identity()按种类进行分组  counting计数

Collectors.summingInt(Item::getQty)记录总价格

Map<BigDecimal, Set<String>> result =            items.stream().collect(Collectors.groupingBy(Item::getPrice,                     Collectors.mapping(Item::getName, Collectors.toSet())));

按价格进行分组 余下内容 变成一个映射关系 item是key 去重复输出

{

19.99=[

Item{name='banana', qty=20, price=19.99},

Item{name='banana', qty=10, price=19.99}

],

29.99=[

Item{name='orang', qty=10, price=29.99},

Item{name='watermelon', qty=10, price=29.99}

],

9.99=[

Item{name='apple', qty=10, price=9.99},

Item{name='papaya', qty=20, price=9.99},

Item{name='apple', qty=10, price=9.99},

Item{name='apple', qty=20, price=9.99}

]

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容