1. Lambda表达式
package com.jsxy.wl;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;
public class LambdaTest1 {
@Test
public void test1() {
Runnable runnable = new Runnable() {
@Override
public void run() {
System.out.println("hello1");
}
};
runnable.run();
Runnable runnable1 = () -> System.out.println("hello2");
runnable1.run();
}
@Test
public void test2() {
Consumer<String> consumer = new Consumer<String>() {
@Override
public void accept(String s) {
System.out.println(s);
}
};
consumer.accept("hello1");
Consumer<String> consumer1 = s -> System.out.println(s);
consumer1.accept("hello2");
}
@Test
public void test3() {
Comparator<Integer> comparator = new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
System.out.println(o1);
System.out.println(o2);
return o1.compareTo(o2);
}
};
int compare = comparator.compare(1, 2);
System.out.println(compare);
Comparator<Integer> comparator1 = (o1, o2) -> {
System.out.println(o1);
System.out.println(o2);
return o1.compareTo(o2);
};
int compare1 = comparator1.compare(2, 1);
System.out.println(compare1);
}
@Test
public void test5(){
List<String> list = Arrays.asList("北京","南京","东京","西京","天津");
List<String> list1 = filterString(list, new Predicate<String>() {
@Override
public boolean test(String s) {
return s.contains("京");
}
});
System.out.println(list1);
List<String> list2 = filterString(list, s -> s.contains("京"));
System.out.println(list2);
}
public List<String> filterString(List<String> list, Predicate<String> predicate){
ArrayList<String> filterList = new ArrayList<>();
for (String s: list ) {
if(predicate.test(s)){
filterList.add(s);
}
}
return filterList;
}
}
2. StreamAPI
package com.jsxy.wl;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class StreamTest {
@Test
public void test1(){
List<String> list = new ArrayList<>();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
Stream<String> stream = list.stream();
Stream<String> parallelStream = list.parallelStream();
}
@Test
public void test2(){
int[] ints = new int[]{1,2,3,4,5};
IntStream stream = Arrays.stream(ints);
User user1 = new User("wang",29);
User user2 = new User("li",28);
User[] users = new User[]{user1,user2};
Stream<User> stream1 = Arrays.stream(users);
}
@Test
public void test3(){
Stream<Integer> integerStream = Stream.of(1, 2, 3, 4, 5);
}
@Test
public void test4(){
User user1 = new User("wang",29);
User user2 = new User("li",28);
List<User> userList = new ArrayList<>();
userList.add(user1);
userList.add(user2);
Stream<User> stream = userList.stream();
stream.filter(user -> user.getName().equals("li")).forEach(System.out::println);
userList.stream().limit(1).forEach(System.out::println);
userList.stream().skip(1).forEach(System.out::println);
userList.stream().map(User::getName).filter(name -> name.length() > 3).forEach(System.out::println);
List<Integer> list = Arrays.asList(34, 23, 56, 6, 98, 37);
list.stream().sorted().forEach(System.out::println);
userList.stream().sorted((u1,u2) ->
Integer.compare(u1.getAge(), u2.getAge())
).forEach(System.out::println);
}
}