List collections are commonly used in projects. This article will share the foreach traversal and stream usage of lists.

List<String> List =Lists. NewArrayList ("a","b","c","d"); List.foreach ((anyThing)-> system.out.println (anyThing)); List. ForEach (any-> system.out.println (any)); ForEach (item->{if("b".equals(item)){system.out.println (item); }});Copy the code

List Stream common methods

In the project, I saw a colleague using stream when traversing the List, and I also wanted to learn about it.

List<T> list = listTest.stream().filter(detail -> { return ! Objects.equal(detail.getId(), currCombo.getId()); }).collect(Collectors.toList());Copy the code
Filter eligible data:Copy the code

list = list.stream() .filter(a -> ! b.BODY_EXAMINE.getCode().equals(a.getA()) && ! b.EXCEPTION.getCode().equals(a.getA())) .collect(Collectors.toList());

Copy the code

Stream provides a high-level abstraction of Java collection operations and expressions in an intuitive way similar to querying data from a database with SQL statements (beginner tutorial). A Stream here is different from a Stream in IO.

Example: Declare the Student object

public class Student { private String name; private Integer age; private Integer math; private Integer english; //get set public Student(String name, Integer age, Integer math, Integer english) { super(); this.name = name; this.age = age; this.math = math; this.english = english; } @Override public String toString() { return "Student [name=" + name + ", age=" + age + ", math=" + math + ", english=" + english + "]"; }}Copy the code

Stream Some common apis

public class StreamDemo { List<Student> list = null; @before public void beforetest() {list = array. asList(new Student("Tom", 18, 88, 90), new Student("Jerry", 20, 77, 89), new Student("Lily", 17, 98, 79), new Student("Lucy", 19, 70, 80), new Student("LiLei", 18, 88, 90), new Student("HanMeiMei", 21, 87, 79)); } @test public void streamtest() {// filter returns a stream with math > 80 and iterates over the output list.stream().filter(e->e.getMath()>80).forEach(System.out::println); //.foreach (e-> system.out.println (e)) // Count system.out.println (list.stream().count()); System.out.println(list.stream().filter(e-> LLDB tenglish ()+ LLDB etMath()> LLDB).count()); List.stream ().limit(3).foreach (system.out ::println); //skip the first n list.stream().skip(2).foreach (system.out ::println); //distinct removes duplicate data list.stream().distinct().foreach (system.out ::println); List.stream ().map(e->{e.setage ()+1); return e; }).forEach(System.out::println); Sorted ((a,b)->{return a.gettenglish ().compareto (b.gettenglish ())); }); Sorted ((a,b)->{return b.gettenglish ().compareto (a.gettenglish ())); }); // Return the first element Optional<Student> first = list.stream().findfirst (); System.out.println(first.get()); System.out.println(list.stream().findany ().get())); Println (list.stream().anymatch (e-> LLDB etName().equals("Tom"))); System.out.println(list.stream().allmatch (e-> LLDB ().equals("Tom"))); System.out.println(list.stream().nonematch (e-> equetName ().equals("Tom"))); Student Student = list.stream().findFirst().get(); Student student1 = list.stream().findany ().get(); Student student2 = list.stream().max((l1,l2)-> l2.getenglish ().compareto (l1.getenglish ()).get(); Student student2 = list.stream().max((l1,l2)-> l2.getenglish ()).get();  Student student3 = list.stream().max((l1,l2)-> l2.getenglish ().compareto (l1.getenglish ())).get(); // convert a stream object toList list.stream().filter(e-> LLDB etMath()>80).collect(receiver.tolist ()); Filter (e-> LLDB etMath()>80).collect(receiver.toset ()); / / the object of a statistical IntSummaryStatistics c = list. The stream () collect (Collectors. SummarizingInt (Student: : getEnglish)); System.out.println(c); //IntSummaryStatistics{count=6, sum=507, min=79, average=84.500000, Max =90}}Copy the code

Refer to the article

  • Blog.csdn.net/ndidai/arti…