繁体   English   中英

从列表中查找并替换具有特定属性的对象不存在

[英]find and replace object from list with certain attribute already not exists

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

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

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

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

我已经用另一种方式做到了

私人无效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);
}

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

  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()获取您要查找的对象。 Java文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM