如何避免List <map <string,string =“”>&gt;?中的重复项

时间:2016-02-11 02:31:02

标签: java arraylist hashmap

我这样做List<Map<String, String>> testList =new ArrayList<Map<String, String>>();

我想根据前5个键从列表中删除重复的地图,最后2个键是可选的。

我尝试使用linkedhashset它工作得很好,但是这段代码是遗留代码,它使用了很多比较器,我无法改变它并使用set。

Set<Map<String, String>> testList = new LinkedHashSet<Map<String, String>>();

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());
    }
}

有谁可以帮我解决这个问题?

由于

1 个答案:

答案 0 :(得分:0)

设计糟糕,很难解决您的问题。这是使用额外的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");
    }
}