List<ProductApplyDetail> detailList = null;
1,取list对象属性
List<String> applyOrderNos = detailList.stream().map(v -> v.getApplyOrderNo()).collect(Collectors.toList());
List<ProductApplyOrder> orders = productApplyOrderMapper.selectByApplyOrderNos(applyOrderNos);
2,list转map
Map<String, ProductApplyOrder> orderMap = orders.stream().collect(Collectors.toMap(v -> v.getApplyOrderNo(), v -> v, (a, b) -> b));
3,循环赋值
detailList.stream().forEach(v -> v.setTitle(orderMap.containsKey(v.getApplyOrderNo()) ? orderMap.get(v.getApplyOrderNo()).getTitle() : ""));
2,list商品数据,根据商品编码分组,获取分组后第一个数据转map,源自导入商品基本信息
midBaseProductInfos=midBaseProductInfos
.stream()
.collect(
Collectors
.groupingBy(MidBaseProductInfo::getProductCode,LinkedHashMap::new,Collectors.toList())
).values().stream().map(list -> list.get(list.size() - 1)).collect(Collectors.toList());
本文列举了stream常用操作,可参考:https://www.cnblogs.com/jaycekon/p/7467026.html
主要方法:
filter:注意->操作函数返回一个boolean值
map
flatMap
代码
实体类
public class Dish {
private String name;
private boolean vegetarian;
private int calories;
private Type type;
public enum Type {MEAT, FISH, OTHER}
public Dish(String name, boolean vegetarian, int calories, Type type) {
this.name = name;
this.vegetarian = vegetarian;
this.calories = calories;
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isVegetarian() {
return vegetarian;
}
public void setVegetarian(boolean vegetarian) {
this.vegetarian = vegetarian;
}
public int getCalories() {
return calories;
}
public void setCalories(int calories) {
this.calories = calories;
}
public Type getType() {
return type;
}
public void setType(Type type) {
this.type = type;
}
@Override
public String toString() {
return "Dish{" +
"name='" + name + '\'' +
", vegetarian=" + vegetarian +
", calories=" + calories +
", type=" + type +
'}';
}
}
测试类
@RunWith(SpringRunner.class)
@SpringBootTest
@EnableAutoConfiguration
@Slf4j
public class StreamTest {
static List<Dish> list = Arrays.asList(
new Dish("pork", false, 800, Dish.Type.MEAT),
new Dish("beef", false, 700, Dish.Type.MEAT),
new Dish("chicken", false, 400, Dish.Type.MEAT),
new Dish("french fries", true, 530, Dish.Type.OTHER),
new Dish("rice", true, 350, Dish.Type.OTHER),
new Dish("season fruit", true, 120, Dish.Type.OTHER),
new Dish("pizza", true, 550, Dish.Type.OTHER),
new Dish("prawns", false, 300, Dish.Type.FISH),
new Dish("salmon", false, 450, Dish.Type.FISH));
public static void testForEach() {
list.stream().forEach(dish ->
dish.setName(dish.getName() + 1)
);
log.info("ForEach输出:数据:{},大小:{}", list.toArray(), list.size());
}
public static void testFilter() {
List<Dish> listfilter = list.stream().filter(dish -> dish.getCalories() > 600).collect(Collectors.toList());
log.info("Filter输出:数据:{},大小:{}", listfilter, listfilter.size());
}
public static void testFilterB() {
List<Dish> listfilterB = list.stream().filter(Dish::isVegetarian).limit(3).collect(Collectors.toList());
log.info("FilterB输出:数据:{},大小:{}", listfilterB, listfilterB.size());
}
public static List<String> testMap() {
List<String> listMap = list.stream().map(Dish::getName).collect(Collectors.toList());
log.info("Map输出:数据:{},大小:{}", listMap, list.size());
return listMap;
}
public static void testFlatMap() {
String[] words = new String[]{"shen", "ke"};
List<String> uniqueCharacters = Arrays.asList(words).stream()
.map(w -> w.split(""))
.flatMap(Arrays::stream)
.distinct()
.collect(Collectors.toList());
log.info("testFlatMap:数据:{},大小:{}", uniqueCharacters, uniqueCharacters.size());
}
public static void main(String[] args) {
testForEach();
testFilter();
testMap();
testFilterB();
testFlatMap();
}
}