[英]How to avoid duplicates in List<Map<String, String>>?
I am having List<Map<String, String>> testList =new ArrayList<Map<String, String>>();
我正在使用List<Map<String, String>> testList =new ArrayList<Map<String, String>>();
this way. 这条路。
I want to eleminate the duplicate maps out of the list based on first 5 keys, last 2 keys are optional. 我想根据前5个键从列表中消除重复的映射,最后2个键是可选的。
I tried using linkedhashset it worked fine, but this code is legacy code which has lot of comparators used and i cannot change that and use set. 我尝试使用linkedhashset可以正常工作,但是此代码是遗留代码,其中使用了很多比较器,并且我无法更改它和使用set。
Set<Map<String, String>> testList = new LinkedHashSet<Map<String, String>>();
ListOfMaps.java ListOfMaps.java
public class ListOfMaps {
Map<String,String> map = new HashMap<String,String>();
List<Map<String, String>> testList =new ArrayList<Map<String, String>>();
public static void main(String[] args) {
ListOfMaps ll = new ListOfMaps();
ll.test();
}
public void test()
{
map = new HashMap<String,String>();
map.put("year", "2015");
map.put("standrad", "second");
map.put("age", "30");
map.put("gender", "M");
map.put("class", "first");
map.put("marks", "100");
map.put("score", "200");
testList.add(map);
map = new HashMap<String,String>();
map.put("year", "2015");
map.put("standrad", "second");
map.put("age", "31");
map.put("gender", "F");
map.put("class", "first");
map.put("marks", "100");
map.put("score", "200");
testList.add(map);
//This map object has duplicate keys year,standrad,age,gender,class same as like first map object .
//so this object should be ignore while adding into list.
//marks and score score keys are optional and need not to be verified.
map = new HashMap<String,String>();
map.put("year", "2015");
map.put("standrad", "second");
map.put("age", "30");
map.put("gender", "M");
map.put("class", "first");
map.put("marks", "100");
map.put("score", "200");
testList.add(map);
System.out.println(testList.toString());
}
}
Can anyone help me in this issue? 谁能帮助我解决这个问题?
Thanks 谢谢
Having bad design it is hard to solve your problem. 设计不好,很难解决您的问题。 Here one way to solve your problem using extra Person
class maintaining your legacy code. 这是使用额外的Person
类维护您的遗留代码来解决问题的一种方法。
public class ListOfMaps {
Map<String, String> map = new HashMap<String, String>();
List<Map<String, String>> testList = new ArrayList<Map<String, String>>();
Set<Person> st = new HashSet<>();
/**
* @param args
*/
public static void main(String[] args) {
ListOfMaps ll = new ListOfMaps();
ll.test();
}
public void test() {
map = new HashMap<String, String>();
map.put("year", "2015");
map.put("standrad", "second");
map.put("age", "30");
map.put("gender", "M");
map.put("class", "first");
map.put("marks", "100");
map.put("score", "200");
if (st.add(new Person(map)))
testList.add(map);
map = new HashMap<String, String>();
map.put("year", "2015");
map.put("standrad", "second");
map.put("age", "31");
map.put("gender", "F");
map.put("class", "first");
map.put("marks", "100");
map.put("score", "200");
if (st.add(new Person(map)))
testList.add(map);
// This map object has duplicate keys year,standrad,age,gender,class
// same as like first map object .
// so this object should be ignore while adding into list.
// marks and score score keys are optional and need not to be verified.
map = new HashMap<String, String>();
map.put("year", "2015");
map.put("standrad", "second");
map.put("age", "30");
map.put("gender", "M");
map.put("class", "first");
map.put("marks", "100");
map.put("score", "200");
if (st.add(new Person(map)))
testList.add(map);
System.out.println(testList.toString());
}
}
class Person {
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((age == null) ? 0 : age.hashCode());
result = prime * result + ((cls == null) ? 0 : cls.hashCode());
result = prime * result + ((gender == null) ? 0 : gender.hashCode());
result = prime * result
+ ((standard == null) ? 0 : standard.hashCode());
result = prime * result + ((year == null) ? 0 : year.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof Person)) {
return false;
}
Person other = (Person) obj;
if (age == null) {
if (other.age != null) {
return false;
}
} else if (!age.equals(other.age)) {
return false;
}
if (cls == null) {
if (other.cls != null) {
return false;
}
} else if (!cls.equals(other.cls)) {
return false;
}
if (gender == null) {
if (other.gender != null) {
return false;
}
} else if (!gender.equals(other.gender)) {
return false;
}
if (standard == null) {
if (other.standard != null) {
return false;
}
} else if (!standard.equals(other.standard)) {
return false;
}
if (year == null) {
if (other.year != null) {
return false;
}
} else if (!year.equals(other.year)) {
return false;
}
return true;
}
String year, standard, age, gender, cls, marks, score;
public Person(String year, String standard, String age, String gender,
String cls, String marks, String score) {
this.year = year;
this.standard = standard;
this.age = age;
this.gender = gender;
this.cls = cls;
this.marks = marks;
this.score = score;
}
public Person(Map<String, String> map) {
this.year = map.get("year");
this.standard = map.get("standrad");
this.age = map.get("age");
this.gender = map.get("gender");
this.cls = map.get("class");
this.marks = map.get("marks");
this.score = map.get("score");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.