【问题标题】:Java List sort remove duplicate ids and return latest DateTimeJava List 排序删除重复的 id 并返回最新的 DateTime
【发布时间】:2014-08-22 14:38:56
【问题描述】:

上课

import org.joda.time.DateTime;

public class Timestamp {
    String id;
    DateTime timestamp = new DateTime();

    public Timestamp(String id, DateTime timestamp){
         this.id = id;
         this.timestamp = timestamp;
    }

}

主函数

public static void main(String[] args) {
List<Timestamp > list = new ArrayList<Timestamp >();

list .add(new Timestamp ("1"));
list .add(new Timestamp ("2"), new DateTime().plusHours(1));
list .add(new Timestamp ("3"), new DateTime().plusHours(2));
list .add(new Timestamp ("4"), new DateTime().plusHours(3));
list .add(new Timestamp ("5"), new DateTime().plusHours(4));
list .add(new Timestamp ("1"), new DateTime().plusHours(5));
list .add(new Timestamp ("2"), new DateTime().plusHours(6));
list .add(new Timestamp ("3"), new DateTime().plusHours(7));
list .add(new Timestamp ("4"), new DateTime().plusHours(8));
list .add(new Timestamp ("5"), new DateTime().plusHours(9));
list .add(new Timestamp ("6"), new DateTime().plusHours(10));
list .add(new Timestamp ("1"), new DateTime().plusHours(11));
list .add(new Timestamp ("3"), new DateTime().plusHours(12));
list .add(new Timestamp ("7"), new DateTime().plusHours(13));
list .add(new Timestamp ("2"), new DateTime().plusHours(14));
list .add(new Timestamp ("3"), new DateTime().plusHours(15));
list .add(new Timestamp ("4"), new DateTime().plusHours(16));
list .add(new Timestamp ("8"), new DateTime().plusHours(17));
list .add(new Timestamp ("9"), new DateTime().plusHours(18));
list .add(new Timestamp ("10"), new DateTime().plusHours(19));
List<Object> newList = removeDuplicates(list);
}

如何从列表中删除重复的时间戳 id 并仅返回该 id 的最新时间戳时间。

我尝试使用以下方法,但已经遇到了无法正确删除 id 的问题。

public static List<Object> removeDuplicates(List<Timestamp > l) {
        // ... the list is already populated
        Set<Timestamp > s = new TreeSet<Timestamp >(new Comparator<Timestamp >() {

            @Override
            public int compare(Timestamp o1, Timestamp o2) {
                if (o1.getId().equalsIgnoreCase(o2.getId())) {
                    return 0;
                } else {
                    return 1;
                }
            }
        });
        s.addAll(l);
        return Arrays.asList(s.toArray());
    }

【问题讨论】:

  • 为什么不首先将项目存储在Set 中?另外,不要将List&lt;Timestamp&gt; 转换为List&lt;Object&gt;
  • Set 的语义是仅在不存在时才添加项目 - 这与我们的要求不同,即应存在具有相同 ID 的最后添加的时间戳。

标签: java sorting arraylist comparator


【解决方案1】:

希望您需要获取时间戳列表,如果 2 个时间戳具有相同的 id,那么您需要获取最新的。 使用 Java 8

 private static List<Timestamp> removeDuplicates(List<Timestamp> list) {
        Map<String, Timestamp> collect = list
                .stream()
                .collect(Collectors.toMap(Timestamp::getId,
                        v -> v,
                        (u, v) -> u.getTimestamp().compareTo(v.getTimestamp()) > 0 ? u : v));
       return new ArrayList<>(collect.values());
    }

【讨论】:

    【解决方案2】:

    你需要实现Set接口,然后使用Stack

    1. Set 将帮助您删除重复项。 Set
    2. Stack 的pop 将帮助您获取添加到Stack 的最新元素。

    【讨论】:

      【解决方案3】:

      最好使用TreeSet:

      // public class whatever implements Comparator<Bean>
      // ...
      // public void foo() {
      // ...
      TreeSet<Bean> ourSet = new TreeSet<Bean>(this);
      
      public int compare(Bean o1,
                Bean o2) {
         return o1.getDateTime().compareTo(o2.getDateTime());
      }
      

      现在使用.first() 获取最低元素,使用.last() 获取最高元素。其他可能感兴趣的方法是lower()higher()pollFirst()pollLast()。希望对您有所帮助。

      【讨论】:

        猜你喜欢
        • 2016-06-13
        • 2016-05-24
        • 2017-02-25
        • 2016-03-17
        • 2013-05-02
        • 1970-01-01
        • 1970-01-01
        • 2021-11-16
        • 2016-04-27
        相关资源
        最近更新 更多