繁体   English   中英

Java:转换列表<Object>到地图<String, List<String> &gt;

[英]Java: Convert List<Object> to Map<String, List<String>>

我目前正在从数据库中获取数据,如下所示:

Model---Year
aaaaa---2010
aaaaa---2011
aaaaa---2012
bbbbb---2010
bbbbb---2011

我通过 SQL 语句、getJdbcTemplate 和 ResultSet 获取这些数据。 然后将其存储在值对象列表中:

List<ModelAndYear> modelYearList = new ArrayList<ModelAndYear>();

我的 ModelAndYear 类是这样的:

public class ModelAndYear{
   private String model;
   private String year;

   public String getModel() {
       return model;
   }
   public void setModel(String model) {
       this.model= model;
   }
   public String getYear() {
       return year;
   }
   public void setYear(String year) {
       this.year= year;
   }

我如何采用填充的 modelYearList 谁的数据格式为:

aaaaa,2010
aaaaa,2011
aaaaa,2012
bbbbb,2010
bbbbb,2011

并将其排序为

Map<String, List<String>> 

所以数据将采用以下格式:

aaaaa,<2010,2011,2012>
bbbbb,<2010,2011>

我不知道从哪里开始。

我有:

Map<String, List<String>> newModelYearList = new HashMap<String, List<String>>();
        for(int i=0; i < modelYearList.size(), i++){
           //sorting will occur here i think?     
           //how do i program the action of getting model from list 1 and checking if it exists in list 2?
        }

只要您拥有 Java 8,您就可以使用流一次性完成此操作。

您本质上要做的是groupBy函数,但还要在其上应用map 以下是您要查找的内容(为了清楚起见,添加了许多缩进):

Map<String, List<String>> yearsByModel = 
    modelYearList
        .stream()
        .collect(
            Collectors.groupingBy(
                may -> may.getModel(),        
                Collectors.mapping(may -> may.getYear(), Collectors.toList())));

我不想只给出代码,你应该尝试自己做。

这是你应该做的:

  1. 创建一个带有字符串和列表的新地图,就像您想要的那样,例如将其称为结果

  2. 循环遍历 modelYearList 中的每个元素

  3. 获取模块并检查结果映射是否已经包含具有相同模块字符串的键

  4. 如果是这样,获取与模块一起存储的列表,并添加年份

  5. 如果没有,则创建一个字符串类型的新列表并添加年份,并将其与模块名称一起存储在结果中

注意:我将使用 HashSet 和 HashMap,但如果您愿意,您也可以使用 List 和 Map

HashSet<ModelAndYear> myl = new HashSet<>();
//Add all the ModelAndYear classes that you have
HashMap<String, HashSet<String>> result = new HashMap<>();
for(ModelAndYear m : myl) //loop through every element in myl; this loop only works for lists, but makes it easier than loop with a counter in loop counting to the size of the list
    String model = myl.getModel(); //get the model
    if(result.containsKey(model)){ //did we already put a list in the result map for the model?
    result.get(model).add(m.getYear()) //add the year to the existing list in result map for the model
}else{
HashSet<String> newList = new HashSet<String>(); //create new list
newList.add(m.getYear()); //add the year
result.put(model, newList); //Store the list in the results
}

此代码根据要求为您提供结果映射:

Map<String, List<String>> resultMap = 
                modelYearList
                    .stream()
                    .collect(
                        Collectors.groupingBy(
                                ModelAndYear::getModel,        
                            Collectors.mapping(ModelAndYear::getYear, Collectors.toList())));
        
        resultMap.forEach((k, v) -> System.out.println(k +" : "+ v.toString()));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM