[英]How to sort a multimap in Java?
所以我試圖用Java對多圖進行排序,這是圖的樣子:
Map<Integer, Pair<Double, Color>> inputMap;
Map<Integer, Pair<Double, Color>> outputMap;
這是我的配對課程:
class Pair<Double, Color> {
public final Double x;
public final Color y;
public Pair(Double x, Color y) {
this.x = x;
this.y = y;
}
}
我正在嘗試按配對的第一項進行排序。 我試圖使用這樣的東西:
outputMap = new TreeMap<>(inputMap);
但這並不完全符合我的預期……似乎根本無法對其進行排序。
如何按Pair.X對地圖排序?
所有SortedMap實現(包括TreeMap
)均key
而不是按value
排序。
https://docs.oracle.com/javase/7/docs/api/java/util/SortedMap.html
進一步提供其鍵的總體排序的Map。
在內部按value
對Map
排序也不利於針對通過key
訪問進行優化的性能。
Map
只會將一個值與每個鍵相關聯。
當您查看put
的文檔時,這一點尤其明顯
將指定值與該映射中的指定鍵關聯(可選操作)。 如果該映射先前包含該鍵的映射,則舊值將替換為指定值。
因此,您使用Map<Integer, Pair<Double, Color>>
定義的甚至不是MultiMap 。 這就解釋了為什么它不符合您的預期。
您正在尋找的是SortedSetMultimap
一個SetMultimap,其給定鍵的值集保持排序; 也就是說,它們包含一個SortedSet。
同
//Class renamed to reflect what it actually is, to avoid conflicts with other Pairs. Also you don't need generics then, which you did not use any way.
class ColorPair implements Comparable {
public final Double x;
public final Color y;
public ColorPair (Double x, Color y) {
this.x = x;
this.y = y;
}
//compares only the first component of the Pair
public int compareTo(ColorPair other){
return Double.compare(x, other.x)
}
}
然后您可以使用以下命令創建SortedSetMultimap
TreeMultimap<Integer, ColorPair> sortedSetMap = new TreeMultimap<>() `
並用ColorPair
填充它
您不能按地圖值排序-僅按鍵排序。
如果同時需要兩者-用於對象的O(1)的HashMap訪問對象和排序遍歷-創建一個排序列表並將相同的對象保留在那里。
List<Pair<Double, Object>>outputList = inputMap.values().stream().sorted((o1, o2) -> 0 /*IMPLEMENT_COMPARATOR_HERE*/).collect(Collectors.toList());
如果地圖是可變的-最好創建一個同時包含地圖和列表的類,並提供“添加”,“獲取”和“刪除”之類的方法。 應避免直接訪問這些結構。
不要忘記同步兩個數據結構上的操作,以確保您的代碼線程安全。
突出
您可以使用以下方法對地圖進行排序:
Map<Integer, Pair<Double, Color>> sortedMap = map.entrySet().stream()
.sorted(Comparator.comparingDouble(e -> e.getValue().x))
.collect(Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(p1, p2) -> p1,
LinkedHashMap::new));
完整代碼
public static void main(String[] args) {
new MyClass().test();
}
public void test() {
Map<Integer, Pair<Double, Color>> map = Map.of(
1, new Pair<>(1.0, Color.BLACK),
2, new Pair<>(3.0, Color.RED),
3, new Pair<>(2.0, Color.BLUE));
Map<Integer, Pair<Double, Color>> sortedMap = map.entrySet().stream()
.sorted(Comparator.comparingDouble(e -> e.getValue().x))
.collect(Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(p1, p2) -> p1,
LinkedHashMap::new));
System.out.println(sortedMap);
}
class Pair<Double, Color> {
public final Double x;
public final Color y;
public Pair(Double x, Color y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair<?, ?> pair = (Pair<?, ?>) o;
return Objects.equals(x, pair.x) &&
Objects.equals(y, pair.y);
}
@Override
public int hashCode() {
return Objects.hash(x, y);
}
@Override
public String toString() {
return "Pair{" +
"x=" + x +
", y=" + y +
'}';
}
}
產量
{1=Pair{x=1.0, y=java.awt.Color[r=0,g=0,b=0]}, 3=Pair{x=2.0, y=java.awt.Color[r=0,g=0,b=255]}, 2=Pair{x=3.0, y=java.awt.Color[r=255,g=0,b=0]}}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.