0

I have a List where some Objects are being stored.I would like to remove an object from list with certain attribute already not exists.My sample code is, here please help me.

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

*The out put is*

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

but i want the out put like

[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

4 回答 4

0

我用另一种方式做到了

私人无效removeDuplicates(列表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);
}
于 2013-11-12T04:17:50.703 回答
0

这可能已经结束了,但仍然..

  1. 定义一个比较器,如下所示:-

    公共 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

于 2013-11-12T04:58:15.213 回答
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);
        }
    }
}
于 2013-11-11T11:11:22.260 回答
0

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

于 2013-11-11T05:07:38.993 回答