最近在工作中使用了java8的新特性感觉比较好用总结了一些常用的api主要对集合的操作 下面开始举例
public class TestJava8 {
public static void main(String[] args) {
//实现Runnable接口
//new Thread(() -> System.out.println("test Thread ")).start();
//迭代遍历
List<Long> idList = new ArrayList<>();
idList.add(1L);
idList.add(2L);
idList.add(3L);
idList.forEach(x -> {
System.out.println(x);
});
/**
* 和传统的for循环比 for循环适合对list集合进行操作,forEach则适合对链表进行操作,原因
* ArrayList:ArrayList是采用数组的形式保存对象的,这种方式将对象放在连续的内存块中,所以插入和删除时比较麻烦,查询比较方便。
* LinkList:LinkList是将对象放在独立的空间中,而且每个空间中还保存下一个空间的索引,也就是数据结构中的链表结构,插入和删除比较方便,但是查找很麻烦,要从第一个开始遍历
*/
//strarm API
/**
* 多个中间操作可以连接起来形成一个流水线,除非流水线上触发终止操作,否则中间操作不会执行任何的处理!而在终止操作时一次性全部处理,称为“惰性求值"
* 即为链式预发调用
*/
//filter
//初始化数据
List<Person> personList = new ArrayList<>();
Person personOne = new Person();
personOne.setId(1L);
personOne.setName("张三");
personOne.setAge(20);
Person personTwo = new Person();
personTwo.setId(2L);
personTwo.setName("李四");
personTwo.setAge(21);
Person personThree = new Person();
personThree.setId(3L);
personThree.setName("王五");
personThree.setAge(22);
personList.add(personOne);
personList.add(personTwo);
personList.add(personThree);
/*
//过滤年龄大于20的
personList = personList.stream().filter(x -> x.getAge() > 20).collect(Collectors.toList());
//过滤名字不等于张三
personList = personList.stream().filter(x -> !Objects.equals(x.getName(), "张三")).collect(Collectors.toList());
//过滤名字不等于张三并且跳过第二个
personList = personList.stream().filter(x -> !Objects.equals(x.getName(), "张三")).skip(1).collect(Collectors.toList());
//去重(需要重写hashCode,equals)
personList = personList.stream().distinct().collect(Collectors.toList());
*/
//自然排序
List<String> list = Arrays.asList("bb", "c", "aa", "ee", "ddd");
list.stream()
.sorted()
.forEach(System.out::println);
//可比较的排序
personList.stream()
.sorted((p1, p2) -> {
if (p1.getAge() == p2.getAge()) {
return p1.getName().compareTo(p2.getName());
} else {
return p1.getAge() - p2.getAge();
}
});
//查找与匹配
boolean b = personList.stream()
.allMatch((e) -> e.getId().equals(1L));
System.out.println(b);
boolean b2 = personList.stream()
.anyMatch((e) -> e.getId().equals(2L));
System.out.println(b2);
boolean b3 = personList.stream()
.noneMatch((e) -> e.getId().equals(3L));
System.out.println(b3);
Optional<Person> op = personList.stream()
.sorted((e1, e2) -> Integer.compare(e1.getAge(), e2.getAge()))
.findFirst();
System.out.println(op.get());
Optional<Person> optional = personList.stream()
.filter((e) -> e.getId().equals(2L))
.findAny();
System.out.println(optional.get());
//数量 最大 最小值
long count = personList.stream()
.count();
System.out.println(count);
Optional<Person> opt = personList.stream()
.max((e1, e2) -> Long.compare(e1.getId(), e2.getId()));
System.out.println(optional.get());
Optional<Integer> optl = personList.stream()
.map(Person::getAge)
.min(Integer::compare);
System.out.println(op.get());
//集合收集器
List<String> arrlist = personList.stream()
.map(Person::getName)
.collect(Collectors.toList());
Set<String> set = personList.stream()
.map(Person::getName)
.collect(Collectors.toSet());
HashSet<String> hashSet = personList.stream()
.map(Person::getName)
.collect(Collectors.toCollection(HashSet::new));
//分组 十分常用
Map<Integer, List<Person>> map = personList.stream()
.collect(Collectors.groupingBy(Person::getAge));//根据年龄进行分组
//树形结构分组 ->先根据id进行分组 然后根据年龄进行分组
Map<Long, Map<Integer, List<Person>>> mapP = personList.stream()
.collect(Collectors.groupingBy(Person::getId, Collectors.groupingBy(Person::getAge)));
}
}