简体   繁体   中英

How to avoid duplicates in List<Map<String, String>>?

I am having 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.

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.

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM