【问题标题】:How to avoid duplicates in List<Map<String, String>>?如何避免 List<Map<String, String>> 中的重复?
【发布时间】:2016-05-21 15:58:06
【问题描述】:

我这样拥有List&lt;Map&lt;String, String&gt;&gt; testList =new ArrayList&lt;Map&lt;String, String&gt;&gt;();

我想根据前 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());
    }
}

谁能帮我解决这个问题?

谢谢

【问题讨论】:

  • 当你真的应该拥有一个对象时,你正在使用数据结构。这是一个糟糕的设计。
  • 我同意@duffymo,我会考虑创建一个“Person”类。
  • 任何标准的 Java 集都只是简单地调用 hashcode()equals() 对放入其中的对象来确定唯一性。我不明白为什么 LinkedHashSet 不适合你。
  • @duffymo 这是退出,我们不能对遗留代码做任何事情。我们可以在这里做点什么吗?
  • 对不起,如果它是遗留的,那么您将无能为力。你真的在问“我应该因为不想做正确的事而做一些愚蠢的事情吗?”我不支持。投票结束。

标签: java arraylist hashmap


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-17
    • 1970-01-01
    • 2021-04-25
    • 1970-01-01
    • 2020-08-01
    • 1970-01-01
    • 2020-04-01
    相关资源
    最近更新 更多