比较 ArrayList 中的 HashMap 值
我有一个 ArrayList ,其中有一些 HashMap
这是我最后一次尝试:
private HashMap<String, String> mapValues = new HashMap<String, String>();
private HashMap<String, String> mapValues2 = new HashMap<String,String>();
private HashMap<Integer, String> mval = new HashMap<Integer, String>();
//i take the ArrayList with the maps for comparison private
ArrayList<HashMap<String, String>> check(ArrayList<HashMap<String, String>> list) {
//a new ArrayList. It will have the maps(HashMap<key, value>) with no same values.
ArrayList<HashMap<String, String>> listFinal = new ArrayList<HashMap<String, String();
for (int i = 0; i < list.size(); i++) {
mapValues = list.get(i);
mval.put(i, mapValues.get("value"));
}
for (int i = 0; i < mval.size(); i++) {
HashMap<String, String> newMapValues = new HashMap<String, String>();
mapValues2 = list.get(i);
String iVal = mapValues2.get("value");
newMapValues = list.get(i);
int flag = -1;
int remove = -1;
for (int j = i+1; j < mval.size()-1; j++) {
String jVal = mval.get(j);
if (val.compareTo(jVal) == 0) {
flag = i;
remove = j;
}
}
if (flag == -1) {
listFinal.add(newMapValues );
} else if (flag != -1) {
listFinal.remove(remove);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
只是大声思考,但我的方法类似于:
创建一个集合,在其中存储已在地图中找到的值。
每次在列表的新位置获取一个Map时,检查该Map的元素是否存在于Set中,如果存在,则从ArrayList中删除该Map(它是重复的),如果不存在,则添加该值地图的设置并继续。
确保使用 Iterator 的删除方法从 ArrayList 中删除 Map!
Just thinking out loud but my approach would be something like:
Create a Set, where you store the values that you already found in the map.
Each time you get a Map in a new position of the list, check if the element of the Map exists in the Set, if it does, remove the Map from the ArrayList (it's duplicated), if it doesn't, add the value of the Map to the Set and Carry on.
Make sure you remove the Map from the ArrayList using the Iterator's remove method!
希望有人校对一下这个,因为我没有在 IDE 中输入它,而且我还没有喝咖啡。
编辑:好的,以前的版本是错误的。检查这个。
编辑2:刚刚意识到这也行不通。例如,它可能会删除映射 3,因为它具有映射 2 的重复值,但映射 2 会因为映射 1 的其他重复值而被删除。结果:仅保留映射 1,映射 2 和 3 被删除,但映射 3 被删除地图 1 没有重复。这比我想象的要复杂一些。最好喝杯咖啡...
Hope someone proofreads this, cause I'm not typing it in an IDE and I haven't had my coffee yet.
EDIT: okay, that previous version was wrong as hell. Check this instead.
EDIT 2: just realized this won't work either. It might remove, say, map 3 because it has a dupe value with map 2, but map 2 is removed because of some other dupe value with map 1. Result: only map 1 is retained and map 2 and 3 are removed but map 3 doesn't have dupes with map 1. This is a bit more complex than I thought. Better get that coffee...
创建一个
Set>
并将list
的每个成员添加到其中。问题解决了!如果您绝对需要
ArrayList
而不是Set
,您可以从Set
创建一个新的ArrayList
,但是无论哪种方式,教训都是:让 Java 为您完成工作。在集合操作方面,您不太可能比标准库做得更好。Create a
Set<HashMap<String,String>>
and add every member oflist
to it. Problem solved!If you absolutely need an
ArrayList
instead of aSet
, you can create a newArrayList
from theSet
, but either way the lesson is: let Java do the work for you. You are unlikely to do a better job at collection manipulation than the standard library.将
Map
键与Arraylist
值进行比较Compare
Map
keys withArraylist
values