public class Test {
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
}).start();
new Thread(() -> {
System.out.println(Thread.currentThread().getName());
}).start();
}
public class NoParameterTest {
public static void main(String[] args) {
getShow(new NoParameterService() {
@Override
public void show() {
System.out.println("show() 方法1");
}
});
getShow(() -> System.out.println("show() 方法2"));
}
public static void getShow(NoParameterService noParameterService){
noParameterService.show();
}
public class ContParameterTest {
public static void main(String[] args) {
ArrayList<Person> list = new ArrayList<>();
PersonList(list);
Collections.sort(list,(x,y) -> x.getScore().compareTo(y.getScore()));
}
public static void PersonList(ArrayList<Person> list) {
list.add(new Person("A", 01, 100));
list.add(new Person("B", 02, 600));
list.add(new Person("C", 03, 300));
list.add(new Person("D", 04, 200));
list.add(new Person("E", 05, 500));
}
public class Test {
public static void main(String[] args) {
ArrayList<User> userList = new ArrayList<>();
userList.add(new User("E", 5, "男"));
userList.add(new User("A", 10, "女"));
userList.add(new User("B", 20, "男"));
userList.add(new User("C", 15, "女"));
userList.add(new User("D", 25, "男"));
userList.add(new User("F", 30, "女"));
userList.stream().filter(x -> x.getAge() > 10).map(User::getEmplyNm).collect(Collectors.toList());
userList.stream().filter(x -> x.getAge() > 10).findFirst().get();
userList.stream().filter(x -> x.getAge() > 10).limit(1).collect(Collectors.toList());
userList.stream().max(Comparator.comparing(User::getAge)).map(User::getAge).get();
userList.stream().min(Comparator.comparing(User::getAge)).map(User::getAge).get();
List<String> strArray = Arrays.asList("A", "AB", "ABC", "ABCD");
strArray.stream().max(Comparator.comparing(String::length)).get();
List<Integer> intArray = Arrays.asList(10, 20, 30, 40, 50);
intArray.stream().max((x, y) -> x.compareTo(y)).get();
String[] strArrays = {"a", "ab", "abc", "abed", "abide"};
Arrays.stream(strArrays).map(x -> x.toUpperCase(Locale.ROOT)).collect(Collectors.toList());
Arrays.stream(strArrays).map(String::toUpperCase).collect(Collectors.toList());
List<Integer> numArray = Arrays.asList(9, 19, 29, 39, 49);
numArray.stream().map(x -> x + 1).collect(Collectors.toList());
userList.stream().map(x -> {
User user = new User().setAge(x.getAge() + 100);
return user;
}).collect(Collectors.toList());
userList.stream().map(x -> {
x.setAge(x.getAge() + 100);
return x;
}).collect(Collectors.toList());
List<String> strData = Arrays.asList("a,b,c,d", "e,f,g,h");
strData.stream().flatMap(x -> {
String[] split = x.split(",");
Stream<String> stream = Arrays.stream(split);
return stream;
}).collect(Collectors.toList());
List<Integer> noArray = Arrays.asList(10, 20, 30, 40, 50);
noArray.stream().reduce(new BinaryOperator<Integer>() {
@Override
public Integer apply(Integer integer, Integer integer2) {
return integer + integer2;
}
}).get();
noArray.stream().reduce((x, y) -> x + y).get();
noArray.stream().reduce(0, Integer::sum);
noArray.stream().reduce((x, y) -> x * y).get();
Integer integer = noArray.stream().reduce((x, y) -> x > y ? x : y).get();
Integer reduce = noArray.stream().reduce(0, Integer::max);
ArrayList<Person> list = CollectionUtil.newArrayList();
list.add(new Person("A", 01, 100));
list.add(new Person("B", 02, 200));
list.add(new Person("C", 03, 300));
list.add(new Person("D", 04, 400));
list.add(new Person("E", 05, 500));
list.stream().map(Person::getScore).reduce((x, y) -> x + y).get();
list.stream().map(Person::getScore).reduce(Integer::sum).get();
list.stream().map(Person::getScore).reduce((x, y) -> x > y ? x : y).get();
list.stream().map(Person::getScore).reduce(Integer::max).get();
String collect = list.stream().map(Person::getEmplyNm).collect(Collectors.joining(","));
List<Integer> soreArray = Arrays.asList(20, 30, 10, 50, 40);
soreArray.stream().sorted().collect(Collectors.toList());
soreArray.stream().sorted(Comparator.comparing(x -> x)).collect(Collectors.toList());
soreArray.stream().sorted(Comparator.comparing(x -> x.toString()).reversed()).collect(Collectors.toList());
String[] array1 = {"a", "b", "c", "e"};
String[] array2 = {"a", "b", "f", "g"};
Stream<String> array11 = Stream.of(array1);
Stream<String> array22 = Arrays.stream(array2);
Stream.concat(array11, array22).collect(Collectors.toSet());
Stream.concat(array11, array22).distinct().collect(Collectors.toList());
Stream.iterate(1, x -> x + 2).limit(3).collect(Collectors.toList());
Stream.iterate(1, x -> x + 2).skip(1).limit(3).collect(Collectors.toList());
}
}
public class Test {
public static void main(String[] args) {
Optional<String> op1 = Optional.of("A");
Optional<String> op2 = Optional.ofNullable("A");
Optional<Object> op3 = Optional.ofNullable(null);
Optional<Object> op4 = Optional.empty();
Object o = op4.orElseGet(() -> {
return "hello";
});
op1.ifPresent(x -> System.out.println(x));
op1.ifPresent(System.out::println);
Person person = new Person().setEmplyNm("abc").setEmplyNo(01).setScore(100);
String name = getName(Optional.ofNullable(person));
System.out.println(name);
}
public static String getName(Person person) {
if (ObjectUtil.isNotNull(person)) {
if (StrUtil.isNotEmpty(person.getEmplyNm())) {
return person.getEmplyNm().toUpperCase(Locale.ROOT);
} else {
return null;
}
} else {
return null;
}
}
public static String getName(Optional<Person> person) {
if (person.isPresent()) {
return person.map(Person::getEmplyNm)
.map(String::toUpperCase)
.orElse(null);
}
return null;
}
}