标签:一个 top ddd java and class length framework uppercase
package com.macro.cloud;
import com.macro.cloud.domain.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.convert.converter.Converter;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.*;
import java.util.function.*;
@RunWith(SpringRunner.class)
@SpringBootTest
public class RibbonServiceApplicationTests {
@Test
public void contextLoads() {
Runnable runnable=new Runnable() {
@Override
public void run() {
System.out.println("runnable:hello");
}
};
new Thread(runnable).start();
//等效于上面
Runnable runnable01= () -> System.out.println("runnable01:hello");
new Thread(runnable01).start();
TreeSet<String> treeSet01=new TreeSet<>(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return Integer.compare(o1.length(),o2.length());
}
});
treeSet01.add("aa");
treeSet01.add("aaa");
treeSet01.add("a");
System.out.println("treeSet01:"+treeSet01);
TreeSet<String> treeSet02=new TreeSet<>((o1, o2) -> Integer.compare(o1.length(),o2.length()));
treeSet02.add("aa");
treeSet02.add("a");
treeSet02.add("aaa");
System.out.println("treeSet02:"+treeSet01);
TreeSet<String> treeSet03=new TreeSet<>(Comparator.comparingInt(String::length));
treeSet03.add("aaa");
treeSet03.add("a");
treeSet03.add("aa");
System.out.println("treeSet03:"+treeSet01);
}
@Test
public void testConsumer(){
//一个参数,小括号可省略
Consumer<String> conn= x-> System.out.println(x);
conn.accept("hello");
/**2.andThen链式*/
Consumer<Integer> consumer1 = x -> System.out.println("first x : " + x);
Consumer<Integer> consumer2 = x -> {
System.out.println("second x : " + x);
// throw new NullPointerException("throw exception test");
};
Consumer<Integer> consumer3 = x -> System.out.println("third x : " + x);
consumer1.andThen(consumer2).andThen(consumer3).accept(1);
//作用:1.处理对象中的某个字段值
Consumer<User> userConsumer = x ->{
x.setId(1L);
x.setUsername("xll");
};
User user = new User();
user.setPassword("123456");
userConsumer.accept(user);
System.out.println(user.toString());
DoubleConsumer doubleConsumer = x ->{
System.out.println("doubleConsumer:"+(x+1));
};
DoubleConsumer doubleConsumer01 = x ->{
System.out.println("doubleConsumer:"+(x));
};
DoubleConsumer doubleConsumer02 = x ->{
System.out.println("doubleConsumer:"+(x+1));
};
doubleConsumer.andThen(doubleConsumer01).andThen(doubleConsumer02).accept(1001);
LongConsumer longConsumer = x ->{
System.out.println("longConsumer:"+(x+1));
};
LongConsumer longConsumer01 = x ->{
System.out.println("longConsumer:"+(x));
};
LongConsumer longConsumer02 = x ->{
System.out.println("longConsumer:"+(x+1));
};
longConsumer.andThen(longConsumer01).andThen(longConsumer02).accept(1001L);
ObjIntConsumer<User> objIntConsumer = (x,y) ->{
System.out.println("第"+y+",user:"+x);
};
User user1 = new User();
user1.setId(1L);
objIntConsumer.accept(user1, 1);
ObjIntConsumer<User> objIntConsumer01 = (x,y) ->{
System.out.println("第"+y+",user:"+x);
};
User user2 = new User();
user1.setId(2L);
objIntConsumer01.accept(user2, 2);
ObjIntConsumer<User> objIntConsumer02 = (x,y) ->{
System.out.println("第"+y+",user:"+x);
};
User user3 = new User();
user1.setId(3L);
objIntConsumer02.accept(user3, 3);
}
@Test
public void testConverter(){
Converter<String, Integer> converter = Integer::valueOf;
Integer converted = converter.convert("123");
System.out.println(converted+1); // 123
}
@Test
public void testPredicate(){
Predicate<String> predicate = (s) -> s.length() > 0;
predicate.test("foo"); // true
predicate.negate().test("foo"); // false
Predicate<Boolean> nonNull = Objects::nonNull;
Predicate<Boolean> isNull = Objects::isNull;
Predicate<String> isEmpty = String::isEmpty;
Predicate<String> isNotEmpty = isEmpty.negate();
}
@Test
public void testFunction(){
Function<String, Integer> toInteger = Integer::valueOf;
Function<String, String> backToString = toInteger.andThen(String::valueOf);
backToString.apply("123"); // "123"
System.out.println(backToString.apply("123"));
}
@Test
public void testSupplier(){
Supplier<User> personSupplier = User::new;
personSupplier.get().setUsername("xll"); // new Person
System.out.println(personSupplier.get());
}
@Test
public void testComparator (){
Comparator<User> comparator = (p1, p2) -> p1.getUsername().compareTo(p2.getUsername());
User p1 = new User(1L,"John", "Doe");
User p2 = new User(2L,"Alice", "Wonderland");
comparator.compare(p1, p2); // > 0
System.out.println(comparator.compare(p1, p2));
comparator.reversed().compare(p1, p2); // < 0
System.out.println(comparator.reversed().compare(p1, p2));
}
@Test
public void testOptional (){
Optional<String> optional = Optional.of("bam");
optional.isPresent(); // true
optional.get(); // "bam"
optional.orElse("fallback"); // "bam"
optional.ifPresent((s) -> System.out.println(s.charAt(0))); // "b"
}
/**
* 重点:流
*/
@Test
public void testStream (){
List<String> stringCollection = new ArrayList<>();
stringCollection.add("ddd2");
stringCollection.add("aaa2");
stringCollection.add("bbb1");
stringCollection.add("aaa1");
stringCollection.add("bbb3");
stringCollection.add("ccc");
stringCollection.add("bbb2");
stringCollection.add("ddd1");
//1.Filter 过滤
stringCollection.stream().filter((s) -> s.startsWith("a")).forEach(System.out::println);
System.out.println("----------------------------------");
//2.Sort 排序
stringCollection.stream().sorted().filter((s) -> s.startsWith("a")).forEach(System.out::println);
System.out.println("----------------------------------");
//3.Map 映射
stringCollection.stream().map(String::toUpperCase).sorted((a, b) -> b.compareTo(a)).forEach(System.out::println);
//4.Match 匹配
boolean anyStartsWithA = stringCollection.stream().anyMatch((s) -> s.startsWith("a"));
System.out.println(anyStartsWithA); // true
boolean allStartsWithA = stringCollection.stream().allMatch((s) -> s.startsWith("a"));
System.out.println(allStartsWithA); // false
boolean noneStartsWithZ = stringCollection.stream().noneMatch((s) -> s.startsWith("z"));
System.out.println(noneStartsWithZ); // true
//5.limit 线段流,使其不超过指定数量
stringCollection.stream().sorted().limit(2).forEach(s-> System.out.println(s));//"aaa1","aaa2"
}
}
标签:一个 top ddd java and class length framework uppercase
原文地址:https://www.cnblogs.com/x-ll123/p/11822356.html