【问题标题】:find and replace object from list with certain attribute already not exists从列表中查找并替换具有某些属性的对象已不存在
【发布时间】:2013-11-11 04:25:09
【问题描述】:

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

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}]]

【问题讨论】:

  • 不清楚,你要达到什么目的
  • 我的结果列表包含的对象少于一个作为实际对象数
  • 那么不添加该结果的标准是什么?以及当添加的列表为空时如何决定添加哪个项目

标签: java list


【解决方案1】:

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

【讨论】:

    【解决方案2】:
    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);
            }
        }
    }
    

    【讨论】:

    • 谢谢。对我很有帮助
    【解决方案3】:

    我用另一种方式做到了

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

    【讨论】:

      【解决方案4】:

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

      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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-30
        • 1970-01-01
        • 2023-04-08
        • 2015-12-01
        • 1970-01-01
        • 2015-08-12
        • 2011-01-29
        • 1970-01-01
        相关资源
        最近更新 更多