我想以这样的方式准备一个HashMap
Key : Country Code
Value : List of returned orderEntries
以下处理数据方法每 5 个 orderEntry
处理一次,可以来自任何国家/地区。
让我说得更清楚一些。我有来自不同国家/地区的 orderEntries
列表,现在我想根据国家/地区键将这些条目放入 map
中。就像如果 20 个条目来自 US
,那么 US
将是键,20 个条目将是值。但问题是我不想为 map
内的每个县创建一个列表。
public void processSegmentData(final List resultSet)
{
for (final Object orderEntry : resultSet)
{
if (orderEntry instanceof OrderEntryModel)
{
String countryCode = null;
final OrderModel order = ((OrderEntryModel) orderEntry).getOrder();
if (order.getDeliveryAddress() != null)
{
countryCode = order.getDeliveryAddress().getCountry().getIsocode();
}
orderEntriesMap.put(Config.getParameter(countryCode+".return.pid"), orderEntries);
}
}
}
最佳答案
所以你正在寻找一个包含链接列表的 HashMap :
public HashMap<String, LinkedList<OrderEntryModel>> processSegmentData(final List resultSet) {
HashMap<String, LinkedList<OrderEntryModel>> orderEntriesMap = new HashMap<String, LinkedList<OrderEntryModel>>();
for (final Object orderEntry : resultSet) {
if (orderEntry instanceof OrderEntryModel) {
String countryCode = null;
final OrderModel order = ((OrderEntryModel) orderEntry).getOrder();
if (order.getDeliveryAddress() != null) {
countryCode = order.getDeliveryAddress().getCountry().getIsocode();
}
if (!orderEntriesMap.containsKey(countryCode)) {
orderEntriesMap.put(countryCode, new LinkedList<OrderEntryModel>());
}
orderEntriesMap.get(countryCode).add((OrderEntryModel) orderEntry);
}
}
return orderEntriesMap;
}
将是一个基于您提供的源代码猜测对象名称的示例。
关于java - 准备多值国家 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28405981/