共计 2156 个字符,预计需要花费 6 分钟才能阅读完成。
就记录在这吧,尽管这个名字叫 JAVA8 新办法,然而实际上 JAVA8 一点也不新啦,次要是咱们老了
list 的 removeIf 办法
List<String> list1 = Arrays.asList("one","two","three","four","five","six","seven");
List<String> list2 = new ArrayList<>(list1);
// 删除
boolean result = list2.removeIf(s -> s.length() > 4);
System.out.println(result);
System.out.println(list2.stream().collect(Collectors.joining(",")));
// 输入
true
one,two,four,five,six
// 替换
list2.replaceAll(s->s.toUpperCase());
System.out.println(list2.stream().collect(Collectors.joining(",")));
// 输入
ONE,TWO,FOUR,FIVE,SIX
// 排序
list2.sort(Comparator.naturalOrder());
System.out.println(list2.stream().collect(Collectors.joining(",")));
// 输入
FIVE,FOUR,ONE,SIX,TWO
比拟
Comparator<Person> comparator = Comparator.comparing(Person::getName).thenComparing(Person::getAge);
// 反向比拟
Comparator<Person> comparatorReversed = comparator.reversed();
// 默认字母程序
Comparator<String> comparator1 = Comparator.naturalOrder();
// 先按空后再依照字母程序
Comparator<String> comparator2 = Comparator.nullsFirst(Comparator.naturalOrder());
// 先依照字母程序,最初排空
Comparator<String> comparator3 = Comparator.nullsLast(Comparator.naturalOrder());
// 比拟
Long max = Long.max(1L,2L);
BinaryOperator<Long> sum = (s1,s2) -> s1 + s2;
sum = Long::sum;
hashCode
Long l = 2234324234234324L;
int hash = l.hashCode();
System.out.println(hash);
hash = Long.hashCode(l);
System.out.println(hash);
// 输入
642554319
642554319
Map forEach,getOrDefault,putIfAbsent
Map<String,Object> map =new HashMap<>();
map.put("key","value");
map.forEach((key,value)-> System.out.println(key+" "+value));
// 输入
key value
//getOrDefault
Person deafulPerson = new Person();
deafulPerson.setName("Test");
Person p = (Person) map.getOrDefault("p",deafulPerson);
System.out.println(p.getName());
// 输入
Test
Person p1 = new Person();
p1.setName("Test11");
//putIfAbsent
map.putIfAbsent("p", p);
System.out.println("putIfAbsent=="+map.get("p"));
// 替换 key 为 p 的值为 p1
map.replace("p", p1);
System.out.println("replace=="+map.get("p"));
// 替换 key 值为 p 的为 defaultPeron
map.replace("p", p1,deafulPerson);
System.out.println("replaceNew=="+map.get("p"));
//lambda 替换
map.replaceAll((key,oldPerson) -> p1);
System.out.println("replaceAll=="+map.get("p"));
// 输入
putIfAbsent==Person{name='Test', age=0}
replace==Person{name='Test11', age=0}
replaceNew==Person{name='Test', age=0}
replaceAll==Person{name='Test11', age=0}
正文完