flatMap()
List的处理: demo代码如下
@Data
public class SkuListProperty {
private List<SkuProperty> skuProperties;
}
@Data
class SkuProperty {
public SkuProperty(Long skuId, Long itemId) {
this.skuId = skuId;
this.itemId = itemId;
}
private Long skuId;
private Long itemId;
}
public class MapFun {
public static void main(String[] args) {
List<SkuProperty> inner1 = new ArrayList<>();
inner1.add(new SkuProperty(1L, 111L));
inner1.add(new SkuProperty(2L, 222L));
inner1.add(new SkuProperty(3L, 333L));
inner1.add(new SkuProperty(4L, 444L));
SkuListProperty skuList1 = new SkuListProperty();
skuList1.setSkuProperties(inner1);
List<SkuProperty> inner2 = new ArrayList<>();
inner2.add(new SkuProperty(1L, 1111L));
inner2.add(new SkuProperty(2L, 2222L));
inner2.add(new SkuProperty(3L, 3333L));
inner2.add(new SkuProperty(4L, 4444L));
SkuListProperty skuList2 = new SkuListProperty();
skuList2.setSkuProperties(inner2);
List<SkuListProperty> outer = new ArrayList<>();
outer.add(skuList1);
outer.add(skuList2);
System.out.println(JSON.toJSONString(outer));
Map<Long, List<SkuProperty>> collect = outer.stream()
.map(SkuListProperty::getSkuProperties) // List<SkuProperty>
.flatMap(Collection::stream) // Stream
.collect(Collectors.groupingBy(SkuProperty::getSkuId));
System.out.println(JSON.toJSONString(collect));
}
}
[{
"skuProperties": [{
"itemId": 111,
"skuId": 1
}, {
"itemId": 222,
"skuId": 2
}, {
"itemId": 333,
"skuId": 3
}, {
"itemId": 444,
"skuId": 4
}]
}, {
"skuProperties": [{
"itemId": 1111,
"skuId": 1
}, {
"itemId": 2222,
"skuId": 2
}, {
"itemId": 3333,
"skuId": 3
}, {
"itemId": 4444,
"skuId": 4
}]
}]
{
1: [{
"itemId": 111,
"skuId": 1
}, {
"itemId": 1111,
"skuId": 1
}],
2: [{
"itemId": 222,
"skuId": 2
}, {
"itemId": 2222,
"skuId": 2
}],
3: [{
"itemId": 333,
"skuId": 3
}, {
"itemId": 3333,
"skuId": 3
}],
4: [{
"itemId": 444,
"skuId": 4
}, {
"itemId": 4444,
"skuId": 4
}]
}
Collectors.toMap无重复key
public class MapFun {
public static void main(String[] args) {
List<Item> items = new ArrayList<>();
items.add(new Item(1L, 2));
items.add(new Item(3L, 4));
items.add(new Item(5L, 6));
System.out.println(JSON.toJSONString(items));
//[{"id":1,"productId":2},{"id":3,"productId":4},{"id":5,"productId":6}]
Map<Long, Integer> map = items.stream().collect(Collectors.toMap(Item::getId, Item::getProductId));
System.out.println(JSON.toJSONString(map));
//{1:2,3:4,5:6}
Map<Long, Integer> map2 = items.stream().collect(
HashMap::new, (m, item) -> m.put(item.getId(), item.getProductId()), HashMap::putAll
);
//{1:2,3:4,5:6}
}
@Data
@AllArgsConstructor
static class Item {
private Long id;
private Integer productId;
}
}
Collectors.toMap有重复key
public class MapFun {
public static void main(String[] args) {
List<Item> items = new ArrayList<>();
items.add(new Item(1L, 1));
items.add(new Item(3L, 3));
items.add(new Item(15L, 15));
items.add(new Item(8L, 8));
items.add(new Item(8L, 88));
System.out.println(JSON.toJSONString(items));
//[{"id":1,"productId":1},{"id":3,"productId":3},{"id":15,"productId":15},{"id":8,"productId":8},{"id":8,"productId":88}]
Map<Long, Integer> map3 = items.stream().collect(Collectors.toMap(Item::getId, Item::getProductId, (v1, v2) -> v1));
System.out.println(JSON.toJSONString(map3));
//{1:1,3:3,8:8,15:15}
}
@Data
@AllArgsConstructor
static class Item {
private Long id;
private Integer productId;
}
}
Collectors.toMap元素有序
public class MapFun {
public static void main(String[] args) {
List<Item> items = new ArrayList<>();
items.add(new Item("a", 1));
items.add(new Item("b", 3));
items.add(new Item("aa", 15));
items.add(new Item("d", 8));
Map<String, Integer> map3 = items.stream().collect(Collectors.toMap(Item::getId, Item::getProductId, (v1, v2) -> v1));
System.out.println(JSON.toJSONString(map3));
//{"aa":15,"a":1,"b":3,"d":8}
Map<String, Integer> map4 = items.stream().collect(Collectors.toMap(Item::getId, Item::getProductId, (v1, v2) -> v1, TreeMap::new));
System.out.println(JSON.toJSONString(map4));
//{"a":1,"aa":15,"b":3,"d":8}
}
@Data
@AllArgsConstructor
static class Item {
private String id;
private Integer productId;
}
}
Collectors.groupingBy Collectors.mapping
public class MapFun {
public static void main(String[] args) {
List<Product> products = new ArrayList<>();
products.add(new Product("苹果", 1, "红富士"));
products.add(new Product("苹果", 2, "国光"));
products.add(new Product("香蕉", 3, "海南牌"));
products.add(new Product("香蕉", 4, "河南牌"));
products.add(new Product("梨子", 15, "安徽"));
products.add(new Product("梨子", 16, "福建"));
Map<String, List<Integer>> collect = products.stream()
.collect(Collectors.groupingBy(Product::getName, Collectors.mapping(Product::getPrice, Collectors.toList())));
System.out.println(JSON.toJSONString(collect));
//{"香蕉":[3,4],"苹果":[1,2],"梨子":[15,16]}
}
@Data
@AllArgsConstructor
static class Product {
private String name;
private Integer price;
private String brand;
}
}
Collectors.groupingBy Collectors.toMap 多级分组
public class MapFun {
public static void main(String[] args) {
List<ProductPrice> products = new ArrayList<>();
products.add(new ProductPrice(1L, 510000, 3));
products.add(new ProductPrice(1L, 520000, 4));
products.add(new ProductPrice(2L, 610000, 4));
products.add(new ProductPrice(2L, 620000, 6));
products.add(new ProductPrice(3L, 710000, 7));
products.add(new ProductPrice(3L, 710000, 8));
Map<Long, Map<Integer, ProductPrice>> collect = products.stream().collect(
Collectors.groupingBy(
ProductPrice::getId, Collectors.toMap(ProductPrice::getAreaCode, v -> v, (v1, v2) -> v2)
)
);
System.out.println(JSON.toJSONString(collect));
}
@Data
@AllArgsConstructor
static class ProductPrice {
private Long id;
private Integer areaCode;
private Integer price;
}
}
{
1: {
520000: {
"areaCode": 520000,
"id": 1,
"price": 4
},
510000: {
"areaCode": 510000,
"id": 1,
"price": 3
}
},
2: {
620000: {
"areaCode": 620000,
"id": 2,
"price": 6
},
610000: {
"areaCode": 610000,
"id": 2,
"price": 4
}
},
3: {
710000: {
"areaCode": 710000,
"id": 3,
"price": 8
}
}
}
Collectors.groupingBy Collectors.groupingBy 多级分组
public class MapFun {
public static void main(String[] args) {
List<ProductPrice> products = new ArrayList<>();
products.add(new ProductPrice(1L, 510000, 3));
products.add(new ProductPrice(1L, 520000, 4));
products.add(new ProductPrice(2L, 610000, 4));
products.add(new ProductPrice(2L, 620000, 6));
products.add(new ProductPrice(3L, 710000, 7));
products.add(new ProductPrice(3L, 710000, 8));
Map<Long, Map<Integer, List<ProductPrice>>> collect = products.stream().collect(
Collectors.groupingBy(
ProductPrice::getId, Collectors.groupingBy(ProductPrice::getAreaCode)
)
);
System.out.println(JSON.toJSONString(collect));
}
@Data
@AllArgsConstructor
static class ProductPrice {
private Long id;
private Integer areaCode;
private Integer price;
}
}
{
1: {
520000: [{
"areaCode": 520000,
"id": 1,
"price": 4
}],
510000: [{
"areaCode": 510000,
"id": 1,
"price": 3
}]
},
2: {
620000: [{
"areaCode": 620000,
"id": 2,
"price": 6
}],
610000: [{
"areaCode": 610000,
"id": 2,
"price": 4
}]
},
3: {
710000: [{
"areaCode": 710000,
"id": 3,
"price": 7
}, {
"areaCode": 710000,
"id": 3,
"price": 8
}]
}
}