Stream &Lambda 表达式常用功能测试
@SpringBootTest
class Jdk8DemoApplicationTests {
@Testpublic void lambdaTest(){
Arrays.asList( "a", "b", "d" ).forEach(e -> System.out.println( e ) );System.out.println("------------------------------------------------------------");Arrays.asList( "a", "b", "d" ).forEach( ( String e ) -> System.out.println( e ) );System.out.println("------------------------------------------------------------");Arrays.asList( "a", "b", "d" ).forEach( e -> {
if (e.equals("a")){
System.out.println("是a");}} );System.out.println("------------------------------------------------------------");String separator = ",";Arrays.asList( "a", "b", "d" ).forEach(( String e ) -> System.out.print( e + separator ) );System.out.println("------------------------------------------------------------");Arrays.asList( "a", "b", "d" ).sort( ( e1, e2 ) -> e1.compareTo( e2 ) );}@Testpublic void streamTest(){
List<User> userList = Arrays.asList(new User("用户1", 16, 0),new User("用户2", 17, 1),new User("用户3", 18, 0),new User("用户4", 19, 1),new User("用户5", 20, 0),new User("用户6", 21, 1),new User("用户7", 22, 0));userList.stream().collect(Collectors.toSet()).forEach(System.out::println);System.out.println("------------------------------------------------------------");userList.stream().filter(user -> user.getAge()>18 && user.getSex().equals(0)).collect(Collectors.toSet()).forEach(System.out::println);System.out.println("------------------------------------------------------------");List<Integer> numbers = Arrays.asList(-1, -2, 0, 4, 5);numbers.stream().map( n -> Math.abs(n)).forEach(n -> System.out.println("Element abs: " + n));System.out.println("------------------------------------------------------------");List<String> list = Arrays.asList("1 2", "3 4", "5 6");list.stream().flatMap(item -> Arrays.stream(item.split(" "))).forEach(System.out::println);list.stream().map(item -> Arrays.stream(item.split(" "))).forEach(n ->n.forEach(System.out::println));System.out.println("------------------------------------------------------------");List<Integer> numbers1 = Arrays.asList(-1, -2, 0, -1, 4, 5, 1);Integer total = numbers1.stream().reduce((t, n) -> t + n).get();System.out.println("Total: " + total);System.out.println("------------------------------------------------------------");numbers1.stream().collect(Collectors.toCollection(ArrayList::new)).forEach(System.out::println);System.out.println("------------------------------------------------------------");numbers1.stream().filter(n -> n>0).limit(2).forEach(System.out::println);System.out.println("------------------------------------------------------------");numbers1.stream().sorted().forEach(System.out::println);System.out.println("------------------------------------------------------------");numbers1.stream().sorted((o1,o2)->o1.compareTo(o2)).forEach(System.out::println);System.out.println("------------------------------------------------------------");numbers1.stream().sorted((o1,o2)->o2.compareTo(o1)).forEach(System.out::println);System.out.println("------------------------------------------------------------");numbers1.stream().map(t->t+=2).forEach(System.out::println);}}