简体   繁体   English

Java将{String,String} []转换为Map <String,String[]>

[英]Java convert {String,String}[] to Map<String,String[]>

Given the class: 鉴于课程:

 public class CategoryValuePair
 {
      String category;
      String value;
 }

And a method: 一个方法:

public Map<String,List<String>> convert(CategoryValuePair[] values);

Given that in values we can receive many entries with the same category, I want to convert these into a Map grouped on category. 鉴于在values我们可以接收具有相同类别的许多条目,我想将它们转换为按类别分组的Map

Is there a quick / efficient way to perform this conversion? 有快速/有效的方法来执行此转换吗?

As far as I know there is not easier way than iterating on values, and then putting the values in the map (like some predefined method). 据我所知,没有比迭代值更简单的方法,然后将值放在地图中(就像一些预定义的方法)。

Map<String, List<String>> map = new HashMap<String, List<String>>();
if (values != null) {
    for (CategoryValuePair cvp : values) {
      List<String> vals = map.get(cvp.category);
      if (vals == null) {
        vals = new ArrayList<String>();
        map.put(cvp.category, vals);
      }
      vals.add(cvp.value);
    }
}

I changed the map values from String[] to List<String> since it seems easier to me to use that so you don't have to hassle with array resizing. 我将地图值从String[]更改为List<String>因为我似乎更容易使用它,因此您不必为数组大小调整而烦恼。

To make it in fewer lines of code, use Google Collections : 要使用更少的代码行,请使用Google Collections

public Map<String, Collection<String>> convert(CategoryValuePair[] values) {
    Multimap<String, String> mmap = ArrayListMultimap.create();
    for (CategoryValuePair value : values) {
        mmap.put(value.category, value.value);
    }
    return mmap.asMap();
}

If you don't want to allow duplicate values, replace ArrayListMultimap with HashMultimap. 如果您不想允许重复值,请将ArrayListMultimap替换为HashMultimap。

使用lambdaj,您只需要一行代码即可实现以下结果:

group(values, by(on(CategoryValuePair.class).getCategory()));

Just for the sake of implementation... The method returns Map and also checks for duplicates in the arrays... though performance wise its heavy ... 只是为了实现...该方法返回Map并检查数组中的重复...虽然性能明显很重...

public Map<String,String[]> convert(CategoryValuePair[] values)
{
    Map<String, String[]> map = new HashMap<String, String[]>();
    for (int i = 0; i < values.length; i++) {
        if(map.containsKey(values[i].category)){
            Set<String> set = new HashSet<String>(Arrays.asList(map.get(values[i].category)));
            set.add(values[i].value);
            map.put(values[i].category, set.toArray(new String[set.size()]));
        }else {
            map.put(values[i].category, new String[]{values[i].value});
        }
    }

    return map;
}

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

相关问题 Java 8:如何将String转换为Map <String,String> ? - Java 8: How to convert String to Map<String,String>? Java转换列表 <String> 到地图 <String, String> - Java Convert List<String> to Map<String, String> 转换列表<String>到地图<String, String>在 Java 中 - Convert List<String> to Map<String, String> in Java 如何转换地图 <String, List<String> &gt;到地图 <String, String> 在java 8中 - How to Convert a Map<String, List<String>> to Map<String, String> in java 8 将 map 字符串转换为 map 字符串列表字符串 - Java 7 - convert map string string to map string list string - Java 7 如何转换地图 <String, Set<String> &gt;到地图 <String, String[]> 在Java中 - How to convert Map<String, Set<String>> to Map<String, String[]> in Java 转换列表<HashMap<String,String> &gt; 列出<map<String,String> &gt; 爪哇 - convert List<HashMap<String,String>> to list<map<String,String>> java Java地图 <String, Map<String, Object> &gt;转换为字符串并返回 - Java Map<String, Map<String, Object>> convert to String and back 转换地图 <String, String[]> 到地图 <String, Object[]> 在java中 - Convert Map<String, String[]> to Map<String, Object[]> in java 如何将java对象列表转换为Map <String, Map<String, String> &gt; - How to convert a list of java objects to Map<String, Map<String, String>>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM