几个月前,突然喜欢上了这种写法,lambda表达式是 java8以后才流行起来的,好处的话,是写起来很方便,而且效率高,因为lambda设计的初衷就是砍掉冗余的代码和中间变量的生成,但是也有缺点,比如阅读比较头疼,尤其是复杂一点的代码,这里简单的把我经常使用的一些方法总结出来,做一个参考也还行
package cn.itcast.spring.stream;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class StreamDemo {
public static void main(String[] args) {
gen24();
}
/**
* Stream foreach表达式,通过数组生成
*/
public static void gen1() {
String[] strs = {"a", "b", "c", "d"};
Stream.of(strs).forEach(System.out::println);
}
/**
* 通过List生成
*/
public static void gen2() {
List<String> list = Arrays.asList("a", "b", "c", "d");
list.stream().forEach(System.out::println);
}
/**
* 生成数据串
*/
public static void gen3() {
Stream<Integer> generate = Stream.generate(() -> 1);
generate.limit(10).forEach(System.out::println);
}
/**
* 使用iterator生成序列串
*/
public static void gen4() {
Stream<Integer> iterate = Stream.iterate(1, x -> x + 1);
iterate.limit(10).forEach(System.out::println);
}
/**
* 其他方式
*/
public static void gen5() {
String chars = "abcdefg";
IntStream intStream = chars.chars();
intStream.forEach(System.out::println);
}
/**
* 过滤
*/
public static void gen6() {
Arrays.asList(1, 2, 3, 4, 5).stream().filter(item -> item % 2 == 0).forEach(System.out::println);
}
/**
* 求偶数和
*/
public static void gen7() {
int sum = Arrays.asList(1, 2, 3, 4, 5).stream().filter(item -> item % 2 == 0).mapToInt(x -> x).sum();
System.out.println(sum);
}
/**
* 求最大值
*/
public static void gen8() {
Optional<Integer> max = Arrays.asList(1, 2, 3, 4, 5).stream().max(Comparator.comparingInt(x -> x));
System.out.println(max.get());
}
/**
* 最小值
*/
public static void gen9() {
Optional<Integer> min = Arrays.asList(1, 2, 3, 4, 5).stream().min(Comparator.comparing(x -> x));
System.out.println(min.get());
}
/**
* 随机获取
*/
public static void gen10() {
Optional<Integer> any = Arrays.asList(1, 2, 3, 4, 5, 6).stream().filter(item -> item % 2 == 0).findAny();
System.out.println(any.get());
}
/**
* 获取第一个
*/
public static void gen11() {
Optional<Integer> first = Arrays.asList(1, 2, 3, 4, 5, 6).stream().filter(item -> item % 2 == 0).findFirst();
System.out.println(first.get());
}
/**
* 排序
*/
public static void gen12() {
Optional<Integer> first = Arrays.asList(1, 2, 3, 4, 5, 6).stream().sorted((a, b) -> b - a).findFirst();
System.out.println(first.get());
}
/**
* 按照长度进行排序
*/
public static void gen13() {
Arrays.asList("C#", "java", "php").stream().sorted(Comparator.comparingInt(String::length)).forEach(System.out::println);
}
/**
* 过滤返回一个集合
*/
public static void gen14() {
List<Integer> collect = Arrays.asList(1, 2, 3, 4, 5, 6).stream().filter(x -> x % 2 == 0).collect(Collectors.toList());
collect.stream().forEach(System.out::println);
}
/**
* 去重
*/
public static void gen15() {
List<Integer> collect = Arrays.asList(1, 2, 3, 4, 5, 6, 2).stream().distinct().collect(Collectors.toList());
collect.stream().forEach(System.out::println);
}
/**
* 去重转set
*/
public static void gen16() {
Arrays.asList(1, 2, 3, 4, 5, 6, 2).stream().collect(Collectors.toSet()).forEach(System.out::println);
}
/**
* 打印20-30的集合数据
*/
public static void gen17() {
List<Integer> collect = Stream.iterate(1, x -> x + 1).limit(50).skip(20).limit(10).collect(Collectors.toList());
collect.stream().forEach(System.out::println);
}
/**
* 字符串求和
*/
public static void gen18() {
String arr = "11,22,33,44,55";
int sum = Stream.of(arr.split(",")).mapToInt(Integer::parseInt).sum();
System.out.println(sum);
}
/**
* 自定义类型操作
*/
public static void gen19() {
String arr = "C#,php,python,java";
List<Person> collect = Stream.of(arr.split(",")).map(item -> new Person(item, "m")).collect(Collectors.toList());
collect.stream().forEach(item -> System.out.println(item.getName()));
}
/**
* 打印+求和
*/
public static void gen20() {
String arr = "11,22,33,44";
int sum = Stream.of(arr.split(",")).peek(System.out::println).mapToInt(item -> Integer.parseInt(item)).sum();
System.out.println(sum);
}
/**
* 条件匹配
*/
public static void gen21() {
boolean b = Arrays.asList(1, 2, 3, 4, 5, 6, 2).stream().allMatch(x -> x >= 0);
System.out.println(b);
}
/**
* 分组
*/
public static void gen22() {
List<Person> personList = new ArrayList<>();
Person p1 = new Person("java", "m");
Person p2 = new Person("C#", "f");
Person p3 = new Person("php", "m");
Person p4 = new Person("python", "f");
personList.add(p1);
personList.add(p2);
personList.add(p3);
personList.add(p4);
Map<String, List<Person>> collectMap = personList.stream().collect(Collectors.groupingBy(item -> item.getSex()));
for (Map.Entry<String, List<Person>> entryPerson : collectMap.entrySet()) {
String key = entryPerson.getKey();
List<Person> entryPersonValue = entryPerson.getValue();
System.out.println(key + ":" + entryPersonValue.size());
}
}
/**
* 转map
*/
public static void gen23() {
List<Person> personList = new ArrayList<>();
Person p1 = new Person("java", "m");
Person p2 = new Person("C#", "f");
Person p3 = new Person("php", "m");
Person p4 = new Person("python", "f");
personList.add(p1);
personList.add(p2);
personList.add(p3);
personList.add(p4);
Map<String, String> collectMap = personList.stream().collect(Collectors.toMap(Person::getName, Person::getSex));
//重复简直如何处理
for (Map.Entry<String, String> entryPerson : collectMap.entrySet()) {
String name = entryPerson.getKey();
String sex = entryPerson.getValue();
System.out.println(name + ":" + sex);
}
System.out.println("------------------------");
Person p5 = new Person("python", "m");
personList.add(p5);
Map<String, String> collectDistrinctMap = personList.stream().collect(Collectors.toMap(Person::getName, Person::getSex, (oldValue, newValue) -> newValue));
for (Map.Entry<String, String> entryPerson : collectDistrinctMap.entrySet()) {
String name = entryPerson.getKey();
String sex = entryPerson.getValue();
System.out.println(name + ":" + sex);
}
}
/**
* BigDecimal求和
*/
public static void gen24() {
List<BigDecimal> dataList = new ArrayList<>();
BigDecimal v1 = BigDecimal.valueOf(20);
BigDecimal v2 = BigDecimal.valueOf(30);
BigDecimal v3 = BigDecimal.valueOf(40);
dataList.add(v1);
dataList.add(v2);
dataList.add(v3);
BigDecimal reduce = dataList.stream().reduce(BigDecimal.ZERO, BigDecimal::add);
System.out.println(reduce.doubleValue());
}
}
中间有一个Person变量
package cn.itcast.spring.stream;
public class Person {
private String name;
private String sex;
public Person(String name, String sex) {
this.name = name;
this.sex = sex;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
以上就是我经常使用的一些lambda操作了