将列表中的项目排序为顺序列表



我有一个来自机器的消息列表,这些事件有一个速度值和一个时间戳。使用速度值,我可以确定机器是否在运行。我想把那个列表中的项目按顺序排列。

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

  1. 速度:90;时间戳:12:00
  2. 速度:80;时间戳:12:01
  3. 速度:80;时间戳:12:02
  4. 速度:90;时间戳:12:03
  5. 速度:10;时间戳:12:04
  6. 速度:10;时间戳:12:05
  7. 速度:20;时间戳:12:06
  8. 速度:90;时间戳:12:07
  9. 速度:90;时间戳:12:08
  10. 速度: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]}]

我能用一种更有效的方式来做这件事吗?

您可以使用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]}]

最新更新