Java8利⽤stream进⾏集合操作
下⾯例⼦中包括利⽤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;
< = 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().upingBy(Fruit::getId));
System.out.println("groupBy:" + groupBy);
}
/**
* List转Map
* @author hujy
* @date 2019-07-18 15:04
* @param fruitList
* @return void
*/
java streamprivate static void listToMap(List<Fruit> fruitList) {
/
/ 注:如果集合对象有重复的key,会报Duplicate key错误,可以⽤ (k1, k2) -> k1 来设置,如果有重复的key,则保留key1,舍弃key2
Map<Integer, Fruit> fruitMap =
fruitList.stream().Map(Fruit::getId, a -> a, (k1, k2) -> k1));
System.out.println("toMap:" + fruitMap);
}
/**
* 按条件过滤
* @author hujy
* @date 2019-07-18 15:07
* @param fruitList
* @return void
* @return void
*/
private static void filter(List<Fruit> fruitList) {
List<Fruit> filterList =
fruitList.stream().filter(a -> a.getName().equals("⾹蕉")).List());
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(
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(Comparatorparing(Fruit::getId)).List());
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(Comparatorparing(Fruit::getId).reversed()).List());
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(Comparatorparing(Fruit::getId).reversed()
.thenComparing(Fruit::getNum).reversed()).List());
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:" + ());
}
/**
* 求最⼩值
* @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:" + ());
}
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, nu
// 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=
// 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),
// 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),
// 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=
// 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;
}).List());

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。