Simple application Java8 Stream stream

Reference details of the document:

https://www.ibm.com/developerworks/cn/java/j-lo-java8streamapi/index.html click on the link to open

Application object: array, list


1 map mapping / extraction element

    stream.Map () is generated by a 1: 1 mapping, each input element, are transformed into rules in accordance with another element;

    List<String> wordList = new LinkedList<>();

    wordList.add("a");

    // elements into capital

    List<String> outList = wordList.stream().map(String::toUpperCase).collect(Collectors.toList());

    // set of attributes entity bean extract

    List<Cat> cats = new ArrayList<Cat>();

    List<Integer> list = cats.stream().map(Cat::getAge).collect(Collectors.toList());

2 screening filter

     stream.filter () Stream original screening for certain rules selected by screening element is left out to generate a new Stream;

     Integer[] sixNums = {1, 2, 3, 4, 5, 6};

     // divisible by 2

     Integer[] evens = Stream.of(sixNums).filter(n -> n%2 == 0).toArray(Integer[]::new);

Sort 3

    Entity class list sorting

    List<Cat> collect = cats.stream().sorted(Comparator.comparing(Cat::getBirthDay)).collect(Collectors.toList());

4 reduce() 

    The main effect of this method is the combination of elements Stream, a string concatenation, the value of the acquired sum, min, max, average;

Guess you like

Origin blog.csdn.net/weixin_37794901/article/details/81060813