前言

属于Java8新特性

代码实例

filter

filter用于过滤操作,将包含“f”的字符串过滤掉,如下所示:

@Test
public void streamTest() {
    List<String> strings = Arrays.asList("abc", "", "cdf", "", "ghn", "wid", "", "clf");
    //stream()方法将list转化成流,然后filter中设置过滤条件进行过滤,最后collect(Collectors.toList())将流收集起来并转化成List
    List<String> newStrings = strings.stream().filter(string -> string.contains("f")).collect(Collectors.toList());
    System.out.println(newStrings);
}

distinct

distinct用于去重,如下所示:

@Test
public void streamTest() {
    List<String> strings = Arrays.asList("abc", "", "cdf", "", "ghn", "wid", "", "clf");
    List<String> newStrings = strings.stream().distinct().collect(Collectors.toList());
    System.out.println(newStrings);
}

控制台输出:

[abc, , cdf, ghn, wid, clf]

可以看到,将重复的空字符串过滤掉了;需要注意的是,对象无法直接使用distinct去重,是因为对象的地址不同

limit

类似分页限制,如下所示,限制输出前3个:

@Test
public void streamTest() {
    List<String> strings = Arrays.asList("abc", "", "cdf", "", "ghn", "wid", "", "clf");
    List<String> newStrings = strings.stream().limit(3).collect(Collectors.toList());
    System.out.println(newStrings);
}

控制台输出:

[abc, , cdf]

skip

与limit(获取前n个元素)相反,skip是舍去前n个元素,如下所示:

@Test
public void streamTest() {
    List<String> strings = Arrays.asList("abc", "", "cdf", "", "ghn", "wid", "", "clf");
    List<String> newStrings = strings.stream().skip(3).collect(Collectors.toList());
    System.out.println(newStrings);
}

控制台输出:

[, ghn, wid, , clf]

map

map在实际应用中使用的很多,它是将List中每个元素经过函数处理后再返回,如下所示,在每个元素后面增加test:

@Test
public void streamTest() {
    List<String> strings = Arrays.asList("abc", "", "cdf", "", "ghn", "wid", "", "clf");
    List<String> newStrings = strings.stream().map(string -> string + "test").collect(Collectors.toList());
    System.out.println(newStrings);
}

控制台输出

[abctest, test, cdftest, test, ghntest, widtest, test, clftest]

flatMap

扁平化处理,与map不同的是,flatMap将所有元素放到了一起进行处理,而map是一个一个处理。此外flatMap中的函数返回的必须是个流,如下所示:

@Test
public void streamTest() {
    List<String> strings = Arrays.asList("abc", "", "cdf", "", "ghn", "wid", "", "clf");
    List<Character> newChars = strings.stream().flatMap(this::stream).collect(Collectors.toList());
    System.out.println(newChars);
}

public Stream<Character> stream(String string) {
    List<Character> characters = new ArrayList<>();
    for (Character character : string.toCharArray()) {
        characters.add(character);
    }
    return characters.stream();
}

控制台输出

[a, b, c, c, d, f, g, h, n, w, i, d, c, l, f]

sorted

排序,如下所示

@Test
public void streamTest() {
    List<String> strings = Arrays.asList("abc", "", "cdf", "", "ghn", "wid", "", "clf");
    List<String> newStrings = strings.stream().sorted().collect(Collectors.toList());
    System.out.println(newStrings);
}

控制台输出

[, , , abc, cdf, clf, ghn, wid]

以上都是默认的排序规则,也可以数字排序,但是复杂的排序规则需要将比较器作为参数传入sorted()方法中,如汉字比较就传入Collator.getInstance(Locale.CHINA)

anyMatch

如果任意一个元素符合要求,则返回true,否则返回false:

@Test
public void streamTest() {
    List<String> strings = Arrays.asList("abc", "", "cdf", "", "ghn", "wid", "", "clf");
    boolean b = strings.stream().anyMatch(string -> string.contains("a"));
    System.out.println(b);
}

allMatch

所有元素都需要满足要求才能返回true,否则返回false

noneMatch

与allMatch相反,所有元素都不满足要求返回true,否则flase

forEach

遍历所有元素

@Test
public void streamTest() {
    List<String> strings = Arrays.asList("abc", "", "cdf", "", "ghn", "wid", "", "clf");
    strings.stream().forEach(string -> System.out.println(string));
}

count

返回元素数量

@Test
public void streamTest() {
    List<String> strings = Arrays.asList("abc", "", "cdf", "", "ghn", "wid", "", "clf");
    System.out.println(strings.stream().count());
}

Q.E.D.

知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议

记录一些学习笔记、杂谈