Stream流操作

  • Post author:
  • Post category:其他


今天了解了Stream流操作,觉得很有意思,如果在写代码的时候应用到,还是能有很大的帮助的。

举个例子,

1、有一个List<String>,我想要找出长度大于10的元素有几个。

List<String> result= Arrays.asList("123","123","12YKHVGVBsds","KHIUYVJVsdJOINKLsdsd","sdsAUBDNds","sdvffbsdd343gbt433","89898xcxb");

//传统的方法就是采用for循环找出长度大于10的数
int count=0;
for (String s : result) {
  if(s.length()>10){
     count++;
   }
}


stream流操作的

过滤函数

,就非常简洁明了啦。

long count1= result.stream().filter(s ->
    s.length()>10
).count();

2、现在还是这个List<String>,我想要将所有小写字母变成大写

这样就可以用到Stream中的.map方法啦

List<String> collect = result.stream().map(s -> s.toUpperCase()).collect(Collectors.toList());
System.out.println(collect);

代码很简洁,结果很nice:

[123, 123, 12YKHVGVBSDS, KHIUYVJVSDJOINKLSDSD, SDSAUBDNDS, SDVFFBSDD343GBT433, 89898XCXB]

3、现在想要将List<String>中的重复元素去掉,用Stream也非常简单

List<String> collect1 = result.stream().distinct().collect(Collectors.toList());
System.out.println(collect1);

结果很nice

[123, 12YKHVGVBsds, KHIUYVJVsdJOINKLsdsd, sdsAUBDNds, sdvffbsdd343gbt433, 89898xcxb]

4、我想要统计List<String>里面所有的数字有几个

应用stream流的思路:现将List里面的所有的元素和成一个流,对这个大流进行过滤

long count2 = result.stream().flatMap(s -> {
    char[] chars = s.toCharArray();
    List<Character> t = new ArrayList<>();
    for (char c : chars) {
        t.add(c);
    }
    return t.stream();
 }).filter(c -> {
    return c >= '0' && c <= '9';
  }).count();

结果:

19

5、将list转为map

结点类:

 static class Node{
        int code;
        String name;

        public Node(int code, String name) {
            this.code = code;
            this.name = name;
        }

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        public String getName() {
            return name;
        }

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

两种实现方式:

List<Node> list=new ArrayList<>();
list.add(new Node(1, "王杰"));
list.add(new Node(2, "hahahah"));
list.add(new Node(3, "hello word"));
list.add(new Node(4, "迪丽热巴"));

Map<Integer,String> map=new HashMap<>();
for(Node n:list){
   map.put(n.getCode(),n.getName());

}
System.out.println(map);

Map<Integer, String> collect =             list.stream().collect(Collectors.toMap(Node::getCode, Node::getName));
System.out.println(collect);

结果:

{1=王杰, 2=hahahah, 3=hello word, 4=迪丽热巴}
{1=王杰, 2=hahahah, 3=hello word, 4=迪丽热巴}

6、tomap收集时的小技巧之

覆盖

        List<Node> list=new ArrayList<>();
        list.add(new Node(1, "王杰"));
        list.add(new Node(2, "hahahah"));
        list.add(new Node(3, "hello word"));
        list.add(new Node(4, "迪丽热巴"));
        list.add(new Node(4, "第2个 迪丽热巴"));

        Map<Integer,String> map=new HashMap<>();
        for(Node n:list){
            map.put(n.getCode(),n.getName());

        }
        System.out.println(map);

        //第一个覆盖第二个
        Map<Integer, String> collect = list.stream().collect(Collectors.toMap(Node::getCode, Node::getName,(k1,k2)->k1));
        System.out.println(collect);
        //第二个覆盖第一个
        Map<Integer, String> collect1 = list.stream().collect(Collectors.toMap(Node::getCode, Node::getName,(k1,k2)->k2));
        System.out.println(collect1);

结果:

{1=王杰, 2=hahahah, 3=hello word, 4=第2个 迪丽热巴}
{1=王杰, 2=hahahah, 3=hello word, 4=迪丽热巴}
{1=王杰, 2=hahahah, 3=hello word, 4=第2个 迪丽热巴}

7、tomap收集时小技巧之

分组

//分组收集
Map<Integer, List<Node>> collect2 = list.stream().collect(Collectors.groupingBy(Node::getCode));

结果:

{1=[Node{code=1, name='王杰'}, Node{code=1, name='第二个王杰'}, Node{code=1, name='第三个王杰'}], 2=[Node{code=2, name='hahahah'}], 3=[Node{code=3, name='hello word'}], 4=[Node{code=4, name='迪丽热巴'}, Node{code=4, name='第2个 迪丽热巴'}]}

8、数组求和

        List<Integer> list =Arrays.asList(1,2,3,4,5,6,7);
        int sum = list.stream().mapToInt(s -> s).sum();

        System.out.println(sum);

结果:

28



版权声明:本文为m0_57659908原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。