gpt4 book ai didi

java - 使用 lambda 表达式将列表转为 Hashmap

转载 作者:行者123 更新时间:2023-12-02 02:43:02 24 4
gpt4 key购买 nike

你能帮我用 ArrayList 中的对象构造 Hashmap 吗?

public class Store {

private int storeId;
private String storeName;
private int deliveryDays;
private List<String> servingZipCodes;
private StoreAddress storeAddress;


public Store() {
}

public Store(int storeId,String storeName,int deliveryDays,List<String> servingZipCodes,StoreAddress storeAddress){
this.storeId = storeId;
this.storeName=storeName;
this.deliveryDays=deliveryDays;
this.servingZipCodes=servingZipCodes;
this.storeAddress = storeAddress;
}

//getters and setters.
}

以及 StoreAddress 类

public class StoreAddress {

private String streetAddress1;
private String streetAddress2;
private String streetAddress3;
private String city;
private String state;
private String postalCode;
private String country;
private String phone;

public StoreAddress() {
}

public StoreAddress(String streetAddress1, String streetAddress2, String streetAddress3, String city, String state, String postalCode, String country, String phone) {
this.streetAddress1 = streetAddress1;
this.streetAddress2 = streetAddress2;
this.streetAddress3 = streetAddress3;
this.city = city;
this.state = state;
this.postalCode = postalCode;
this.country = country;
this.phone = phone;
}

这是用于测试的 Test 类。

public class Test {

public static void main(String args[]){

List<Store> storeList=new ArrayList();
StoreAddress storeAddress1 = new StoreAddress("1500 Polaris Pkwy",null,null,"Columbus","OH","43240","US","9165452345");
StoreAddress storeAddress2 = new StoreAddress("160 Easton Town Center",null,null,"Columbus","OH","43240","US","9165452345");
storeList.add(new Store(1,"Store 1",7,new ArrayList<String>(Arrays.asList("43240","43241","43242")),storeAddress1));
storeList.add(new Store(2,"Store 2",7,new ArrayList<String>(Arrays.asList("43240","43082","43081")),storeAddress2));
Map<String,List<Store>> zipCodeStoreList = null;
storeList.forEach(store -> {
List<String> servingZipCodes = store.getServingZipCodes();
servingZipCodes.stream().filter(x -> x.matches(store.getStoreAddress().getPostalCode().substring(0,5))).map(x ->new HashMap<String, Object>(){{
put(x, store);
}});
});

虽然它在 Java 7 中是可能的,但在 Java 8 中寻找解决方案。

Key: 43240, value: Store1 , Store2
Key: 43241, value: null
Key: 43242, value: null
Key: 43082, value: null
Key: 43081, value: null

最佳答案

正如我所说,你应该首先考虑做什么而不是如何,这样问题就很容易解决。然后就可以获得自描述代码,例如:

//             v--- union all zip codes from stores
Stream<String> union = storeList.stream().map(Store::getServingZipCodes)
.flatMap(List::stream)
.distinct();

// v--- find out intersections between zip codes
Stream<String> intersection = union.filter(zip ->
storeList.stream().map(Store::getServingZipCodes)
.allMatch(it -> it.contains(zip))
);

// v--- create a Map simply from intersections
Map<String, List<Store>> result = intersection.collect(toMap(
Function.identity(),
unused -> storeList
));

输出

assert result.get("43240") == [store1, store2];
assert result.get("others") == null;

关于java - 使用 lambda 表达式将列表转为 Hashmap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45109481/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com