gpt4 book ai didi

Java:通过匹配 LinkedHashMap 中的相似键来复制值

转载 作者:太空宇宙 更新时间:2023-11-04 11:31:47 25 4
gpt4 key购买 nike

我有一个 LinkedHashMap 并且想要执行以下操作:

  1. 循环遍历 HashMap 并检查 Key 中的字符串是否包含“Group”单词
  2. 创建与该键字符串匹配的所有值的 String[]

例如,如果我有一个如下所示的 LinkedHashMap

HashMap<String, String> nameNum = new LinkedHashMap<String, String>();
nameNum.put("row 1", "data 1");
nameNum.put("Group_Colour_R", "Red");
nameNum.put("Group_Colour_B", "Blue");
nameNum.put("row 4", "data 4");
nameNum.put("row 5", "data 5");
nameNum.put("Group_Shape_R", "Rectangle");
nameNum.put("Group_Shape_T", "Triangle");
nameNum.put("Group_Shape_C", "Circle");
nameNum.put("row 9", "data 9");
nameNum.put("row 10", "data 10");
nameNum.put("row 11", "data 11");

循环完上面的HashMap后,我应该有2个String[]

String[] colour = {"Red", "Blue"}; 
String[] shape = {"Rectangle", "Triangle", "Circle"};

我只是不知道如何实现它。

有一些与我想要的相关的东西,在这里找到了:Partial search in HashMap

基于上面的内容,我像下面一样实现了它,但是,正如您所看到的,它多次运行相同的值。我不确定何时以及如何打破循环,以便它只给出一次值:

import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import org.apache.commons.lang3.StringUtils;

public class MainClass {
public static void main(String[] args) {
SortedMap<String, String> nameNum = new TreeMap<String, String>();
nameNum.put("row 1", "data 1");
nameNum.put("Group_Colour_R", "Red");
nameNum.put("Group_Colour_B", "Blue");
nameNum.put("row 4", "data 4");
nameNum.put("row 5", "data 5");
nameNum.put("Group_Shape_R", "Rectangle");
nameNum.put("Group_Shape_T", "Triangle");
nameNum.put("Group_Shape_C", "Circle");
nameNum.put("row 9", "data 9");
nameNum.put("row 10", "data 10");
nameNum.put("row 11", "data 11");


for (Map.Entry<String, String> entry : nameNum.entrySet()) {
String prefix = entry.getKey();
if (prefix.contains("Group")) {
prefix = prefix.substring(0, StringUtils.lastIndexOf(prefix, "_"));
System.out.println("******");
for (Map.Entry<String, String> entry1 : filterPrefix(nameNum, prefix).entrySet()) {
System.out.println(entry1);
}
System.out.println("******");
}
}
}

public static <V> SortedMap<String, V> filterPrefix(SortedMap<String,V> baseMap, String prefix) {
if(prefix.length() > 0) {
char nextLetter = (char) (prefix.charAt(prefix.length() -1) + 1);
String end = prefix.substring(0, prefix.length()-1) + nextLetter;
return baseMap.subMap(prefix, end);
}
return baseMap;
}
}

理想情况下,我想使用 LinkedHashMap,因为我想保留顺序。我非常感谢任何有关如何实现类似功能的指针/代码。任何代码片段都将受到高度赞赏。提前致谢。

最佳答案

也许 Java 8 Stream API 在这里可能会有所帮助。您可以尝试使用 Collectors.groupingBy 将相关条目分组在一起:

Map<String,List<String>> groups =
nameNum.entrySet()
.stream()
.filter(entry -> entry.getKey().contains("Group"))
.collect(Collectors.groupingBy(entry -> entry.getKey().substring(0, entry.getKey().lastIndexOf("_")),
Collectors.mapping(Map.Entry::getValue,
Collectors.toList()));

这会生成一个Map,其中包含:

{Group_Shape=[Rectangle, Triangle, Circle], Group_Colour=[Red, Blue]}

如果您愿意,还可以强制输出 MapLinkedHashMap:

Map<String,List<String>> groups =
nameNum.entrySet()
.stream()
.filter(entry -> entry.getKey().contains("Group"))
.collect(Collectors.groupingBy(entry -> entry.getKey().substring(0, entry.getKey().lastIndexOf("_")),
LinkedHashMap::new,
Collectors.mapping(Map.Entry::getValue,
Collectors.toList()));

这将改变输出Map的迭代顺序:

{Group_Colour=[Red, Blue], Group_Shape=[Rectangle, Triangle, Circle]}

关于Java:通过匹配 LinkedHashMap 中的相似键来复制值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43703337/

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