关于stream使用的好文推荐:
这里去看了ibm的一篇关于stream的文章,get到了不少stream还没遇到过的用法。老铁们可以去学习一下。[https://www.ibm.com/developerworks/cn/java/j-lo-java8streamapi/ ]
lambda优雅取出对象list中某个属性重复的集合数据
public class Test { // https://blog.csdn.net/qq_35902833/article/details/88351470
@Data
@AllArgsConstructor
static class Dog {
String name;
int age;
}
public static List<Dog> dogs = null;
static {
dogs = new ArrayList<Dog>() {
{
add(new Dog("黄一", 11));
add(new Dog("黄一", 22));
add(new Dog("黄三", 33));
}
};
}
// @SuppressWarnings("AlibabaAvoidManuallyCreateThread")
public static void main(String[] args) {
// dogs.stream()
// .flatMap(i->i.getSonList().stream()) // lambda: list1.addAll(list2)
// .collect(Collectors.toSet());
Map<String, Long> collect = dogs.stream().
collect(Collectors.groupingBy(i -> i.getName(), Collectors.counting()));
System.out.println("-1-->" + collect.toString());
Map<String, List<Dog>> collect1 = dogs.stream()
.collect(Collectors.groupingBy(Dog::getName));
System.out.println("-2-->" + collect1.toString());
IntSummaryStatistics summaryStatistics3 = dogs.stream().collect(Collectors.summarizingInt(Dog::getAge));
System.out.println("-3-->" + summaryStatistics3.getSum());
Map<String, IntSummaryStatistics> intSummaryStatistics = dogs.stream().
collect(Collectors.groupingBy(i -> i.getName(), Collectors.summarizingInt(Dog::getAge)));
System.out.println("-4-->" + intSummaryStatistics);
System.out.println("-5-->" + intSummaryStatistics.get("黄一").getSum());
IntSummaryStatistics collect21 = dogs.stream().collect(Collectors.summarizingInt(Dog::getAge));
Optional.ofNullable(collect21).ifPresent(System.out::println);
System.out.println("-6-->" + Optional.ofNullable(collect21).get().getSum());
List<String> collect7 = dogs.stream().
collect(Collectors.groupingBy(i -> i.getName(), Collectors.counting()))
.entrySet()
.stream()
.map(entry -> {
return "key-->" + entry.getKey() + "; val-->" + entry.getValue();
})
.collect(Collectors.toList());
System.out.println("-7-->" + collect7);
List<String> collect8 = dogs.stream().
collect(Collectors.groupingBy(i -> i.getName(), Collectors.counting()))
.entrySet()
.stream()
.filter(entry -> entry.getValue() > 1)
.map(entry -> entry.getKey())
.collect(Collectors.toList());
System.out.println("-7-->" + collect8);
Consumer<Dog> consumer = (dog) -> System.out.println("-8-->" + dog.getAge());
Predicate<Dog> dogPredicate = (dog) -> dog.getAge() > 10;
Test.dogs.stream()
.filter(dogPredicate)
.limit(3)
.forEach(consumer);
//-1-->{黄三=1, 黄一=2}
//-2-->{黄三=[test.Dog(name=黄三, age=33)], 黄一=[test.Dog(name=黄一, age=11), test.Dog(name=黄一, age=22)]}
//-3-->66
//-4-->{黄三=IntSummaryStatistics{count=1, sum=33, min=33, average=33.000000, max=33}, 黄一=IntSummaryStatistics{count=2, sum=33, min=11, average=16.500000, max=22}}
//-5-->33
//IntSummaryStatistics{count=3, sum=66, min=11, average=22.000000, max=33}
//-6-->66
//-7-->[key-->黄三; val-->1, key-->黄一; val-->2]
//-7-->[黄一]
//-8-->11
//-8-->22
//-8-->33
}
}
//获取分享数Integer shareTatol = msgBrowseVos .stream() .filter(msgBrowse -> msgBrowse.getMsgType().equals(MsgBrowseConstants.msgType.SHARE)) .mapToInt(MsgBrowseVo::getCount) .max() .orElse(0);//-------对集合抽取属性求和--------------------------------------
@Builder
@Data
@EqualsAndHashCode(callSuper = false)
static class Student {
int id;
String name;
Integer unit;
BigDecimal price;
String groupId;
}
List<Student> list1 = Arrays.asList(
new Student(1, "t1", 2, BigDecimal.valueOf(3), "one1"),
new Student(2, "one", 2, BigDecimal.valueOf(4), "one2"));
int sum1 = list1.stream()
.mapToInt(p -> p.unit)
.sum();
System.out.println("--01->" + sum1);
BigDecimal amounts = list1.stream()
.map(item -> item.price)
.reduce(BigDecimal.ZERO, BigDecimal::add);
amounts = amounts.setScale(1, BigDecimal.ROUND_UP);
System.out.println("--02->" + amounts);
BigDecimal amounts21 = list1.stream()
.map(item -> item.price.multiply(BigDecimal.valueOf(item.unit)))
.reduce(BigDecimal.ZERO, BigDecimal::add);
amounts21 = amounts21.setScale(1, BigDecimal.ROUND_UP);
System.out.println("--021->" + amounts21);
BigDecimal sum2 = list1.stream()
.map(Student::getPrice)
.reduce(BigDecimal::add)
.get();
sum2 = sum2.setScale(1, BigDecimal.ROUND_UP);
System.out.println("--03->" + sum2);
BigDecimal sum21 = list1.stream()
.map(item -> item.price.multiply(BigDecimal.valueOf(item.unit)))
.reduce(BigDecimal::add)
.get();
sum21 = sum21.setScale(1, BigDecimal.ROUND_UP);
System.out.println("--031->" + sum21);
Integer totalSeller = list1.stream().map(Student::getUnit).reduce(0, Integer::sum);
System.out.println("--04->" + totalSeller);
BigDecimal totalSeller2 = list1.stream()
.map(item -> item.price.multiply(BigDecimal.valueOf(item.unit)))
.reduce(BigDecimal.ZERO, BigDecimal::add);
totalSeller2 = totalSeller2.setScale(1, BigDecimal.ROUND_UP);
System.out.println("--041->" + totalSeller2);
//★★★★ 注意此写法: .reduce((sum1, i) -> sum1 + i)
// 新方法:可以用内建方法,也可以自己定义
//Map和Reduce操作是函数式编程的核心操作,因为其功能,reduce 又被称为折叠操作。
//SQL中类似 sum()、avg() 或者 count() 的聚集函数,实际上就是 reduce 操作,因为它们接收多个值并返回一个值。
//流API定义的 reduceh() 函数可以接受lambda表达式,并对所有值进行合并。
// IntStream这样的类有类似 average()、count()、sum() 的内建方法来做 reduce 操作,也有mapToLong()、mapToDouble() 方法来做转换。
double bill = list1.stream()
.map((i) -> i + .12 * i)
.reduce((sum1, i) -> sum1 + i)
.get();
System.out.println("Total : " + bill);
// 为每个订单加上12%的税
// // 使用lambda表达式
// list1.stream().map((i) -> i + .12*i)
// .forEach(System.out::println);
public class Apple {
private Integer id;
private String name;
private BigDecimal money;
private Integer num;
public Apple(Integer id, String name, BigDecimal money, Integer num) {
this.id = id;
this.name = name;
this.money = money;
this.num = num;
}
}
List<Apple> appleList = new ArrayList<>();//存放apple对象集合
Apple apple1 = new Apple(1,"苹果1",new BigDecimal("3.25"),10);
Apple apple12 = new Apple(1,"苹果2",new BigDecimal("1.35"),20);
Apple apple2 = new Apple(2,"香蕉",new BigDecimal("2.89"),30);
Apple apple3 = new Apple(3,"荔枝",new BigDecimal("9.99"),40);
appleList.add(apple1);
appleList.add(apple12);
appleList.add(apple2);
appleList.add(apple3);
list.stream().mapToDouble(User::getHeight).sum()//和 list.stream().mapToDouble(User::getHeight).max()//最大 list.stream().mapToDouble(User::getHeight).min()//最小 list.stream().mapToDouble(User::getHeight).average()//平均值
// intStream、LongStream 和 DoubleStream 等流的类中,有个非常有用的方法叫做 summaryStatistics() 。可以返回 IntSummaryStatistics、LongSummaryStatistics 或者 DoubleSummaryStatistic s,描述流中元素的各种摘要数据。
//获取数字的个数、最小值、最大值、总和以及平均值 https://www.cnblogs.com/L-a-u-r-a/p/9077615.html
List<Integer> primes = Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19, 23, 29);
IntSummaryStatistics subject = primes.stream().mapToInt((x) -> x).summaryStatistics();
System.out.println("Highest prime number in List : " + subject.getMax());
System.out.println("Lowest prime number in List : " + subject.getMin());
System.out.println("Sum of all prime numbers : " + subject.getSum());
System.out.println("Average of all prime numbers : " + subject.getAverage());
最大值,最小值,平均值 // 为啥返回Optional? 如果stream为null怎么办, 这时候Optinal就很有意义了 Optional<Dish> mostCalorieDish = dishes.stream().max(Comparator.comparingInt(Dish::getCalories)); Optional<Dish> minCalorieDish = dishes.stream().min(Comparator.comparingInt(Dish::getCalories)); Double avgCalories = dishes.stream().collect(Collectors.averagingInt(Dish::getCalories)); IntSummaryStatistics summaryStatistics = dishes.stream().collect(Collectors.summarizingInt(Dish::getCalories)); double average = summaryStatistics.getAverage(); long count = summaryStatistics.getCount(); int max = summaryStatistics.getMax(); int min = summaryStatistics.getMin(); long sum = summaryStatistics.getSum();
// Collectors.maxBy 和 Collectors.minBy 来计算流中的最大或最小值。 Optional<Dish> maxDish = Dish.menu.stream().collect(Collectors.maxBy(Comparator.comparing(Dish::getCalories))); Optional<Dish> minDish = Dish.menu.stream().collect(Collectors.minBy(Comparator.comparing(Dish::getCalories)));
List转Map
//不用去重的情况:
Map<Integer, String> collect1 = appleList.stream().collect(Collectors.toMap(Apple::getId, Apple::getName));
System.out.println(collect1);//{1=苹果1, 2=香蕉, 3=荔枝}
Map<Integer, Apple> collect2 = appleList.stream().collect(Collectors.toMap(Apple::getId, appleList -> appleList));
System.out.println(collect2);
//{1=Apple(id=1, name=苹果1, money=3.25, num=10), 2=Apple(id=2, name=香蕉, money=2.89, num=30), 3=Apple(id=3, name=荔枝, money=9.99, num=40)}
// account -> account是一个返回本身的lambda表达式,其实还可以使用Function接口中的一个默认方法代替,使整个方法更简洁优雅:
Map<Integer, Apple> collect3 = appleList.stream().collect(Collectors.toMap(Apple::getId, Function.identity()));
System.out.println(collect3 );
//{1=Apple(id=1, name=苹果1, money=3.25, num=10), 2=Apple(id=2, name=香蕉, money=2.89, num=30), 3=Apple(id=3, name=荔枝, money=9.99, num=40)}
1. List转Map
id为key,apple对象为value,可以这么做:
/**
* List -> Map
* 需要注意的是:
* toMap 如果集合对象有重复的key,会报错Duplicate key ....
* apple1,apple12的id都为1。
* 可以用 (k1,k2)->k1 来设置,如果有重复的key,则保留key1,舍弃key2
*/
Map<Integer, Apple> appleMap = appleList.stream().collect(Collectors.toMap(Apple::getId, a -> a,(k1,k2)->k1));
打印appleMap:
{1=Apple{id=1, name='苹果1', money=3.25, num=10}, 2=Apple{id=2, name='香蕉', money=2.89, num=30}, 3=Apple{id=3, name='荔枝', money=9.99, num=40}}
https://zacard.net/2016/03/17/java8-list-to-map/
重复key的情况
代码如下:
public Map<String, Account> getNameAccountMap(List<Account> accounts) {
return accounts.stream().collect(Collectors.toMap(Account::getUsername, Function.identity()));
}
这个方法可能报错(java.lang.IllegalStateException: Duplicate key),因为name是有可能重复的。toMap有个重载方法,可以传入一个合并的函数来解决key冲突问题:
public Map<String, Account> getNameAccountMap(List<Account> accounts) {
return accounts.stream().collect(Collectors.toMap(Account::getUsername, Function.identity(), (key1, key2) -> key2));
}
这里只是简单的使用后者覆盖前者来解决key重复问题。
指定具体收集的map
toMap还有另一个重载方法,可以指定一个Map的具体实现,来收集数据:
public Map<String, Account> getNameAccountMap(List<Account> accounts) {
return accounts.stream().collect(Collectors.toMap(Account::getUsername, Function.identity(), (key1, key2) -> key2, LinkedHashMap::new));
}
2. 分组
List里面的对象元素,以某个属性来分组,例如,以id分组,将id相同的放在一起:
//List 以ID分组 Map<Integer,List<Apple>>
Map<Integer, List<Apple>> groupBy = appleList.stream().collect(Collectors.groupingBy(Apple::getId));
System.err.println("groupBy:"+groupBy);
{1=[Apple{id=1, name='苹果1', money=3.25, num=10}, Apple{id=1, name='苹果2', money=1.35, num=20}], 2=[Apple{id=2, name='香蕉', money=2.89, num=30}], 3=[Apple{id=3, name='荔枝', money=9.99, num=40}]}
3. 过滤filter: 从集合中过滤出来符合条件的元素(map只是覆盖属性,filter根据判断属性来collect宿主bean):
//过滤出符合条件的数据
List<Apple> filterList = appleList.stream().filter(a -> a.getName().equals("香蕉")).collect(Collectors.toList());
System.err.println("filterList:"+filterList);
[Apple{id=2, name='香蕉', money=2.89, num=30}]
4. 求和: 将集合中的数据按照某个属性求和:
BigDecimal:
//计算 总金额
BigDecimal totalMoney = appleList.stream().map(Apple::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
System.err.println("totalMoney:"+totalMoney); //totalMoney:17.48
Integer:
//计算 数量
int sum = appleList.stream().mapToInt(Apple::getNum).sum();
System.err.println("sum:"+sum); //sum:100
List<Integer> cc = new ArrayList<>();
cc.add(1);cc.add(2);cc.add(3);
int sum = cc.stream().mapToInt(Integer::intValue).sum();//6
★5 Collectors.groupingBy进行分组、排序等操作:
import javaX.Model.Student;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public class FunctionX {
public static void main(String[] args) {
//1.分组计数
List<Student> list1= Arrays.asList(new Student(1,"one","zhao"),new Student(2,"one","qian"),new Student(3,"two","sun"));
//1.1根据某个属性分组计数
Map<String,Long> result1=list1.stream().collect(Collectors.groupingBy(Student::getGroupId,Collectors.counting()));
System.out.println(result1);
//1.2根据整个实体对象分组计数,当其为String时常使用
Map<Student,Long> result2=list1.stream().collect(Collectors.groupingBy(Function.identity(),Collectors.counting()));
System.out.println(result2);
//1.3根据分组的key值对结果进行排序、放进另一个map中并输出
Map<String,Long> xMap=new HashMap<>();
result1.entrySet().stream().sorted(Map.Entry.<String,Long>comparingByKey().reversed()) //reversed不生效
.forEachOrdered(x->xMap.put(x.getKey(),x.getValue()));
System.out.println(xMap);
//2.分组,并统计其中一个属性值得sum或者avg:id总和
Map<String,Integer> result3=list1.stream().collect(
Collectors.groupingBy(Student::getGroupId,Collectors.summingInt(Student::getId))
);
System.out.println(result3);
}
}
//去重 https://blog.csdn.net/lu930124/article/details/77595585/
import static java.util.Comparator.comparingLong;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;
// 根据id去重
List<Person> unique = appleList.stream().collect(
collectingAndThen(
toCollection(() -> new TreeSet<>(comparingLong(Apple::getId))), ArrayList::new)
https://www.cnblogs.com/yangweiqiang/p/6934671.html
来源:https://www.cnblogs.com/hahajava/p/9204500.html