Java8中Stream的一些用法

导读:本篇文章讲解 Java8中Stream的一些用法,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

数据结构和数据准备

@Data
@AllArgsConstructor
@NoArgsConstructor
static class StreamItem {
    Integer id;
    String name;
    Boolean sex;
}

List<StreamItem> list = Lists.newArrayList(new StreamItem(1, "a", true), new StreamItem(2, "b", false), 
new StreamItem(3, "c", true), new StreamItem(1, "d", false), new StreamItem(4, "e", true));

map

List<String> list2 = list.stream().map(StreamItem::getName).collect(Collectors.toList());
System.out.println(list2);

[a, b, c, d, e]

groupingBy

Map<Integer, List<StreamItem>> list2 = list.stream().collect(Collectors.groupingBy(StreamItem::getId));
System.out.println(list2);

{1=[StreamStudy.StreamItem(id=1, name=a, sex=true), StreamStudy.StreamItem(id=1, name=d, sex=false)], 2=[StreamStudy.StreamItem(id=2, name=b, sex=false)], 
3=[StreamStudy.StreamItem(id=3, name=c, sex=true)], 4=[StreamStudy.StreamItem(id=4, name=e, sex=true)]}


@Test
public void testValueIsSetInMap() {
    Map<Integer, Set<Boolean>> map = list.stream()
            .collect(Collectors.groupingBy(StreamItem::getId,
                    Collectors.mapping(StreamItem::getSex, Collectors.toSet())));
    System.out.println(map);
}
{1=[false, true], 2=[false], 3=[true], 4=[true]}

toMap

Map<Integer, String> list2 = list.stream().collect(Collectors.toMap(StreamItem::getId, StreamItem::getName, (k1, k2) -> k2));
System.out.println(list2);

{1=d, 2=b, 3=c, 4=e}

filter

List<StreamItem> list2 = list.stream().filter(StreamItem::getSex).collect(Collectors.toList());
System.out.println(list2);

[StreamStudy.StreamItem(id=1, name=a, sex=true), StreamStudy.StreamItem(id=3, name=c, sex=true), StreamStudy.StreamItem(id=4, name=e, sex=true)]

sum

int sum = list.stream().mapToInt(StreamItem::getId).sum();
System.out.println(sum);

sum = list.stream().map(StreamItem::getId).reduce(Integer::sum).get();
System.out.println(sum);

sum = list.stream().map(StreamItem::getId).reduce(0, Integer::sum);
System.out.println(sum);

BigDecimal bigDecimal = list.stream()
        .map(streamItem -> new BigDecimal(streamItem.getId()))
        .reduce(BigDecimal.ZERO, BigDecimal::add);
System.out.println(bigDecimal);

11
11
11
11

多维度分类

多个属性分类->先groupingBy再toMap
    @Test
    public void testPortfolio() {
        Map<Integer, Map<String, StreamItem>> map = list.stream()
                .collect(Collectors.groupingBy(StreamItem::getId, Collectors.toMap(StreamItem::getName, item -> item)));
        System.out.println(map);
    }

{1={a=StreamStudy.StreamItem(id=1, name=a, sex=true), d=StreamStudy.StreamItem(id=1, name=d, sex=false)}, 2={b=StreamStudy.StreamItem(id=2, name=b, sex=false)}, 3={c=StreamStudy.StreamItem(id=3, name=c, sex=true)}, 4={e=StreamStudy.StreamItem(id=4, name=e, sex=true)}}

toMap LinkedHashMap

    @Test
    public void toLinkedHashMap() {
        Map<Integer, StreamItem> map = list.stream()
                .collect(Collectors.toMap(StreamItem::getId, s -> s, (v1, v2) -> v1, LinkedHashMap::new));
        System.out.println(map);
    }

{1=StreamStudy.StreamItem(id=1, name=a, sex=true), 2=StreamStudy.StreamItem(id=2, name=b, sex=false), 3=StreamStudy.StreamItem(id=3, name=c, sex=true), 4=StreamStudy.StreamItem(id=4, name=e, sex=true)}

sort

list.sort(Comparator.comparing(StreamItem::getName).reversed());
System.out.println(list);

根据name进行倒序排序
[StreamStudy.StreamItem(id=4, name=e, sex=true), StreamStudy.StreamItem(id=1, name=d, sex=false), StreamStudy.StreamItem(id=3, name=c, sex=true), StreamStudy.StreamItem(id=2, name=b, sex=false), StreamStudy.StreamItem(id=1, name=a, sex=true)]

foreach

list.forEach(streamItem -> streamItem.setId(streamItem.getId() + 1));
list.forEach(System.out::println);

StreamStudy.StreamItem(id=2, name=a, sex=true)
StreamStudy.StreamItem(id=3, name=b, sex=false)
StreamStudy.StreamItem(id=4, name=c, sex=true)
StreamStudy.StreamItem(id=2, name=d, sex=false)
StreamStudy.StreamItem(id=5, name=e, sex=true)

参考

使用Stream快速对List进行一些操作
Java8之Consumer、Supplier、Predicate和Function攻略

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/5964.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!