在Map中对键和值进行分组

v7pvogib  于 2021-08-20  发布在  Java
关注(0)|答案(4)|浏览(464)

我有一个Map,它的键是string,值是string[]。

  1. Map<String,String[]> map = new HashMap<>();
  2. map.put("String1", new String[]{ "abc", "dfe", "new"});
  3. map.put("String8", new String[]{"xyz","hji","new"});
  4. map.put("String2", new String[]{"abc","dfe","old"});
  5. map.put("String3", new String[]{"abc","dfe","past"});
  6. map.put("String5", new String[]{"xyz","hji","ancient"});
  7. map.put("String6",new String[]{"xyz","hji","past"});
  8. map.put("String4", new String[]{"abc","dfe","ancient"});
  9. map.put("String7", new String[]{"xyz","hji","old"});

我想使用值打印Map分组。
值是字符串的数组。
字符串数组的前两个元素将用于分组。
代码必须找到类似的值并按值分组,而不通过任何过滤器。
最终输出如下所示:

  1. String1:[{ "abc", "dfe", "new"}]
  2. String2:[{"abc","dfe","old"}]
  3. String3:[{"abc","dfe","past"}]
  4. String4:[{"abc","dfe","ancient"}]
  5. String5:[{"xyz","hji","ancient"}]
  6. String6:[{"xyz","hji","past"}]
  7. String7:[{"xyz","hji","old"}]
  8. String8:[{"xyz","hji","new"}]

如何在Java8中实现这一点?

ifmq2ha2

ifmq2ha21#

根据您的输出,您似乎只想根据键进行排序。所以你可以这样做。创建一个 SortedMap 只需将当前Map添加到构造函数中即可。

  1. SortedMap<String,String[]> smap = new TreeMap<>(map);
  2. smap.forEach((k,v)-> System.out.println(k + ":" + Arrays.toString(v)));

印刷品

  1. String1:[abc, dfe, new]
  2. String2:[abc, dfe, old]
  3. String3:[abc, dfe, past]
  4. String4:[abc, dfe, ancient]
  5. String5:[xyz, hji, ancient]
  6. String6:[xyz, hji, past]
  7. String7:[xyz, hji, old]
  8. String8:[xyz, hji, new]
soat7uwm

soat7uwm2#

您可以通过如下方式进行双重分组:

  1. Map<String, String[]> map = new HashMap<>();
  2. map.put("String1", new String[]{"abc", "dfe", "new"});
  3. map.put("String8", new String[]{"xyz", "hji", "new"});
  4. map.put("String2", new String[]{"abc", "dfe", "old"});
  5. map.put("String3", new String[]{"abc", "dfe", "past"});
  6. map.put("String5", new String[]{"xyz", "hji", "ancient"});
  7. map.put("String6", new String[]{"xyz", "hji", "past"});
  8. map.put("String4", new String[]{"abc", "dfe", "ancient"});
  9. map.put("String7", new String[]{"xyz", "hji", "old"});
  10. Map<String, Map<String, List<Map.Entry<String, String[]>>>> collect = map.entrySet()
  11. .stream()
  12. .collect(Collectors.groupingBy(x -> x.getValue()[0], Collectors.groupingBy(x -> x.getValue()[1])));
  13. Map<String, String[]> collect1 = collect.values()
  14. .stream()
  15. .flatMap(x -> x.values().stream())
  16. .flatMap(Collection::stream)
  17. .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
  18. System.out.println(collect1);
展开查看全部
ej83mcc0

ej83mcc03#

如果您只想根据键进行排序,则可以使用java 8:

  1. Map<String, String[]> map = new HashMap<>();
  2. map.put("String1", new String[]{"abc", "dfe", "new"});
  3. map.put("String8", new String[]{"xyz", "hji", "new"});
  4. map.put("String2", new String[]{"abc", "dfe", "old"});
  5. map.put("String3", new String[]{"abc", "dfe", "past"});
  6. map.put("String5", new String[]{"xyz", "hji", "ancient"});
  7. map.put("String6", new String[]{"xyz", "hji", "past"});
  8. map.put("String4", new String[]{"abc", "dfe", "ancient"});
  9. map.put("String7", new String[]{"xyz", "hji", "old"});
  10. map = map.entrySet().stream().sorted(comparingByKey()).collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e2, LinkedHashMap::new));
  11. map.forEach((s1, strings) -> System.out.println(s1 + " : " + Arrays.toString(strings)));

输出将是:

  1. String1 : [abc, dfe, new]
  2. String2 : [abc, dfe, old]
  3. String3 : [abc, dfe, past]
  4. String4 : [abc, dfe, ancient]
  5. String5 : [xyz, hji, ancient]
  6. String6 : [xyz, hji, past]
  7. String7 : [xyz, hji, old]
  8. String8 : [xyz, hji, new]
展开查看全部
mzmfm0qo

mzmfm0qo4#

正如您所提到的,您希望根据数组的前2个元素(值)对Map进行排序,然后您可以尝试使用comparator:
根据值对数组的前2个元素进行分组,然后进行排序:

  1. Map<String, String[]> map = new HashMap<>();
  2. map.put("String1", new String[]{"abc", "dfe", "new"});
  3. map.put("String8", new String[]{"xyz", "hji", "new"});
  4. map.put("String2", new String[]{"abc", "dfe", "old"});
  5. map.put("String3", new String[]{"abc", "dfe", "past"});
  6. map.put("String5", new String[]{"xyz", "hji", "ancient"});
  7. map.put("String6", new String[]{"xyz", "hji", "past"});
  8. map.put("String4", new String[]{"abc", "dfe", "ancient"});
  9. map.put("String7", new String[]{"xyz", "hji", "old"});
  10. // Compare based on 1st 2 element oof Array (Map Value)
  11. Comparator<String[]> byArrayValue = (String[] o1, String[] o2) -> o1[0].concat(o1[1]).compareTo(o2[0].concat(o2[1]));
  12. // Sort based on 1st 2 element of array
  13. map = map.entrySet().stream()
  14. .sorted(Map.Entry.<String, String[]>comparingByValue(byArrayValue))
  15. .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
  16. map.forEach((s1, strings) -> System.out.println(s1 + " : " + String.join(",", strings)));

输出将基于值数组的前2个元素:

  1. String4 : [abc, dfe, ancient]
  2. String3 : [abc, dfe, past]
  3. String2 : [abc, dfe, old]
  4. String1 : [abc, dfe, new]
  5. String8 : [xyz, hji, new]
  6. String7 : [xyz, hji, old]
  7. String6 : [xyz, hji, past]
  8. String5 : [xyz, hji, ancient]
展开查看全部

相关问题