Advantage compared to the flow set
delay execution flow characteristics
set or array acquisition stream mapping
common stream operation
method of outputting a reference sentence
structure using class and array references
public class StreamDemo { // flow control, printing @Test public void test1 () { List <String> Data = new new the ArrayList <String> (); data.add ( " Zhang Wuji " ); data.add ( " fascination " ); data.add ( " Zhang San " ); data.add ( " Barry group " ); data.add ( " two eggs, two Gouzi " ); . data.stream () filter ( S-> s.startsWith("张")) .filter(s -> s.length() == 3).forEach(System.out::println); } //获取流 @Test public void test2() { List<String> data = new ArrayList<String>(); Stream<String> stream = data.stream(); HashMap<String, Object> data2 = new HashMap<>(); Stream<String> keys = data2.keySet().stream(); Stream<Object> values = } / * * * If needed to map the flow element to another stream, the method can use the map. Method Signature: * Function The interface requires a functional interface parameters, T type data in the current stream can be converted into another type of flow R * / @Test public void test3() { String[] arr = {"张三", "李四", "张三丰"}; Stream<String> data = Stream.of("12", "13", "22"); Stream<Integer> result = data.map(str -> Integer.parseInt(str)); result.forEach(str -> System.out.println(str)); } /** * 如旧集合 Collection 当中的 size 方法一样,流提供 count 方法来数一数其中的元素个数: * / @Test public void Test4 () { String [] ARR = { " John Doe " , " John Doe " , " Zhang San " }; Stream <String> of arr1 = Stream.of (ARR); Stream <String> Result = arr1.filter (S -> s.startswith ( " Zhang " )); . the System OUT .println (result.count ()); } / * * * previous access: limit * limit convection method may be taken, only access to the first n. Method signature: * / @Test public void test5() { String[] arr = {"张三", "李四", "张三丰"}; Stream<String> arr1 = Stream.of(arr); Stream<String> limit = arr1.limit(1); Stream<String> result = limit.filter(s -> s.startsWith("张")); System.out.println(result.count()); } /** * 如果希望跳过前几个元素,可以使用 skip 方法获取一个截取之后的新流 */ @Test public void test6 () { String [] ARR = { " John Doe " , " John Doe " , " Zhang San " }; Stream <String> of arr1 = Stream.of (ARR); Stream <String> = Skip of arr1. skip ( . 1 ); the System. OUT .println (skip.count ()); } / * * * to skip before a new flow if desired several elements, obtaining a skip method can be used after the intercept * / @Test public void TEST7 () { String [] ARR = { " John Doe", "李四" , " Zhang San " }; Stream <String> = Stream.of of arr1 ( " Zhang San " ); Stream <String> = arr2 is Stream.of ( " Zhang Promise " ); Stream <String> the concat = Stream.concat (of arr1, arr2 is); concat.forEach (the System. OUT :: the println); } / * * * 1. the first member of a team name is the name of three characters long; storing a new collection. * 2. the first team after the screening just before three people; to store a new set. * 3. the second team member's name as long as the surname of the; storage to a new collection * 4. 第二个队伍筛选之后不要前2个人;存储到一个新集合中。 * 5. the two teams combined into a team; stored in a the new collection. * 6. create Person objects by name; storage to a new collection. * 7. 打印整个队伍的Person对象信息。 */ class Person { private String name; public Person(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + '}'; } } @Test public void test8() { List<String> one = new ArrayList<String>(); one.add("迪丽热巴"); one.add("宋远桥"); one.add("苏星河"); one.add("石破天"); one.add("石中玉"); one.add ( I " ); one.add ( " Zi " ); one.add ( " Hong Qigong " ); List <String> TWO = new new the ArrayList <String> (); two.add ( " Curly Na bar " ); two.add ( " Zhang Wuji " ); two.add ( " Zhao Liying " ); two.add ( " Zhang San " ); two.add ( " Nicholas Zhao Si " ); two.add("" Zhang day love " ); two.add ( " Zhang Ergou " ); Stream . <String> oneConcat = one.stream () filter (name -> name.length () == . 3 ) .limit ( . 3 ); Stream < String> twoConcat = two.stream () filter (name -> name.startsWith (. " Zhang " )) Skip (. 2 ); Stream.concat (oneConcat, twoConcat) .map (the Person :: new new ) .forEach (System . OUT :: the println); } / * * * :: "method reference", while the double colon is a new syntax * Lambda expressions written: s -> System.out.println (s) ; * 方法引用写法: System.out::println * 第一种语义是指:拿到参数之后经Lambda之手,继而传递给 System.out.println 方法去处理。 * 第二种等效写法的语义是指:直接让 System.out 中的 println 方法来取代Lambda。两种写法的执行效果完全一 * 样,而第二种方法引用的写法复用了已有方案,更加简洁。 */ /** * 通过函数引用静态方法 */ @Test public void test9() { test9Demo(s->{ System.out.println(s); }); test9Demo(System.out::println); } public void test9Demo(Printable p) { p.print("hello"); } Class ChildInterfacd the implements ParentInterface { @Override public String Method (String A) { the System. OUT .println (A); return null ; } } / * ** * reference method object name * / @Test public void test9Demo1 () { childInterfacd childInterfacd = new new childInterfacd (); test9Demo (childInterfacd :: method); } / * * * static method invocation * / @FunctionalInterface public interface Calcable{ int calc(int num); } public class Demo05Lamba the implements Calcable { @Override public int Calc ( int NUM) { return 0 ; } } // call the class constants @Test public void test10 () { // this example, the following two are equivalent wording: // Lambda expressions: n--> the Math.abs (n-) // method reference: the Math :: ABS test10Demo (- 10 , N-> the Math.abs (n-+ . 5 )); test10Demo ( - 10 , the Math :: ABS); } public void test10Demo ( int num Calcable c) { System. out .println (c.calc (num)); }