从列表中查找和替换具有某些属性的对象已经不存在

时间:2013-11-11 04:25:10

标签: java list

我有一个列表,其中存储了一些对象。我想从列表中删除某个属性已经不存在的对象。我的示例代码,请帮助我。

public class Mysamples {

    private void example() {
        List<SomeType> listWithDuplicates = new ArrayList<SomeType>();

        SomeType someObject1 = new SomeType("hello", "1");
        SomeType someObject2 = new SomeType("hello", "2");
 }

private void removeDuplicates(List<SomeType> listWithDuplicates) {
    /* Set of all attributes seen so far */
    Set<String> attributes = new HashSet<String>();
    /* All confirmed duplicates go in here */
    List duplicates = new ArrayList<SomeType>();

    for (SomeType x : listWithDuplicates) {
        if (attributes.contains(x.getName())) 
        {
            duplicates.add(x);
        }
        attributes.add(x.getName());
    }

    System.out.println(duplicates);
   // System.out.println(attributes);
}

public static void main(String[] args) {
    // TODO code application logic here
    List<SomeType> listWithDuplicates = new ArrayList<SomeType>();
    SomeType someObject1 = new SomeType("hello", "1");
    SomeType someObject2 = new SomeType("hello", "2");
    SomeType someObject3 = new SomeType("hello", "1");
    SomeType someObject4 = new SomeType("hello1", "2");
    SomeType someObject5 = new SomeType("hello1", "1");
    SomeType someObject6 = new SomeType("hello2", "2");


    listWithDuplicates.add(someObject1);
    listWithDuplicates.add(someObject2);
    listWithDuplicates.add(someObject3);
    listWithDuplicates.add(someObject4);
    listWithDuplicates.add(someObject5);
    listWithDuplicates.add(someObject6);
    Mysamples s = new Mysamples();

    s.removeDuplicates(listWithDuplicates);
}
}

* 输出 *

[SomeType{name=hello, id=2}, SomeType{name=hello, id=1}, SomeType{name=hello1, id=1}]

但我希望像

一样
[SomeType{name=hello, id=1,SomeType{name=hello, id=2}, SomeType{name=hello, id=1}} SomeType{name=hello1, id=2},SomeType{name=hello1, id=1}]]

4 个答案:

答案 0 :(得分:0)

您的算法只会在第一次遇到名称后才会找到重复的。因此,例如,第一个“hello”不在属性集中,因此您添加它(但它不被视为重复),然后接下来的两个“hello”条目被视为重复项并被添加到重复项列表中。 “hello1”也一样。如果要查找所有重复项(包括第一次遇到它们),则必须对集合执行两次传递。首先收集属性集中的所有名称,然后再次标记所有重复项。

答案 1 :(得分:0)

private void removeDuplicates(List<SomeType> listWithDuplicates) {
    Map<String, Integer> nameCountMap = new HashMap<String, Integer>();
    // get count of each name
    for (SomeType x : listWithDuplicates) {
        Integer count = nameCountMap.get(x.getName());
        if (count == null) {
            count = new Integer(0);
        }
        count++;
        nameCountMap.put(x.getName(), count);
    }
    // Print duplicates
    for (SomeType x : listWithDuplicates) {
        if (nameCountMap.get(x.getName()) > 1) {
            System.out.println(x);
        }
    }
}

答案 2 :(得分:0)

我以另一种方式做到了

private void removeDuplicates(List listWithDuplicates){

    List<SomeType> list = null;
    Map<Object, List<SomeType>> map = new HashMap<Object, List<SomeType>>();
    for (SomeType x : listWithDuplicates) {
        if (map.get(x.getName()) != null) {
            map.get(x.getName()).add(x);
        } else {
            list = new ArrayList<SomeType>();
            list.add(x);
            map.put(x.getName(), list);
        }
    }
    List<SomeType> listWithOutDuplicates = new ArrayList<SomeType>();
    for (Entry<Object, List<SomeType>> entry : map.entrySet()) {
        if (entry.getValue().size() > 1) {
            for (SomeType someType : entry.getValue()) {
                listWithOutDuplicates.add(someType);
            }
        }
    }
    System.out.println(listWithOutDuplicates);
}

答案 3 :(得分:0)

这可能会结束,但仍然......

  1. 定义比较器如下: -

    public NameComparator实现Comparator {

       public int compare(SomeType x, SomeType y){
          if(x.getName()==null){   // you want this field to be missing or null right?
                return Integer.MIN_VALUE;
          }
          else if(y.getName()==null){
              return Integer.MAX_VALUE;
          }else{
                return Integer.MIN_VALUE;
          }
       }
    

    }

  2. 不使用List,而是使用上面的比较器使用TreeSet。

    TreeSet sortedSet = new TreeSet(new NameComparator());

  3. 现在将项目添加到上面的集合中。

  4. 现在,如果存在,您可以使用sortedSet.first()获取您要查找的对象。 Javadoc