Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从列表中查找并替换某些属性已不存在的对象_Java_List - Fatal编程技术网

Java 从列表中查找并替换某些属性已不存在的对象

Java 从列表中查找并替换某些属性已不存在的对象,java,list,Java,List,我有一个存储某些对象的列表。我想从列表中删除一个属性不存在的对象。我的示例代码是,这里请帮助我 public class Mysamples { private void example() { List<SomeType> listWithDuplicates = new ArrayList<SomeType>(); SomeType someObject1 = new SomeType("hello", "1");

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

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=1,SomeType{name=hello, id=2}, SomeType{name=hello, id=1}} SomeType{name=hello1, id=2},SomeType{name=hello1, id=1}]]

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

private void removeDuplicates(列表中有重复项){
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);
        }
    }
}
Map nameCountMap=新HashMap(); //获取每个名称的计数 对于(SomeType x:listWithDuplicates){ 整数计数=nameCountMap.get(x.getName()); 如果(计数=null){ 计数=新整数(0); } 计数++; nameCountMap.put(x.getName(),count); } //打印副本 对于(SomeType x:listWithDuplicates){ 如果(nameCountMap.get(x.getName())>1){ 系统输出println(x); } } }
我用了另一种方法

已删除的私有无效副本(包含副本的列表){

List=null;
Map Map=newhashmap();
对于(SomeType x:listWithDuplicates){
if(map.get(x.getName())!=null){
get(x.getName()).add(x);
}否则{
列表=新的ArrayList();
增加(x);
put(x.getName(),list);
}
}
List listWithOutDuplicates=新建ArrayList();
for(条目:map.entrySet()){
if(entry.getValue().size()>1){
for(SomeType SomeType:entry.getValue()){
没有重复项的列表。添加(someType);
}
}
}
System.out.println(列表无重复项);
}

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

  • 定义比较器,如下所示:-

    公共名称比较器实现比较器{

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

  • 使用上述比较器使用TreeSet,而不是使用List

    TreeSet-sortedSet=新树集(新名称比较器())

  • 现在将项目添加到上述集合中

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


  • 不清楚的是,您试图实现的目标我的结果列表包含的对象少于实际数量的一个。不添加一个结果的标准是什么?当添加的列表为空时,如何决定添加哪一项?这对我很有帮助
        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);
    }
    
       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;
          }
       }