gpt4 book ai didi

java - 如何在 Map java 中允许重复值?

转载 作者:行者123 更新时间:2023-11-29 04:16:38 28 4
gpt4 key购买 nike

我正在使用 Autocompletetextview,所以我希望显示重复名称...该名称来自服务器...我尝试了下面的一些 Java 代码示例,但它不起作用...

public class EmptyActivity extends AppCompatActivity {

LinkedHashMap<String, ArrayList<String>> hashMap_searchresult = new LinkedHashMap<String, ArrayList<String>>();


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_empty);
hashMap_searchresult.clear();

hashMap_searchresult.put("a1",new ArrayList<String>(Arrays.asList("a","b","c","d")));
hashMap_searchresult.put("a1",new ArrayList<String>(Arrays.asList("e","f","g","h")));

for (Map.Entry<String, ArrayList<String>> entry : hashMap_searchresult.entrySet()) {
String key = entry.getKey();
ArrayList<String> value2222 = entry.getValue();
Log.e("checkstatus"," key " + key + " value : " + value2222.toString());
}
}
}

This is the Output getting

key : a1 value : [e, f, g, h]


But i want output below like this

 key :  a1  value : [a, b, c, d]
key : a1 value : [e, f, g, h]

谁能告诉我应该用什么做这个?

最佳答案

这越来越丑陋了,但你能不能给你一张列表列表的 map :

LinkedHashMap<String, List<List<String>>> hashMap_searchresult = new LinkedHashMap<>();

List<List<String>> list = new ArrayList<>();
list.add(new ArrayList<String>(Arrays.asList("a", "b", "c", "d")));
list.add(new ArrayList<String>(Arrays.asList("e", "f", "g", "h")));
hashMap_searchresult.put("a1", list);

for (Map.Entry<String, List<List<String>>> entry : hashMap_searchresult.entrySet()) {
String key = entry.getKey();
List<List<String>> value2222 = entry.getValue();
for (List<String> lst : value2222) {
String[] arr = new String[list.size()];
arr = lst.toArray(arr);
System.out.println("checkstatus" + " key " + key + " value : " + Arrays.toString(arr));
}
}

enter image description here

Demo

关于java - 如何在 Map java 中允许重复值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51983871/

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