開胃菜 ——實現遍歷集合,開啟Stream流的便利化 import java.util.ArrayList; import java.util.Collections; public class Main{ public static void main(String[] args) { Array ...
開胃菜
——實現遍歷集合,開啟Stream流的便利化
import java.util.ArrayList; import java.util.Collections; public class Main{ public static void main(String[] args) { ArrayList<String> list = new ArrayList<String>(); ArrayList<String> list1 = new ArrayList<String>(); Collections.addAll(list,"ab","cea","fshwe","dea"); list.stream(). filter(str -> str.contains("a")). filter(str -> str.length() == 3). forEach(str-> System.out.println(str)); } }
列印結果:
cea
dea
走進Stream流的世界
1.0 獲取stream流的方法
import java.util.*; import java.util.stream.Stream; public class Main{ public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(); list.stream();//第一種方式 //特別的Map集合也可以間接的獲取 Map<String,Integer> map=new HashMap<>(); Set<String> keySet = map.keySet(); keySet.stream(); Collection<Integer> integers = map.values(); integers.stream(); Set<Map.Entry<String, Integer>> entries = map.entrySet(); entries.stream(); //第二種方式 Stream<Integer> integerStream = Stream.of(1, 2, 3, 4, 5);//數組 } }
2.0 Stream流的方法
(1)forEach
import java.util.stream.Stream; public class Main{ public static void main(String[] args) { Stream<String> stream = Stream.of("a", "b", "c", "d", "e"); stream.forEach((str)-> System.out.print(str+" ")); //a b c d e } }
(2) filter()
import java.util.stream.Stream; public class Main{ public static void main(String[] args) { Stream<String> stream = Stream.of("baby", "boy", "home", "dream", "eage"); Stream<String> a = stream.filter(str -> str.contains("a")).filter(str -> str.length() >=3); a.forEach((str)-> System.out.print(str+" ")); //baby dream eage } }
(3)Stream流的特點
(4)Map方法
import java.util.stream.Stream; public class Main{ public static void main(String[] args) { Stream<String> stream = Stream.of("12", "34", "56"); stream.map((str)->Integer.parseInt(str)+1). forEach((Str)-> System.out.print(Str+" ")); //13 35 57 } }
(5)count方法
(6)limit方法
(7)skip方法
(8)concat方法
方法引用
—— 簡便你我他
1.0 通過對象名引用成員方法
public class Main{ public static void PrintString(MessageBuilder mes) { mes.bulidString("hello"); } public static void main(String[] args) { //Lambda PrintString((s)->{ Printer pri=new Printer(); pri.PrintUpper(s);//HELLO }); //方法引用 Printer printer=new Printer(); PrintString(printer::PrintUpper);//HELLO } }
2.0 通過類名引用靜態方法
public class Main{ public static int calmathod(int num,MessageBuilder mes) { return mes.Cal(num); } public static void main(String[] args) { int num=-10; /* int calmathod = calmathod(num, (number) -> { return Math.abs(number); });*/ int calmathod=calmathod(num,Math::abs); System.out.println(calmathod);//10 } }
3.0 通過super引用父類的方法
public class Main extends FU{ @Override public void show(){ System.out.println("Hello,My soulmate!"); } public void method(MessageBuilder mes){ mes.greet(); } public void Print()//調用父類方法 { /* method(()->{ FU fu = new FU(); fu.show(); }); */ method(super::show);//Hello,My friend! } public static void main(String[] args) { new Main().Print(); } }
4.0 通過this調用本類中的成員方法
public class Main{ public void method() { System.out.println("Hello world!"); } public void showMethod(MessageBuilder mes){//函數式介面 mes.show(); } public void MethodPrint() { /* showMethod(()->{ this.method(); }); */ showMethod(this::method); } public static void main(String[] args) { new Main().MethodPrint();//Hello world! } }
5.0 類的構造器(構造方法)
public class Person { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public Person() { } public Person(String name) { this.name = name; } }
@FunctionalInterface public interface MessageBuilder { public abstract Person show(String name); }
public class Main{ public static String Method(String name,MessageBuilder mes) { Person person = mes.show(name); return person.getName(); } public static void main(String[] args) { String name="布丁"; /* String method = Method(name,(str)->{ return new Person(str); }); */ String method = Method(name, Person::new); System.out.println(method); } }
6.0 數組的構造器引用
@FunctionalInterface public interface MessageBuilder { public abstract int[] Builder(int length); }
public class Main{ public static int[] Method(int length,MessageBuilder mes) { int[] builder = mes.Builder(length); return builder; } public static void main(String[] args) { /* int[] method = Method(3, (length) -> { return new int[length]; }); */ int[] method = Method(3, int[]::new); System.out.println(method.length); } }