[英]Sort items of list into sequential lists
我有來自機器的消息列表,這些事件具有速度值和時間戳。 使用速度值,我可以確定機器是否正在運行。 我想將該列表中的項目排序為順序列表。
讓我們以原始的項目列表為例:
這就是我要的:
運行:
停止:
運行:
我還無法為此提出一個優雅的解決方案。 這就是我所擁有的:
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]}]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.