最近写代码被同事嘲讽,只会写if else,for 循环,现在来了解下jdk8新特性lambda和stream的使用。
lambda表达式
语法:
(parameters) -> expression
或
(parameters) ->{ statements; }
parameters是参数,expression是表达式,statements是代码块。
比如 x->x+5,表示接收参数x,返回x+5,(x, y) -> x + y ,表示接收参数x和y,返回x+y的和。
:: 双冒号用法
通过 :: 来使用类的方法。
语法:
类名::方法名
比如 Integer::valueOf,就相当于Integer.valueOf() ,person -> person.getName() ,可写成 Person::getName
forEach() 方法迭代集合中的数据
简单例子:
- 遍历List:
List<String> list=Arrays.asList("abc","def","","123"); list.forEach(System.out::println);
- 遍历Map:
List<Worker> list = new ArrayList<>();list.add(new Worker("123",18,"lin"));list.add(new Worker("456",28,"chen"));list.add(new Worker("789",38,"wu"));Map<String, Integer> map = new HashMap<>();//forEach()内的方法参数为work,执行的操作是将Worker对象的id作为key,age作为valuelist.forEach(worker -> map.put(worker.getId(),worker.getAge()));//遍历map,打印key和value// map.forEach((key,value)->System.out.println(key+","+value));map.forEach((key,value)-> {System.out.println(key+","+value);if ("456".equals(key)) {System.out.println("TargetValue:"+value);}});
stream流
它是java对集合操作的优化,相较于迭代器,使用Stream的速度非常快,并且它支持并行方式处理集合中的数据,默认情况能充分利用cpu的资源。同时支持函数式编程,代码非常简洁。
一个java 8的stream是由三部分组成的。数据源,零个或一个或多个中间操作,一个或零个终止操作。
中间操作是对数据的加工,注意,中间操作是lazy操作,并不会立马启动,需要等待终止操作才会执行。
终止操作是stream的启动操作,只有加上终止操作,stream才会真正的开始执行。
流方法 |
含义 |
示例 |
filter |
(中间操作)该操作会接受一个谓词(一个返回boolean的函数)作为参数,并返回一个包括所有符合谓词的元素的流。 |
List<Dish> vegetarianMenu = menu.stream() .filter(Dish::isVegetarian) .collect(toList()); System.out.println(vegetarianMenu); |
distinct |
(中间操作)返回一个元素各异(根据流所生成元素的hashCode和equals方法实现)的流。 |
List<Integer> numbers = Arrays.asList(1, 2, 1, 3, 3, 2, 4); numbers.stream() .filter(i -> i % 2 == 0) .distinct() .forEach(System.out::println); |
limit |
(中间操作)会返回一个不超过给定长度的流。 |
List<Dish> dishes = menu.stream() .filter(d -> d.getCalories() > 300) .limit(3) .collect(toList()); System.out.println(dishes); |
skip |
(中间操作)返回一个扔掉了前n个元素的流。 |
List<Dish> dishes = menu.stream() .filter(d -> d.getCalories() > 300) .skip(2) .collect(toList()); System.out.println(dishes); |
map |
(中间操作)接受一个函数作为参数。这个函数会被应用到每个元素上,并将其映射成一个新的元素(使用映射一词,是因为它和转换类似,但其中的细微差别在于它是“创建一个新版本”而不是去“修改”)。 |
list<Dish> menu =List.of(DishObject1,DishObject2); List<String> dishNames = menu.stream() .map(Dish::getName) .collect(toList()); System.out.println(dishNames); |
flatMap |
(中间操作)使用flatMap方法的效果是,各个数组并不是分别映射成一个流,而是映射成流的内容。所有使用map(Arrays::stream)时生成的单个流都被合并起来,即扁平化为一个流。 |
List<String> words = Arrays.asList("Goodbye", "World"); List<String> uniqueCharacters = words.stream() .map(w -> w.split("")) .flatMap(Arrays::stream) .distinct() .collect(Collectors.toList()); System.out.println(uniqueCharacters); |
sorted |
(中间操作)返回排序后的流 |
List<String> traderStr = menu.stream() .map(transaction -> transaction.getName()) .sorted() .collect(toList()); System.out.println(traderStr); |
anyMatch |
(终端操作)可以回答“流中是否有一个元素能匹配给定的谓词”。 |
if (menu.stream().anyMatch(Dish::isVegetarian)) { System.out.println("The menu is (somewhat) vegetarian friendly!!"); } |
allMatch |
(终端操作)会看看流中的元素是否都能匹配给定的谓词。 |
boolean isHealthy = menu.stream() .allMatch(d -> d.getCalories() < 1000); System.out.println(isHealthy); |
noneMatch |
(终端操作)可以确保流中没有任何元素与给定的谓词匹配。 |
boolean isHealthy = menu.stream() .noneMatch(d -> d.getCalories() >= 1000); System.out.println(isHealthy); |
findAny |
(终端操作)将返回当前流中的任意元素。 |
Optional<Dish> dish = menu.stream() .filter(Dish::isVegetarian) .findAny(); System.out.println(dish); |
findFirst |
(终端操作)有些流有一个出现顺序(encounter order)来指定流中项目出现的逻辑顺序(比如由List或排序好的数据列生成的流)。对于这种流,可能想要找到第一个元素。 |
List<Integer> someNumbers = Arrays.asList(1, 2, 3, 4, 5); Optional<Integer> firstSquareDivisibleByThree = someNumbers.stream() .map(x -> x * x) .filter(x -> x % 3 == 0) .findFirst(); System.out.println(firstSquareDivisibleByThree); |
forEach |
(终端操作)遍历流 |
List<Integer> numbers = Arrays.asList(1, 2, 1, 3, 3, 2, 4); numbers.stream() .filter(i -> i % 2 == 0) .distinct() .forEach(System.out::println); |
collect |
(终端操作)收集器 |
List<String> traderStr = menu.stream() .map(transaction -> transaction.getName()) .sorted() .collect(toList()); |
reduce |
(终端操作)归约reduce接受两个参数: ?一个初始值,这里是0; ?一个BinaryOperator<T>来将两个元素结合起来产生一个新值,这里我们用的是lambda (a, b) -> a + b。 |
List<Integer> numbers = Arrays.asList(1, 2, 1, 3, 3, 2, 4); int sum = numbers.stream().reduce(0, (a, b) -> a + b); System.out.println(sum); |
count |
(终端操作)返回此流中元素的计数。 |
long evenNumbers = IntStream.rangeClosed(1, 100) .filter(n -> n % 2 == 0) .count(); System.out.println(evenNumbers); |
实例:
- map() 方法用于映射每个元素到对应的结果。
/*** 映射*/
public void mapDemo(){List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);// 获取对应的平方数。distinct()用于去重。List<Integer> squaresList = numbers.stream().map( i -> i*i).distinct().collect(Collectors.toList());squaresList.forEach(System.out::println);
}
//list转map
Map<String,Integer> map = personList.stream().collect(Collectors.toMap(Person::getName,Person::getAge));
//在Collectors.toMap()中,要注意:
//key不可重复,否则toMap()会报错"Duplicate key"。
//value不可为null,否则toMap()会报错"空指针异常"。
Map<String,Integer> map = personList.stream().collect(Collectors.toMap(Person::getName,Person::getAge,(key1,key2)->key2));//重复则key2替换key1的值
Map<String,Integer> map = personList.stream().collect(Collectors.toMap(Person::getName,Person::getAge,(key1,key2)->key2+key1));//重复则key2值加key1值
peek和map修改Stream的元素
map函数对Stream中元素执行的是映射操作,会以新的元素(map的结果)填充新的Stream,严格的讲map不是修改原来的元素。peek只能消费Stream中的元素,是否可以更该Stream中的元素,取决于Stream中的元素是否是不可变对象。如果是不可变对象,则不可修改Stream中的元素;如果是可变对象,则可以修改对象的值,但是无法修改对象的引用。显而易见,当我们只需要对元素内部处理,使用peek是比较合适的,如果我们需要返回一个自定义的Stream时候,需要使用map。
//不可改变
List<String> list = Stream.of("one", "two", "three", "four").filter(e -> e.length() > 3).peek(s -> {s = s + "-" + s;System.out.println(s);}).map(String::toUpperCase).peek(e -> System.out.println("Mapped value: " + e)).collect(Collectors.toList());
System.out.println(list);
//可变对象
Company apple = new Company("apple", 44);
Company huawei = new Company("huawei", 33);
Company qualcomm = new Company("Qualcomm ", 35);
List<Company> list = Stream.of(apple, huawei, qualcomm).filter(company -> company.getAge() < 35).peek(company -> company.setAge(company.getAge() - 10)).map(company -> new Company(company.getName().toUpperCase(),company.getAge())).peek(e -> System.out.println("Mapped value: " + e)).collect(Collectors.toList());
System.out.println(list);//输出[Person{name='HUAWEI', age=23}]
- mapToInt()、mapToLong()用于转换流的数据类型:
mapToInt()将映射转成int类型,转化后还可以接上max(),min(),sum()等函数。
public void mapToIntDemo() {List<String> numberList= Arrays.asList("3", "2","2","3");//将集合中的字符串转化数字,并进行加一操作,最后求和。int sum = numberList.stream().mapToInt(Integer::valueOf).map(x->x+1).sum();System.out.println(sum);
}
filter()
/*** 过滤*/
public void filterDemo(){List<String> list=Arrays.asList("abc","def","","123");//筛选出不为空的数据List<String> filterList = list.stream().filter(StringUtils::isNotEmpty).collect(Collectors.toList());filterList.forEach(System.out::println);//多条件List<Person> filterList = list.stream().filter(p-> (p!=null && p.getPersonName()!=null )).collect(Collectors.toList());
}
sorted()
List<Person> list = personList.stream().sorted(Comparator.comparingInt(Person::getAge)).collect(Collectors.toList());//倒叙
List<Person> list = personList.stream().sorted(Comparator.comparingInt(Person::getAge)..reverseOrder()).collect(Collectors.toList());
collect()
collect() 可以将流转化为集合以及字符串等其他形式
public void listStringtoInteger() {List<String> strList=new ArrayList<>();strList.add("1");strList.add("2");strList.add("abc");//将list转化为流,然后过滤掉非数字的字符串,再将字符串转化为数字,最后转化回list。List<Integer> integerList = strList.stream().filter(StringUtils::isNumeric).map(Integer::valueOf).collect(Collectors.toList());integerList.forEach(System.out::println);
}
/*** 将集合转化成符号分隔的字符串**/
public void collectJoiningDemo(){List<String> list=Arrays.asList("abc","def","","123");
// String contents=String.join(",",list);//表示通过 ,号分隔String contents = list.stream().filter(StringUtils::isNotEmpty).collect(Collectors.joining(","));System.out.println(contents);
}