[英]How do I merge two sorted object lists based on common attribute in Java
我有兩個對象列表,它們都有一個稱為時間戳的通用屬性,並且都按時間戳排序。
我想根據時間戳逐個廣播這些對象,例如,如果第一個列表的第一個對象的時間戳<第二個列表的第一個對象,廣播第一個列表的第一個對象,然后將第一個列表的第二個對象與第一個對象進行比較第二個列表。
我是Java的新手。 這是我想出的:
//Merge the 2 object lists based on timestamp
ListIterator<x> xIterator = list1.listIterator();
ListIterator<y> yIterator = list2.listIterator();
while (xIterator.hasNext() && yIterator.hasNext()) {
if (xIterator.next().timestamp <= yIterator.next().timestamp) {
Bundle extra = new Bundle();
extra.putParcelable(LocalBroadcastConstants.ACTION, xIterator.previous());
MyBroadcastManager.getInstance(getTargetContext()).sendBroadcast(
new Intent(LocalBroadcastConstants.ACTION_SEND).putExtras(extra)
);
yIterator.previous();
} else {
Bundle extra = new Bundle();
extra.putParcelable(LocalBroadcastConstants.ACTION,
yIterator.previous());
MyBroadcastManager.getInstance(getTargetContext()).sendBroadcast(
new Intent(LocalBroadcastConstants.ACTION_SEND).putExtras(extra)
);
xIterator.previous();
}
我知道我的邏輯是不正確的,因為對於Iterators中的第一項,xIterator.previous和yIterator.previous將什么都沒有指向。 我似乎找不到此問題陳述的正確解決方案。 請幫忙。
使用索引而不是迭代器。 您還可以使用方法來防止代碼冗余
int xIndex = 0;
int yIndex = 0;
while (xIndex < list1.size() && yIndex < list2.size()) {
if (list1[xIndex].timestamp <= list2[yIndex].timestamp) {
broadcast(list1, xIndex);
++xIndex;
}
else {
broadcast(list2, yIndex);
++yIndex;
}
}
dealWithLeftovers(list1, xIndex);
dealWithLeftovers(list2, yIndex);
broadcast
:
private void broadcast(List<> list, int index) {
Bundle extra = new Bundle();
extra.putParcelable(LocalBroadcastConstants.ACTION, list[index]);
MyBroadcastManager.getInstance(getTargetContext()).sendBroadcast(
new Intent(LocalBroadcastConstants.ACTION_SEND).putExtras(extra)
);
}
dealWithLeftovers
:
private void dealWithLeftovers(List<> list, int index) {
for (int i = index; i < list.size() ; ++i) {
broadcast(list, i);
}
}
一個簡單但易於實現的解決方案:
而已。 現在,循環排序列表以按順序廣播對象。
如果要將兩個列表與“其中的一個字段進行排序”合並,以下示例可能會對您有所幫助。 使用Java8流
Obj p1 = new Obj();
p1.setDept("EC");
p1.setName("Sagar");
p1.setJoinedDate(new Date(2007, 8, 2));
Obj p2 = new Obj();
p2.setDept("EE");
p2.setName("Rang");
p2.setJoinedDate(new Date(2007, 8, 3));
Obj p3 = new Obj();
p3.setDept("ME");
p3.setName("Avadh");
p3.setJoinedDate(new Date(2008, 8, 2));
Obj p4 = new Obj();
p4.setDept("IP");
p4.setName("Pams");
p4.setJoinedDate(new Date(2007, 8, 1));
List<Obj> dept1 = new ArrayList<>();
dept1.add(p1);
dept1.add(p2);
List<Obj> dept2 = new ArrayList<>();
dept2.add(p3);
dept2.add(p4);
dept1.addAll(dept2);
List<Obj> sortedList = dept1.stream().sorted(new Comparator<Obj>() {
@Override
public int compare(Obj o1, Obj o2) {
if(Instant.ofEpochMilli(o1.getJoinedDate().getTime()).atZone(ZoneId.systemDefault())
.toLocalDateTime().isBefore(Instant.ofEpochMilli(o2.getJoinedDate().getTime()).atZone(ZoneId.systemDefault())
.toLocalDateTime())){
return -1;
}else if(Instant.ofEpochMilli(o1.getJoinedDate().getTime()).atZone(ZoneId.systemDefault())
.toLocalDateTime().isAfter(Instant.ofEpochMilli(o2.getJoinedDate().getTime()).atZone(ZoneId.systemDefault())
.toLocalDateTime())){
return 1;
}
return 0;
}
}).collect(Collectors.toList());
System.out.println(sortedList);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.