将列表项排序为顺序列表

时间:2018-12-18 13:24:15

标签: java arraylist

我有来自计算机的消息列表,这些事件具有速度值和时间戳。使用速度值,我可以确定机器是否正在运行。我想将该列表中的项目排序为顺序列表。

让我们以原始的项目列表为例:

    速度:90;时间戳:12:00 速度:80;时间戳:12:01 速度:80;时间戳:12:02 速度:90;时间戳:12:03 速度:10;时间戳:12:04 速度:10;时间戳:12:05 速度:20;时间戳:12:06 速度:90;时间戳:12:07 速度:90;时间戳:12:08 速度:90;时间戳:12:09

这就是我想要的:

运行:

  • 12:00
  • 12:01
  • 12:02
  • 12:03

已停止:

  • 12:04
  • 12:05
  • 12:06

运行:

  • 12:07
  • 12:08
  • 12:09

我无法为此提出一个优雅的解决方案。这就是我所拥有的:

public class ListSortingTest {

    static List<Map<String, List<String>>> prodList = new ArrayList<>();
    static ArrayList<String> pts = new ArrayList<>();
    static ArrayList<String> npts = new ArrayList<>();

    public static void main(String[] args) {
        List<Pair<Integer, String>> messages = new ArrayList<>();
        messages.add(new ImmutablePair<Integer, String>(90, "12:00")); //p
        messages.add(new ImmutablePair<Integer, String>(80, "12:01")); //p
        messages.add(new ImmutablePair<Integer, String>(80, "12:02")); //p
        messages.add(new ImmutablePair<Integer, String>(90, "12:03")); //p
        messages.add(new ImmutablePair<Integer, String>(10, "12:04")); //np
        messages.add(new ImmutablePair<Integer, String>(10, "12:05")); //np
        messages.add(new ImmutablePair<Integer, String>(20, "12:06")); //np
        messages.add(new ImmutablePair<Integer, String>(90, "12:07")); //p
        messages.add(new ImmutablePair<Integer, String>(90, "12:08")); //p
        messages.add(new ImmutablePair<Integer, String>(90, "12:09")); //p

        int x = 0;
        for (Pair<Integer, String> message : messages) {
            if (message.getLeft() > 60) {
                pts.add(message.getRight());
                // check if end of array is reached + if next message has speed lower than threshold
                // so i know when to compile the list
                if (x++ == messages.size() - 1 || messages.get(x).getLeft() < 20) {
                    Map<String, List<String>> temp = new HashMap<>();
                    //copy so clear() doesn't remove list from temp
                    ArrayList<String> temparr = new ArrayList<String>(pts);
                    temp.put("p", temparr);
                    prodList.add(temp);
                    pts.clear();
                }
            } else {
                npts.add(message.getRight());
                if (x++ == messages.size() - 1 || messages.get(x).getLeft() > 20) {
                    Map<String, List<String>> temp = new HashMap<>();
                    //copy so clear() doesn't remove list from temp
                    ArrayList<String> temparr = new ArrayList<String>(npts);
                    temp.put("np", temparr);
                    prodList.add(temp);
                    npts.clear();
                }
            }
        }
        System.out.println(prodList);
    }
}

这将产生正确的输出:

[{p=[12:00, 12:01, 12:02, 12:03]}, {np=[12:04, 12:05, 12:06]}, {p=[12:07,   12:08, 12:09]}]

我可以更有效地做到这一点吗?

1 个答案:

答案 0 :(得分:0)

您可以使用Java 8流,然后编写类似的代码

prodList = messages.stream()
        .collect(Collector.of(
            ArrayList::new,
            (accumulator, item) -> {
                if (accumulator.isEmpty()) {
                    accumulator.add(createNewMap(item));
                } else {
                    Map<String, List<String>> lastMap = accumulator.get(accumulator.size() - 1);
                    String keyMap = (new ArrayList<>(lastMap.keySet()).get(0));
                    if (keyMap.equals(getKeyStringValue(item))) {
                        List<String> items = lastMap.get(keyMap);
                        items.add(item.getValue());
                        lastMap.put(keyMap, items);
                    } else {
                        accumulator.add(createNewMap(item));
                    }
                }
            },
            (li1, li2) -> {
                li1.addAll(li2);
                return li1;
            }
        ));

private Map<String, List<String>> createNewMap(Pair<Integer, String> item) {
    Map<String, List<String>> map = new HashMap<>();
    List<String> list = new ArrayList<>();
    list.add(item.getValue());
    map.put(getKeyStringValue(item), list);
    return map;
}

private String getKeyStringValue(Pair<Integer, String> item) {
    return item.getKey() > 20 ? "p" : "np";
}

并会看到类似的输出:

[{p=[12:00, 12:01, 12:02, 12:03]}, {np=[12:04, 12:05, 12:06]}, {p=[12:07, 12:08, 12:09]}]