标签:over static util filter 专注 List 集合 pre err foreach
之前: import java.util.ArrayList; import java.util.List; public class Demo02NormalFilter { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("张无忌"); list.add("周芷若"); list.add("赵敏"); list.add("张强"); list.add("张三丰"); List<String> zhangList = new ArrayList<>(); for (String name : list) { if (name.startsWith("张")) { zhangList.add(name); } } List<String> shortList = new ArrayList<>(); for (String name : zhangList) { if (name.length() == 3) { shortList.add(name); } } for (String name : shortList) { System.out.println(name); } } } Stream的更优写法 import java.util.ArrayList; import java.util.List; public class Demo03StreamFilter { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("张无忌"); list.add("周芷若"); list.add("赵敏"); list.add("张强"); list.add("张三丰"); list.stream() .filter(s ‐> s.startsWith("张")) .filter(s ‐> s.length() == 3) .forEach(System.out::println); } }
import java.util.*; import java.util.stream.Stream; public class Demo04GetStream { 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(); Vector<String> vector = new Vector<>(); // ... Stream<String> stream3 = vector.stream(); } }
import java.util.HashMap; import java.util.Map; import java.util.stream.Stream; public class Demo05GetStream { public static void main(String[] args) { Map<String, String> map = new HashMap<>(); // ... Stream<String> keyStream = map.keySet().stream(); Stream<String> valueStream = map.values().stream(); Stream<Map.Entry<String, String>> entryStream = map.entrySet().stream(); } }
import java.util.stream.Stream; public class Demo06GetStream { public static void main(String[] args) { String[] array = { "张无忌", "张翠山", "张三丰", "张一元" }; Stream<String> stream = Stream.of(array); } }
void forEach(Consumer<? super T> action); java.util.function.Consumer<T>接口是一个消费型接口。 Consumer接口中包含抽象方法void accept(T t),意为消费一个指定泛型的数据。 import java.util.stream.Stream; public class Demo12StreamForEach { public static void main(String[] args) { Stream<String> stream = Stream.of("张无忌", "张三丰", "周芷若"); stream.forEach(name‐> System.out.println(name)); } }
Stream<T> filter(Predicate<? super T> predicate); boolean test(T t); import java.util.stream.Stream; public class Demo07StreamFilter { public static void main(String[] args) { Stream<String> original = Stream.of("张无忌", "张三丰", "周芷若"); Stream<String> result = original.filter(s ‐> s.startsWith("张")); } }
<R> Stream<R> map(Function<? super T, ? extends R> mapper); R apply(T t); import java.util.stream.Stream; public class Demo08StreamMap { public static void main(String[] args) { Stream<String> original = Stream.of("10", "12", "18"); Stream<Integer> result = original.map(str‐>Integer.parseInt(str)); } }
long count(); import java.util.stream.Stream; public class Demo09StreamCount { public static void main(String[] args) { Stream<String> original = Stream.of("张无忌", "张三丰", "周芷若"); Stream<String> result = original.filter(s ‐> s.startsWith("张")); System.out.println(result.count()); // 2 } }
Stream<T> limit(long maxSize); import java.util.stream.Stream; public class Demo10StreamLimit { public static void main(String[] args) { Stream<String> original = Stream.of("张无忌", "张三丰", "周芷若"); Stream<String> result = original.limit(2); System.out.println(result.count()); // 2 } }
Stream<T> skip(long n); import java.util.stream.Stream; public class Demo11StreamSkip { public static void main(String[] args) { Stream<String> original = Stream.of("张无忌", "张三丰", "周芷若"); Stream<String> result = original.skip(2); System.out.println(result.count()); // 1 } }
static <T> Stream<T> concat(Stream<? extends T> a, Stream<? extends T> b) import java.util.stream.Stream; public class Demo12StreamConcat { public static void main(String[] args) { Stream<String> streamA = Stream.of("张无忌"); Stream<String> streamB = Stream.of("张翠山"); Stream<String> result = Stream.concat(streamA, streamB); } }
public class Person { private String name; public Person() {} public Person(String name) { this.name = name; } @Override public String toString() { return "Person{name=‘" + name + "‘}"; } public String getName() { return name; } public void setName(String name) { this.name = name; } } import java.util.ArrayList; import java.util.List; public class DemoArrayListNames { public static void main(String[] args) { //第一支队伍 ArrayList<String> one = new ArrayList<>(); one.add("迪丽热巴"); one.add("宋远桥"); one.add("苏星河"); one.add("石破天"); one.add("石中玉"); one.add("老子"); one.add("庄子"); one.add("洪七公"); //第二支队伍 ArrayList<String> two = new ArrayList<>(); two.add("古力娜扎"); two.add("张无忌"); two.add("赵丽颖"); two.add("张三丰"); two.add("尼古拉斯赵四"); two.add("张天爱"); two.add("张二狗"); // .... } } public class DemoArrayListNames { public static void main(String[] args) { List<String> one = new ArrayList<>(); // ... List<String> two = new ArrayList<>(); // ... // 第一个队伍只要名字为3个字的成员姓名; List<String> oneA = new ArrayList<>(); for (String name : one) { if (name.length() == 3) { oneA.add(name); } } // 第一个队伍筛选之后只要前3个人; List<String> oneB = new ArrayList<>(); for (int i = 0; i < 3; i++) { oneB.add(oneA.get(i)); } // 第二个队伍只要姓张的成员姓名; List<String> twoA = new ArrayList<>(); for (String name : two) { if (name.startsWith("张")) { twoA.add(name); } } // 第二个队伍筛选之后不要前2个人; List<String> twoB = new ArrayList<>(); for (int i = 2; i < twoA.size(); i++) { twoB.add(twoA.get(i)); } // 将两个队伍合并为一个队伍; List<String> totalNames = new ArrayList<>(); totalNames.addAll(oneB); totalNames.addAll(twoB); // 根据姓名创建Person对象; List<Person> totalPersonList = new ArrayList<>(); for (String name : totalNames) { totalPersonList.add(new Person(name)); } // 打印整个队伍的Person对象信息。 for (Person person : totalPersonList) { System.out.println(person); } } } 运行结果为: Person{name=‘宋远桥‘} Person{name=‘苏星河‘} Person{name=‘石破天‘} Person{name=‘张天爱‘} Person{name=‘张二狗‘}
import java.util.ArrayList; import java.util.List; import java.util.stream.Stream; public class DemoStreamNames { public static void main(String[] args) { List<String> one = new ArrayList<>(); // ... List<String> two = new ArrayList<>(); // ... // 第一个队伍只要名字为3个字的成员姓名; // 第一个队伍筛选之后只要前3个人; Stream<String> streamOne = one.stream().filter(s ‐> s.length() == 3).limit(3); // 第二个队伍只要姓张的成员姓名; // 第二个队伍筛选之后不要前2个人; Stream<String> streamTwo = two.stream().filter(s ‐> s.startsWith("张")).skip(2); // 将两个队伍合并为一个队伍; // 根据姓名创建Person对象; // 打印整个队伍的Person对象信息。 Stream.concat(streamOne, streamTwo).map(Person::new).forEach(System.out::println); } } 运行效果完全一样: Person{name=‘宋远桥‘} Person{name=‘苏星河‘} Person{name=‘石破天‘} Person{name=‘张天爱‘} Person{name=‘张二狗‘}
@FunctionalInterface public interface Printable { void print(String str); } public class Demo01PrintSimple { private static void printString(Printable data) { data.print("Hello, World!"); } public static void main(String[] args) { printString(s ‐> System.out.println(s)); } }
public class Demo02PrintRef { private static void printString(Printable data) { data.print("Hello, World!"); } public static void main(String[] args) { printString(System.out::println); } } 请注意其中的双冒号 :: 写法,这被称为“方法引用”,而双冒号是一种新的语法。
public class MethodRefObject { public void printUpperCase(String str) { System.out.println(str.toUpperCase()); } } @FunctionalInterface public interface Printable { void print(String str); } public class Demo04MethodRef { private static void printString(Printable lambda) { lambda.print("Hello"); } public static void main(String[] args) { MethodRefObject obj = new MethodRefObject(); printString(obj::printUpperCase); } }
@FunctionalInterface public interface Calcable { int calc(int num); } 第一种写法是使用Lambda表达式: public class Demo05Lambda { private static void method(int num, Calcable lambda) { System.out.println(lambda.calc(num)); } public static void main(String[] args) { method(‐10, n ‐> Math.abs(n)); } } 但是使用方法引用的更好写法是: public class Demo06MethodRef { private static void method(int num, Calcable lambda) { System.out.println(lambda.calc(num)); } public static void main(String[] args) { method(‐10, Math::abs); } }
@FunctionalInterface public interface Greetable { void greet(); } public class Human { public void sayHello() { System.out.println("Hello!"); } } public class Man extends Human { @Override public void sayHello() { System.out.println("大家好,我是Man!"); } //定义方法method,参数传递Greetable接口 public void method(Greetable g){ g.greet(); } public void show(){ //调用method方法,使用Lambda表达式 method(()‐>{ //创建Human对象,调用sayHello方法 new Human().sayHello(); }); //简化Lambda method(()‐>new Human().sayHello()); //使用super关键字代替父类对象 method(()‐>super.sayHello()); } } public class Man extends Human { @Override public void sayHello() { System.out.println("大家好,我是Man!"); } //定义方法method,参数传递Greetable接口 public void method(Greetable g){ g.greet(); } public void show(){ method(super::sayHello); } } Lambda表达式: () -> super.sayHello() 方法引用: super::sayHello
public class Husband { private void buyHouse() { System.out.println("买套房子"); } private void marry(Richable lambda) { lambda.buy(); } public void beHappy() { marry(() ‐> this.buyHouse()); } } public class Husband { private void buyHouse() { System.out.println("买套房子"); } private void marry(Richable lambda) { lambda.buy(); } public void beHappy() { marry(this::buyHouse); } }
public 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; } } public interface PersonBuilder { Person buildPerson(String name); } public class Demo09Lambda { public static void printName(String name, PersonBuilder builder) { System.out.println(builder.buildPerson(name).getName()); } public static void main(String[] args) { printName("赵丽颖", name ‐> new Person(name)); } } public class Demo10ConstructorRef { public static void printName(String name, PersonBuilder builder) { System.out.println(builder.buildPerson(name).getName()); } public static void main(String[] args) { printName("赵丽颖", Person::new); } } Lambda表达式: name -> new Person(name) 方法引用: Person::new
@FunctionalInterface public interface ArrayBuilder { int[] buildArray(int length); } public class Demo11ArrayInitRef { private static int[] initArray(int length, ArrayBuilder builder) { return builder.buildArray(length); } public static void main(String[] args) { int[] array = initArray(10, length ‐> new int[length]); } } public class Demo12ArrayInitRef { private static int[] initArray(int length, ArrayBuilder builder) { return builder.buildArray(length); } public static void main(String[] args) { int[] array = initArray(10, int[]::new); } } Lambda表达式: length -> new int[length] 方法引用: int[]::new
标签:over static util filter 专注 List 集合 pre err foreach
原文地址:https://www.cnblogs.com/alice-bj/p/12470852.html