【问题标题】:Sort multiple relative Lists in Java在Java中对多个相对列表进行排序
【发布时间】:2016-10-20 08:37:49
【问题描述】:

我见过其他类似的问题 - 但人们无法很好地理解这些问题以提供好的解决方案 - 所以我会努力做得更好......

我有两个列表,一个用于事件名称,一个用于事件时间

List<String> eventNames= new ArrayList<String>();
List<Calendar> eventTimes= new ArrayList<Calendar>();

这些列表之间存在一对一的关系,因此 eventNames 中的第一项与 eventTimes 的第一项相关……第二项相同,第三项……等等。

我可以使用以下命令按升序排列 eventTimes:

Collections.sort(eventTimes, new Comparator<Calendar>() {
    public int compare(Calendar c1, Calendar c2) {
        return c1.getTime().compareTo(c2.getTime());
    }
});

但我想按 eventTimes 中事件的时间对这两个列表进行排序。

我认为我需要“映射”这些列表...?但我不明白该怎么做?

【问题讨论】:

  • 将它们都封装在一个对象中,创建该对象的列表,然后按日期排序。

标签: java android dictionary arraylist comparator


【解决方案1】:

如果您不想创建一个新实体class 只是为了对它们进行排序,那么您可以创建一个表示索引的整数列表并使用带有Calendar 列表的比较器对其进行排序。

public static void main(String[] args) {
    final List<String> eventNames= new ArrayList<String>();
    final List<Calendar> eventTimes= new ArrayList<Calendar>();
    ArrayList<Integer> indices = range(eventNames.size());
    Collections.sort(indices, new Comparator<Integer>() {
        @Override
        public int compare(Integer i, Integer j) {
            return eventTimes.get(i).compareTo(eventTimes.get(j));
        }
    });

    //Now to get the index i = 5 after sort you must do this
    eventNames.get(indices.get(5));
}

private static ArrayList<Integer> range(int size) {
    return range(0, size);
}

public static ArrayList<Integer> range(int start, int end) {
    ArrayList<Integer> list = Lists.newArrayList();
    for (int i = start; i < end; i++) {
        list.add(i);
    }
    return list;
}

PS:我并不为上面的代码感到自豪。为了某种意义,要么将此代码包装在一个类中,要么创建一个可比较的实体类。

【讨论】:

  • 谢谢,这是我选择继续使用的解决方案,尽管其他建议也很有帮助。
【解决方案2】:

Apache Commons Math 库有一个实用方法,允许您对 double[] 参数执行类似的操作,请参阅MathArrays#sortInPlace(...)。通过使用compareTo(...) 而不是双重比较来调整集合和任意类型的代码应该很简单。

【讨论】:

    【解决方案3】:

    定义一个新的类,例如:

    public class Event {
        private final String name;
        private final Calendar time;
    
        public Event(String name, Calendar time) {
            this.name = name;
            this.time = time;
        }
    
        public String getName() {
            return name;
        }
    
        public Calendar getTime() {
            return time;
        }
    }
    

    并将您的事件的名称和时间存储在一个 ListEvent 中。

    【讨论】:

      【解决方案4】:

      为该实体创建新的实体类并将两者封装在该实体中

              public class NewEntity {
               String eventNames;    
               Calendar eventTimes;
          // Getter and setter here 
          }
      

      然后当您将它们添加到集合中时。

      List<NewEntity > newEntityList = new ArrayList<NewEntity >();
      
      NewEntity newEntityFirst = new NewEntity();
      newEntityFirst .setEventNames(eventNameFirst);
      newEntityFirst .setEventTimes(eventTimesFirst);
      newEntityList.add(newEntityFirst);
      
      
      NewEntity newEntitySecond = new NewEntity();
      newEntitySecond .setEventNames(eventNameSecond);
      newEntitySecond .setEventTimes(eventTimesSecond);
      newEntityList .add(newEntitySecond);
      

      所以你会得到相关的事件和他们的名字

      希望这会有所帮助:)

      【讨论】:

        猜你喜欢
        • 2017-12-13
        • 2018-02-12
        • 2013-07-09
        • 2022-01-24
        • 2018-10-20
        • 1970-01-01
        • 2012-10-20
        • 2019-08-18
        • 1970-01-01
        相关资源
        最近更新 更多