- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个 LinkedHashMap 并且想要执行以下操作:
例如,如果我有一个如下所示的 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]}
如果您愿意,还可以强制输出 Map
为 LinkedHashMap
:
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/
我是一名 Java 新手,在概念化如何解决尝试创建表示某些分层数据的 LinkedHashMap 的问题时遇到问题。 这是我目前所知的。在此示例中,我有一个 LinkedHashMap,它显示了展平的
我有 LinkedHashMap 列表,如何不可分割地访问每个 LinkedHashMap。 List> listOfRecords 我想要 List abc= new listOsRecords.1
我有一个 LinkedHashMap,其中包含另一个 LinkedHashMap,如下所示: LinkedHashMap> containerMap = new LinkedHashMap>(); 我
我有 2 个 LinkedHashMap, LinkedHashMap, Color> map; LinkedHashMap, Color> totalMap; 两者是相同的,但 map 已通过使用撤
我有一个 map 列表,我需要单独使用其中的每张 map 以用于进一步的目的。这是我正在使用的代码片段 for(int i = 0; i ()); } int j=0; whi
我有一个扩展 LinkedHashMap 的类 (EntireFile)。我尝试转换: EntireFile old = (EntireFile) functionReturningLinkedHas
我有一个构建 LinkedHashMap 的方法,这样我就可以保持顺序。这是我的方法: public class MyClass { public Map buildMap() {
我是 Scala 的新手。我一直在尝试将 java LinkedHashMap 转换为 Scala 中的等效集合(LinkedHashMap?)以保留插入顺序。 尝试按照其他线程中的建议进行操作,但似
我有一个LinkedHashMap看起来像这样(真的不知道如何说明 HashMap): { "10/10/2010 10:10:10" => "SomeText1", "10/10/2019
我被包裹了LinkedHashMap>进入列表; List>> list = new ArrayList(mainCodesMap.entrySet()); 哪个mainCodeMap是 Map> 的
我有两个链接的 HashMap (key - String, value = String[]),它们在两个链接的 HashMap 中具有相同的大小和相同的键,我希望能够根据键,验证一个链接 Hash
假设我有以下数据结构: LinkedHashMap> foodFamilies = new LinkedHashMap<>(); 看起来像这样: {Fruit = [{Name = Apple,
如何对 LinkedHashMap 的 Arraylist 进行排序 前任。 ArrayList> list = new ArrayList>(); LinkedHashMap lin
LinkedHashMap lHashMap = new LinkedHashMap(); lHashMap.put("One", new Integer(1)); lHashMap.
关闭。这个问题需要details or clarity .它目前不接受答案。 想改善这个问题吗?通过 editing this post 添加详细信息并澄清问题. 8年前关闭。 Improve thi
我有一个简单的问题来找到数组 A 中的第一个唯一元素。但是,令我困扰的是使用不同方法的时间复杂度。到目前为止,我已经尝试过这两种方法。 第一种方法: LinkedHashMap> map = new
这个问题已经有答案了: How do I compare strings in Java? (23 个回答) 已关闭 4 年前。 我正在尝试将一对放入 linkedhashmap 中,但是当我放入 2
我需要用Java实现很久以前在Delphi中实现的代码,我尝试使用LinkedHashMap(在Delphi中使用TStringlist),因为我需要在插入元素时获取元素的索引,问题是是它不起作用..
在正确实现 hashCode 和 equals() 的情况下,以下代码会返回 false? myLinkedHashMap.containsKey(myLinkedHashMap.keySet().i
我让 XStream 使用此 xml 为我构建链接 HashMap : #!/masterofsoundtrack/broadcast #!/masterofsoun
我是一名优秀的程序员,十分优秀!