简体   繁体   English

如何基于Java中的几种条件从列表中删除重复项

[英]How remove duplicates from list based on several conditions in Java

I have a List of Flight Objects, that object has id(Long) and destinations(String). 我有一个飞行对象列表,该对象具有id(Long)和destinations(String)。 what i need to do is remove the duplicate entries of same id. 我需要做的是删除相同ID的重复条目。 But the condition is to remove those element, i can only keep the elements which has the longest destination list. 但条件是要删除这些元素,我只能保留目标列表最长的元素。

Flight{
   Long id;
   String destination;       
}

List flights <- This is what is have to filter based on the condition. 列表排期<-这是必须根据条件进行过滤的内容。

Examples for the List : 列表示例:

flight1 -> id:456 , destination LON/DXB
flight2 -> id:456 , destination LON/DXB/IND
flight3 -> id:465 , destination LON/DXB/IND/CMB
flight4 -> id:555 , destination LON/DXB
flight5 -> id:666 , destination DXB/SHJ
flight6 -> id:666 , destination DXB/SHJ/BOM

According to the condition given i can only keep in the list is : flight3,flight4,flight6 . 根据给定的条件,我只能保留在列表中: flight3,flight4,flight6 other flights can`t be in the list. 其他航班不能在列表中。 How can i get done this? 我该怎么做?

What you can do is to have a map that will make the relation ship between an id and an instance. 您可以做的是创建一个映射,使关系在ID和实例之间传递。

While iterating through the list, you check if there is an instance with the same id in the map. 在遍历列表时,您检查映射中是否存在具有相同ID的实例。 If it's not the case, you just add the mapping, otherwise you compare the destination's length (or whatever you want to compare) and you update the mapping if necessary. 如果不是这种情况,则只需添加映射,否则就比较目标的长度(或要比较的任何内容),并在必要时更新映射。

Map<Long, Flight> map = new HashMap<>();
for(Flight f : flights) {
    if(map.containsKey(f.id)) {
        if(map.get(f.id).destination.length() < f.destination.length()) {
            map.put(f.id, f);
        }
    } else {
        map.put(f.id, f);
    }
}

//you can even store them in a set
List<Flight> longestDestinationFlights = new ArrayList<>(map.values());

Ok I expect you to have a list of flights like in : List<Flight> . 好的,我希望您有一个航班清单,例如: List<Flight> And I assume further, that there will be more entries which are duplicated and the "longer" destination should be stored. 我进一步假设,将有更多重复的条目,并且应存储“更长”的目的地。

What you would do is iterating using the iterator. 您要做的是使用迭代器进行迭代。 (You can't use a for loop while removing) (删除时不能使用for循环)

   List<Flight> a = new ArrayList<>();
   List<Flight> copy = new ArrayList<>();
   Collections.copy(copy, a);
   Iterator<Flight> it = a.iterator();
   while(it.hasNext()){
       Flight f = it.next();
       if(shouldGetRemoved(f, copy)){
           it.remove();
       }
   }

shouldGetRemoved is you logic to compare the destinations. shouldGetRemoved是您比较目的地的逻辑。 Would be another for loop over the copy. 将是另一个遍历副本的循环。

My implementation goes a little long, but it is easy. 我的实现有点长,但是很容易。

  • Sort the list in descending order of size of destination list. 按目的地列表大小的降序对列表进行排序。
  • Remove flights with duplicate ids. 删除具有重复ID的航班。

Working code here . 这里的工作代码。

Here is the code. 这是代码。

private static List<Flight> removeDuplicates(List<Flight> list) {

    //sort the list, descending order of size of destination list
    Collections.sort(list, new Comparator<Flight>() {

        @Override
        public int compare(Flight o1, Flight o2) {
            if (o1.getId().equals(o2.getId())) {
                int l1 = o1.getDestination().split("/").length;
                int l2 = o2.getDestination().split("/").length;
                if (l1 < l2) {  //check whose destination list is long
                    return 1;
                } else {
                    return -1;
                }
            }
            return 0;
        }
    });

    // remove duplicates
    Set<Long> ids = new HashSet<>();
    List<Flight> newList = new ArrayList<>();
    for (int i = 0; i < list.size(); i++) {
        if (!ids.contains(list.get(i).getId())) {
            newList.add(list.get(i));
            ids.add(list.get(i).getId());
        }
    }
    return newList;
}

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

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