1

I have a list of objects in this format :

class myObj {

    private String identifier;

    public myObj(String identifier){
        this.identifier = identifier;
    }

}

List<myObj> allobjects = new ArrayList<myObj>();
allobjects.add(new myObj("123"));
allobjects.add(new myObj("123"));
allobjects.add(new myObj("123"));
allobjects.add(new myObj("123"));
allobjects.add(new myObj("1234"));
allobjects.add(new myObj("12345"));
allobjects.add(new myObj("12"));
allobjects.add(new myObj("12"));

What is an elegant method of extracting the duplicate objects into seperate Lists ? So in above example a new List is returned containing two lists. The first lists contains :

new myObj("123");
new myObj("123");
new myObj("123");
new myObj("123"); 

The second list contains :

new myObj("12");
new myObj("12");

A possible solution is to create a new object :

List<List<myObj>> newList = new ArrayList<List<myObj>>

And then for each element in the list 'allobjects' iterate over each element and for each element that is contained more than once add it to the list. Then at the end of the iteration for the current element add the newly created list to 'newList'

Is this acceptable or is there another solution ?

4

2 回答 2

2

将 equals 和 hashCode 方法添加到 myObj 类,以便您可以将它们用作 Map 键:

class myObj {

    private String identifier;

    public myObj(String identifier){
        this.identifier = identifier;
    }

    public int hashCode(){
        return identifier.hashCode();
    }

    public boolean equals(Object o){
        return identifier.equals(((myObj)o).identifier);
    }
}

然后声明一个地图:

Map<myObj, List<myObj>> map = new HashMap<myObj, List<MyObj>>()

并遍历原始列表。使用 myObj 作为映射键,每次检索与该 myObj 对应的列表。如果您第一次遇到某个 myObj,请不要忘记创建列表:

for(myObj obj : allobjects){
    List<myObj> list = map.get(obj);
    if(list == null){
        list = new ArrayList<myObj>();
        map.put(obj, list);
    }
    list.add(obj);
}
于 2012-12-05T14:10:07.427 回答
1

Implement equals as required and then you can just use contains and iterate through checking other collection.

Here's a way to do it with jdk8s' lambdas.

TransformService transformService = (inputs1, inputs2) -> {
            Collection<String> results = new ArrayList<>();
            for (String str : inputs1) {
                if (inputs2.contains(str)) {
                    results.add(str);
                }
            }
            return results;
        };
        Collection<String> inputs1 = new ArrayList<String>(2) {{
            add("lemon");
            add("cheese");
            add("orange");
        }};
        Collection<String> inputs2 = new
                ArrayList<String>(2) {{
                    add("apple");
                    add("random");
                    add("cheese");
                }};
        Collection<String> results = transformService.transform(inputs1, inputs2);
        for (String result : results) {
            System.out.println(result);
        }
    }

    public interface TransformService {
        Collection<String> transform(Collection<String> inputs1, Collection<String> inputs2);
    }
于 2012-12-05T14:02:22.317 回答