Java8 has its own functional interface

  • Predicate Boolean test(T T) Passing in a parameter returns the Boolean value
  • Consumer void Accept (T T) Passes an argument with no return value
  • Function

    R apply(T T) passes a parameter and returns another type
    ,r>

To prepare data

Private static List<Student> computerClub = Arrays. AsList (new Student("2015134001", "xiaomin ", 15, "1501"), New Student("2015134003", "xiao Wang ", 14, "1503"), New Student("2015134006"," Xiao Zhang ", 15, "1501"), new Student("2015134008", "Liang ", 17, "1505")); Private static List<Student> basketballClub = array. asList(new Student("2015134012", "small C ", 13, "1503"), New Student("2015134013", "小 S ", 15, "1504"), new Student("2015134018", 15, "1504"), new Student("2015134018", "Little Y ", 16, "1505"); Private static List<Student> pingpongClub = Arrays. AsList (new Student("2015134022", "xiaou ", 16, "1502"), New Student("2015134021", "I ", 14, "1502"), new Student("2015134026"," M ", 17, "1504"), new Student("2015134027", "Small n", 16, "1504"); private static List<List<Student>> allClubStu = new ArrayList<>(); allClubStu.add(computerClub); allClubStu.add(basketballClub); allClubStu.add(pingpongClub);Copy the code

There are three ways to create a stream

  • Collection of the Collection. The stream ()
  • Static method stream.of
  • An array of Arrays. The stream
Stream<Student> Stream = basketballclub.stream (); / / 2. Static methods Stream < String > stream2. = Stream of (" a ", "b", "c"); Array String[] arr = {"a","b","c"}; Stream<String> stream3 = Arrays.stream(arr);Copy the code

Terminate Stream

  • Foreach (Consumer C) Traversal operation
  • Collect converts flows to other forms
  • Max (Comparator) returns the maximum value in the stream
  • Min (Comparator) returns the minimum value in the stream
  • Count returns an overview of the elements in the stream

Iv. Collectors Specific methods

  • ToList List collects elements from the stream into the List
  • ToSet Set collects elements from the stream into a Set
  • ToCollection Coolection Collects elements from the stream into the Collection
  • GroupingBy Map

    Groups the flow according to the K attribute
    ,list>
  • PartitioningBy Map< Boolean, List> Groups Boolean values
List<Student> collect = computerclub.stream ().collect(Collectors. ToList ()); Set<Student> collect1 = pingpongClub.stream().collect(Collectors.toSet()); // Note that the key must be unique. If it is not unique, an error will be reported. Instead of overwriting map <String like a normal map, String> collect2 = pingpongClub.stream() .collect(Collectors.toMap(Student::getIdNum, Student::getName)); // Group by Map<String, List<Student>> collect3 = pingpongClub.stream() .collect(Collectors.groupingBy(Student::getClassNum)); // String concatenation The first argument is a delimiter the second argument is a prefix and the third argument is a suffix String collect4 = Pingpongclub.stream ().map(Student::getName).collect(Collectors. Joining (",", "[","] "); pingpongclub.stream ().map(Student::getName). Map<String, List<Student>> Collect5 = stream. of(basketballClub, pingpongClub, computerClub) .flatMap(e -> e.stream().filter(s -> s.getAge() < 17)) .collect(Collectors.groupingBy(Student::getClassNum)); ConcurrentMap<Boolean, List<Student>> Collect6 = stream. of(basketballClub, pingpongClub, computerClub) .flatMap(Collection::stream) .collect(Collectors.groupingByConcurrent(s -> s.getAge() > 16));Copy the code

5. Stream intermediate operation

1. Filter (Predicate) Filters certain elements of the stream

Computerclub.stream ().filter(e -> LLDB ().equals("1501")).foreach (system.out ::println); List<Student> collect = computerclub.stream ().filter(e -> LLDB () > 15).collect(Collectors.Copy the code

2. Map (Function f) takes elements from the stream and maps them to new elements, such as the name attribute from the student object

List<String> collect1 = basketballclub.stream ().map(e -> LLDB etName() + "^_^") .collect(Collectors.toList()); collect1.forEach(System.out::println); // little c^_^ _ ^_^ // little s^_^ _^ // little d^_^ _ ^_^ // little y^_^ _ ^_^Copy the code

3. FlatMap (Function f) concatenates all the elements in a stream into a stream

List<Student> collect2 = stream. of(basketballClub, computerClub, pingpongClub) .flatMap(e -> e.stream().filter(s -> s.getAge() > 15)) .collect(Collectors.toList()); collect2.forEach(System.out::println); List<Student> collect3 = allclubstu.stream ().flatmap (e -> e.stam ().filter(s -> s.gage ()) >  15)) .collect(Collectors.toList()); collect3.forEach(System.out::println);Copy the code

Peek (Consumer C) gets elements in the stream and operates on elements in the stream. Unlike foreach, peek does not truncate the stream and can continue to operate on the stream

List<Student> collect = basketballclub.stream (). Peek (e -> e.setName(LLDB etName() + "^_^")) .collect(Collectors.toList()); collect.forEach(System.out::println); / / Student {idNum = '2015134012', name = 'small c ^_^' age = 13, classNum = '1503'} / / Student {idNum = '2015134013', name = 'small s ^_^', //Student{idNum='2015134015', name=' d^_^', age=15, //Student{idNum='2015134018', name=' _ ^_^', age=16, classNum=' _^ _ '}Copy the code

5. Distinct () is de-weighted by equals and hashCode of the elements generated by the stream

6. Limit (long val) truncates the stream by taking the first val

7. Sorted (Comparator) Produces a new stream that is sorted according to the Comparator rules

8. Sorted () produces a new stream, sorted in the natural order

List<String> list = Arrays.asList("b","b","c","a"); list.forEach(System.out::print); //bbca List<String> collect = list.stream().distinct().sorted().collect(Collectors.toList()); collect.forEach(System.out::print); List<String> collect1 = list.stream().distinct().sorted().limit(2).collect(Collectors.toList()); collect1.forEach(System.out::print); //abCopy the code

Six, match,

  • Booelan allMatch(Predicate) both match
  • Boolean anyMatch(Predicate) Matches either element
  • Boolean noneMatch(Predicate) doesn’t match
boolean b = basketballClub.stream().allMatch(e -> e.getAge() < 20);
boolean b1 = basketballClub.stream().anyMatch(e -> e.getAge() < 20);
boolean b2 = basketballClub.stream().noneMatch(e -> e.getAge() < 20);
Copy the code

Look for elements

  • FindFirst – Returns the first element
  • FindAny – Returns any element in the current stream
Optional<Student> first = basketballClub.stream().findFirst();
if (first.isPresent()) {
    Student student = first.get();
    System.out.println(student);
}

Optional<Student> any = basketballClub.stream().findAny();
if (any.isPresent()) {
    Student student2 = any.get();
    System.out.println(student2);
}
Optional<Student> any1 = basketballClub.stream().parallel().findAny();
System.out.println(any1);
Copy the code

Count and extremum

  • Count – Returns the total number of elements in the stream
  • Max – Returns the maximum value in the stream
  • Min – Returns the minimum value in the stream
long count = basketballClub.stream().count();
Optional<Student> max = basketballClub.stream().max(Comparator.comparing(Student::getAge));
if (max.isPresent()) {
    Student student = max.get();
}
Optional<Student> min = basketballClub.stream().min(Comparator.comparingInt(Student::getAge));
if (min.isPresent()) {
    Student student = min.get();
}
Copy the code

Nine,

I believe XDM has understood how to use Java streaming programming, point attention, not lost, focus on programmers have, every day to share different Java knowledge, if you want to know more about Java knowledge as well as the interview I sorted out a side of my ownGitHub, you can check it yourself if you need to