java8推出已经好久了,然而其新特性却几乎没有怎么用过。缘由是既然已有的只是知足需求何须去学习新的?然而,随着敲代码愈来愈多,业务上有不少臃肿繁琐的判断校验之类的代码,使得代码很长并且可读性不好。因而,从新捡起了java8。java
[TOC]编程
这里是实际代码中使用过的简单套式作法,随时更新。app
将知足需求的list元素筛选出来。
filter
返回一个boolean值,true的时候经过并放入一个stream里。ide
接收一个参数list,须要根据元素的某些条件来判断是否知足需求,将知足需求或者不知足需求的元素拿出来。这个正常代码也简单,遍历list,if判断,add赋值到结果集。然而,这个串代码仅仅是一个方法的前置校验部分,这会使得这个方法臃肿难以阅读。虽然重构提取函数能够提升可读性,但分散的代码管理也是麻烦。因此,java8的流式函数编程就适合:函数
List<TipUI> badList = tips.stream().filter(tipUI -> StringUtils.isBlank(tipUI.getGaiaId())).collect(Collectors.toList());
操做list元素中的部分属性构建新的list
map
用来转换元素学习
有个users列表,我想要或者这些user的id列表。code
@Test public void testMap(){ List<User> users = Arrays.asList( new User(1,"Ryan"), new User(2, "Leslie"), new User(3, "Test") ); //collect user ids Stream<Integer> integerStream = users.stream().map(User::getId); List<Integer> ids = integerStream.collect(Collectors.toList()); Assert.assertEquals("[1, 2, 3]",ids.toString()); //collect user names List<String> names = users.stream().map(user -> user.getName()).collect(Collectors.toList()); Assert.assertEquals("[Ryan, Leslie, Test]", names.toString()); }
仍旧使用
filter
和map
。此次演示构建过程。orm
将person列表转换为student列表:排序
Stream<Student> students = persons.stream() .filter(p -> p.getAge() > 18) .map(new Function<Person, Student>() { @Override public Student apply(Person person) { return new Student(person); } });
简化map的function为lambda:ip
Stream<Student> map = persons.stream() .filter(p -> p.getAge() > 18) .map(person -> new Student(person));
由于map的用法中参数的使用只经过传递,那么(前提是student有参数为person的构造器):
Stream<Student> map = persons.stream() .filter(p -> p.getAge() > 18) .map(Student::new);
收集为list:
List<Student> students = persons.stream() .parallel() .filter(p -> p.getAge() > 18) // filtering will be performed concurrently .sequential() .map(Student::new) .collect(Collectors.toCollection(ArrayList::new));
对list进行排序
好比从大到小排序:
@Test public void testListSortDesc(){ Integer a = 12,b = 23; Assert.assertEquals(-1, a.compareTo(b)); List<Integer> list = Arrays.asList(null,a,b,null); //large > small List<Integer> sorted = list.stream() .sorted(( m, n) ->{ if (m==null) return 1; if (n==null) return -1; return n.compareTo(m); }) .collect(Collectors.toList()); Assert.assertEquals("[23, 12, null, null]", sorted.toString()); }
map按照value排序,逆序:
@Test public void testHashMapSortByValueDesc(){ Map<String, Integer> unsortMap = new HashMap<>(); unsortMap.put("z", 10); unsortMap.put("b", 5); unsortMap.put("a", 6); unsortMap.put("c", 20); unsortMap.put("d", 1); unsortMap.put("e", 7); unsortMap.put("y", 8); unsortMap.put("n", 99); unsortMap.put("j", 50); unsortMap.put("m", 2); unsortMap.put("f", 9); LinkedHashMap<String, Integer> orderMap = unsortMap.entrySet().stream() .sorted(Map.Entry.comparingByValue(Collections.reverseOrder())) .collect(Collectors.toMap( Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new )); Assert.assertEquals("{n=99, j=50, c=20, z=10, f=9, y=8, e=7, a=6, b=5, m=2, d=1}", orderMap.toString()); }
//re-init brandScoreMap LinkedHashMap<String, Score> scoreMap = brandScoreMap.entrySet().stream() .collect(Collectors.toMap(p -> p.getKey(), p -> new Score(p.getValue(), caculateGrade(p.getValue())), (e1, e2) -> e1, LinkedHashMap::new));
此处留着等待系统学习的时候补上。