Java8 利用stream进行集合操作

it2022-05-05  145

下面例子中包括利用Java8 stream方式对于对象集合的分组、list转map、条件过滤、去重、排序、求和、求最大最小值。 

import lombok.Data; import java.math.BigDecimal; import java.util.*; import java.util.stream.Collectors; /** * Java8 集合操作 * * @Author hujy * @Date Created in 2019-07-16 11:38 * @Version 1.0 */ @Data public class Fruit { private Integer id; private String name; private BigDecimal money; private Integer num; public Fruit(Integer id, String name, BigDecimal money, Integer num) { this.id = id; this.name = name; this.money = money; this.num = num; } /** * 分组 * @author hujy * @date 2019-07-18 15:03 * @param fruitList * @return void */ private static void groupBy(List<Fruit> fruitList) { Map<Integer, List<Fruit>> groupBy = fruitList.stream().collect(Collectors.groupingBy(Fruit::getId)); System.out.println("groupBy:" + groupBy); } /** * List转Map * @author hujy * @date 2019-07-18 15:04 * @param fruitList * @return void */ private static void listToMap(List<Fruit> fruitList) { // 注:如果集合对象有重复的key,会报Duplicate key错误,可以用 (k1, k2) -> k1 来设置,如果有重复的key,则保留key1,舍弃key2 Map<Integer, Fruit> fruitMap = fruitList.stream().collect(Collectors.toMap(Fruit::getId, a -> a, (k1, k2) -> k1)); System.out.println("toMap:" + fruitMap); } /** * 按条件过滤 * @author hujy * @date 2019-07-18 15:07 * @param fruitList * @return void */ private static void filter(List<Fruit> fruitList) { List<Fruit> filterList = fruitList.stream().filter(a -> a.getName().equals("香蕉")).collect(Collectors.toList()); System.out.println("filter:" + filterList); } /** * 去重 * @author hujy * @date 2019-07-18 15:08 * @param fruitList * @return void */ private static void toUnique(List<Fruit> fruitList) { // 根据id去重 List<Fruit> uniqueList = fruitList.stream().collect( Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getId()))), ArrayList::new)); System.out.println("unique:" + uniqueList); } /** * 升序 * @author hujy * @date 2019-07-18 15:09 * @param fruitList * @return void */ private static void sortAsc(List<Fruit> fruitList) { // 按id升序排序 List<Fruit> sortAsc= fruitList.stream().sorted(Comparator.comparing(Fruit::getId)).collect(Collectors.toList()); System.out.println("sortAsc:" + sortAsc); } /** * 降序 * @author hujy * @date 2019-07-18 15:09 * @param fruitList * @return void */ private static void sortDesc(List<Fruit> fruitList) { // 按id降序排序 List<Fruit> sortDesc = fruitList.stream().sorted(Comparator.comparing(Fruit::getId).reversed()).collect(Collectors.toList()); System.out.println("sortDesc:" + sortDesc); } /** * 先升序再降序 * @author hujy * @date 2019-07-18 15:09 * @param fruitList * @return void */ private static void sortAscDesc(List<Fruit> fruitList) { // 按id升序排序,id一样的按num降序 List<Fruit> sortAscDesc = fruitList.stream().sorted(Comparator.comparing(Fruit::getId).reversed() .thenComparing(Fruit::getNum).reversed()).collect(Collectors.toList()); System.out.println("sortAscDesc:" + sortAscDesc); } /** * 求和 * @author hujy * @date 2019-07-18 15:10 * @param fruitList * @return void */ private static void sum(List<Fruit> fruitList) { // 按money求和 BigDecimal totalMoney = fruitList.stream().map(Fruit::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add); System.out.println("totalMoney:" + totalMoney); } /** * 求最大值 * @author hujy * @date 2019-07-18 15:10 * @param fruitList * @return void */ private static void getMax(List<Fruit> fruitList) { // 求money最大值 Optional<BigDecimal> max = fruitList.stream().map(Fruit::getMoney).max(BigDecimal::compareTo); System.out.println("max:" + max.get()); } /** * 求最小值 * @author hujy * @date 2019-07-18 15:10 * @param fruitList * @return void */ private static void getMin(List<Fruit> fruitList) { // 求money最小值 Optional<BigDecimal> min = fruitList.stream().map(Fruit::getMoney).min(BigDecimal::compareTo); System.out.println("min:" + min.get()); } public static void main(String[] args) { Fruit fruit1 = new Fruit(1, "苹果1", new BigDecimal("2.25"), 10); Fruit fruit2 = new Fruit(1, "苹果2", new BigDecimal("1.85"), 20); Fruit fruit3 = new Fruit(2, "香蕉", new BigDecimal("1.89"), 30); Fruit fruit4 = new Fruit(3, "樱桃", new BigDecimal("8.99"), 40); List<Fruit> fruitList = Arrays.asList(fruit1, fruit2, fruit3, fruit4); // 1.分组 groupBy(fruitList); // groupBy:{1=[Fruit(id=1, name=苹果1, money=2.25, num=10), Fruit(id=1, name=苹果2, money=1.85, num=20)], 2=[Fruit(id=2, name=香蕉, money=1.89, num=30)], 3=[Fruit(id=3, name=樱桃, money=8.99, num=40)]} // 2.List转Map listToMap(fruitList); // toMap:{1=Fruit(id=1, name=苹果1, money=2.25, num=10), 2=Fruit(id=2, name=香蕉, money=1.89, num=30), 3=Fruit(id=3, name=樱桃, money=8.99, num=40)} // 3.按条件过滤 filter(fruitList); // filter:[Fruit(id=2, name=香蕉, money=1.89, num=30)] // 4.根据id去重 toUnique(fruitList); // unique:[Fruit(id=1, name=苹果1, money=2.25, num=10), Fruit(id=2, name=香蕉, money=1.89, num=30), Fruit(id=3, name=樱桃, money=8.99, num=40)] // 5.按id升序 sortAsc(fruitList); // sortAsc:[Fruit(id=1, name=苹果1, money=2.25, num=10), Fruit(id=1, name=苹果2, money=1.85, num=20), Fruit(id=2, name=香蕉, money=1.89, num=30), Fruit(id=3, name=樱桃, money=8.99, num=40)] // 6.按id降序 sortDesc(fruitList); // sortDesc:[Fruit(id=3, name=樱桃, money=8.99, num=40), Fruit(id=2, name=香蕉, money=1.89, num=30), Fruit(id=1, name=苹果1, money=2.25, num=10), Fruit(id=1, name=苹果2, money=1.85, num=20)] // 7.按id升序排序,id一样的按num降序 sortAscDesc(fruitList); // sortAscDesc:[Fruit(id=1, name=苹果2, money=1.85, num=20), Fruit(id=1, name=苹果1, money=2.25, num=10), Fruit(id=2, name=香蕉, money=1.89, num=30), Fruit(id=3, name=樱桃, money=8.99, num=40)] // 8.money求和 sum(fruitList); // totalMoney:14.98 // 9.求money最大值 getMax(fruitList); // max:8.99 // 10.求money最小值 getMin(fruitList); // min:1.85 } }

将一个List的属性赋值给另一个List

List<OrderOpHis> records = orderIds.stream().map(orderId -> { OrderOpHis record = new OrderOpHis(); record.setOrderId(orderId); record.setOperatorName("system"); return record; }).collect(Collectors.toList());

 


最新回复(0)