Stream
1. Use Stream to traverse the collection
public class Demo01 {
public static void main(String[] args) {
List<String> list=new ArrayList<>();
list.add("Kobe");
list.add("James");
list.add("Jordan");
list.add("Curry");
List<String> listA=new ArrayList<>();
list.stream().filter(name->name.length()<6)
.filter(name->name.startsWith("K"))
.forEach(name-> System.out.println(name));
}
}
2. Get the stream
public class Demo01 {
public static void main(String[] args) {
List<String> list=new ArrayList<>();
Stream<String> stream1=list.stream();
Set<String> set=new HashSet<>();
Stream<String> stream2=set.stream();
Map<String,String> map=new HashMap<>();
//获取键,存储到一个set集合中
Set<String> keySet=map.keySet();
Stream<String> stream3=keySet.stream();
//获取值,存储到一个Collection集合中
Collection<String> values=map.values();
Stream<String> stream4 = values.stream();
//获取键值对
Set<Map.Entry<String, String>> entries = map.entrySet();
Stream<Map.Entry<String, String>> stream5 = entries.stream();
}
}
3. Common methods
Delay method: the return value type is still a method of the Stream interface's own type, so chained calls are supported.
Final method: the return value type is no longer a method of the Stream interface's own type, so chain calls
are not supported one by one: forEach
void forEach(Consumer<? super T> action)
public class Demo02 {
public static void main(String[] args) {
Stream<String> stream=Stream.of("Kobe","James","Jordan","Curry");
stream.forEach(name-> System.out.println(name));
}
}
Filter: filter
public class Demo02 {
public static void main(String[] args) {
Stream<String> stream=Stream.of("Kobe","James","Jordan","Curry");
Stream<String> stream1 = stream.filter(name -> name.length() < 6)
.filter(name->name.startsWith("K"));
stream1.forEach(name-> System.out.println(name));
}
}
Mapping: map
If you need to map elements in a stream to another stream, you can use the map method
Stream map(Function<? super T,? extends R> mapper);
public class Demo02 {
public static void main(String[] args) {
Stream<String> stream=Stream.of("24","23","30","8");
//使用map方法,把字符串类型的整数,转换为Integer类型
Stream<Integer> stream1 = stream.map((String s) -> {
return Integer.parseInt(s);
});
stream1.forEach(num-> System.out.println(num));
}
}
Number of statistics: count
public class Demo02 {
public static void main(String[] args) {
Stream<String> stream=Stream.of("24","23","30","8");
System.out.println(stream.count());
}
}
Take the first few: limit
Stream limit(long maxSize)
public class Demo02 {
public static void main(String[] args) {
Stream<String> stream=Stream.of("24","23","30","8","10","35");
Stream<String> stream1=stream.limit(2);
stream1.forEach(num-> System.out.println(num));
}
}
Skip the first few: skip
public class Demo02 {
public static void main(String[] args) {
Stream<String> stream=Stream.of("24","23","30","8","10","35");
Stream<String> stream1=stream.skip(2);
stream1.forEach(num-> System.out.println(num));
}
}
Combination: concat
public class Demo02 {
public static void main(String[] args) {
Stream<String> stream1=Stream.of("24");
Stream<String> stream2=Stream.of("Kobe");
Stream<String> res=Stream.concat(stream1,stream2);
res.forEach(num-> System.out.println(num));
}
}
Method reference
1. Refer to member method by object name
Prerequisite: The object name already exists, and the member method also already exists
public class Demo01 {
public static void printString(Printable p){
p.print("Hello");
}
public static void main(String[] args) {
//调用printString方法,方法的参数是一个函数式接口,可以调用Lambda表达式
printString((s)->{
//创建Method对象
Method m=new Method();
m.printUpper(s);
});
/*
使用方法引用优化Lambda
创建Method对象
*/
Method m=new Method();
printString(m::printUpper);
}
}
2. Reference static methods by class name. The
class already exists, and the static member methods also exist
public class Demo02 {
//定义一个方法,方法参数传递整数和函数式接口
public static int method(int number,Calculate c){
return c.calAbs(number);
}
public static void main(String[] args) {
int number = method(-10, (num) -> {
return Math.abs(num);
});
System.out.println(number);
//方法引用优化Lambda
int number2 = method(-10, Math::abs);
System.out.println(number2);
}
}
3. Referencing member methods through super
public class Man extends Human{
public void sayHello(){
System.out.println("hello");
}
//定义一个参数传递Greetable接口
public void method(Greetable g){
g.greet();
}
public void show(){
/* //调用method方法,方法的参数Greetable是一个函数式接口
method(()->{
Human h=new Human();
h.sayHello();
});*/
method(super::sayHello);
}
public static void main(String[] args) {
new Man().show();
}
}
4. Refer to member methods through this
public class Father {
//通过this引用本类成员方法
public void method1(){
System.out.println("buy something");
}
//定义方法参数传递Rich接口
public void method2(Rich r){
r.buy();
}
public void method3(){
method2(()->{
this.method1();
});
//使用方法引用优化
method2(this::method1);
}
public static void main(String[] args) {
new Father().method3();
}
}
5. Class constructor reference
public class Demo01 {
public static void method(String name,PersonBuilder pb){
Person person=pb.personBuilder(name);
System.out.println(person.getName());
}
public static void main(String[] args) {
//调用method方法,PersonBuilder是一个函数式接口
method("Kobe",(String name)->{
return new Person(name);
});
//方法引用优化Lambda表达式构造方法与创建对象已知
method("Kobe",Person::new);
}
}
6. Reference to the array constructor
public class Demo02 {
public static int[] creatArr(int length,ArrayBuilder ab){
return ab.builderArray(length);
}
public static void main(String[] args) {
int[] arr = creatArr(10, (len) -> {
return new int[len];
});
System.out.println(arr.length);
//使用方法引用优化Lambda
int[] arr2 = creatArr(10, int[]::new);
System.out.println(arr2.length);
}
}