有一节课
class Employee {
int id;
String name;
}
Run Code Online (Sandbox Code Playgroud)
和一个包含此对象的地图
Map<Integer, Employee> map = new HashMap<Integer, Employee>();
Run Code Online (Sandbox Code Playgroud)
现在我想在map一个基础之上排序Employee's name.意味着当我使用迭代此地图时Map.Entry,Employee对象必须按字母顺序返回.
提前致谢
这不是我为了理解下一步要做什么而模拟的真实代码。
我有一个具有年龄、身高体重属性的 Person 类。
现在在我的班级组中,
我创建了两个四个对象
Person programmer, student, clerk, tech;
Run Code Online (Sandbox Code Playgroud)
我有 HashMap rollCall
Map<Person, Integer> rollCall = new HashMap<Person, Integer>();
Run Code Online (Sandbox Code Playgroud)
使用 Person 和 Number of Persons 作为整数类型添加所有这些
rollCall.put(programmer, 1);
rollCall.put(clerk, 2);
rollCall.put(student, 1);
rollCall.put(tech, 3);
Run Code Online (Sandbox Code Playgroud)
我见过很多人使用 TreeMap 根据值对 HashMap 进行排序,我想对 Person 的属性而不是值进行排序。我想按年龄对所有这些人进行排序(即程序员.getAge();)。我不确定我是否会使用仅适用于集合而非地图的比较器。. 请帮忙 ... 。
在我的项目中,我已经采用了这样的HashMap
HashMap degree = new HashMap();
假设我有:
degree.put("a",5);
degree.put("b",2);
degree.put("c",4);
degree.put("d",2);
degree.put("e",3);
degree.put("f",5);
Run Code Online (Sandbox Code Playgroud)
现在我必须根据给定的Integer值对此列表进行排序
排序的HashMap应该是:
{a = 5,f = 5,c = 4,e = 4,b = 4,d = 2}
我怎么能这样做?
我需要像TreeMap一样排序的地图,但按值排序.我的地图将是巨大的,所以我不能随时对我的地图进行排序.是否存在解决此问题的好方案?也许存在符合此要求的外部罐子?
为什么循环keySetTreeMap并得到一个.containsKey == false?
for (Object thisObject : map.keySet()) {
if (!map.containsKey(thisObject)) {
System.out.println("This line should be never reached.");
}
}
Run Code Online (Sandbox Code Playgroud)
经过很多次,很多不同的迭代和调用此行被击中.A map.get(thisObject)会回来null.但调试显示密钥(相同的引用,值和散列)和实际值在地图中.地图很小(25个元素)TreeMap<Long, Double>
更新:
正如@rgettman theres 所猜测的那样Comparator,在构造TreeMap时使用了一个自定义排序(没有看到它,因为它是从另一个类构造的).这个比较器只是(我猜)从这里复制粘贴
改变Comparator:
public int compare(Object a, Object b) {
if((Double)base.get(a) > (Double)base.get(b)) {
return 1;
} else if((Double)base.get(a) == (Double)base.get(b)) {
return 0;
} else {
return -1;
}
}
Run Code Online (Sandbox Code Playgroud)
至
...
} else if(base.get(a).equals(base.get(b))) { …Run Code Online (Sandbox Code Playgroud) 我有2个数组:
private String[] placeName;
private Double[] miles;
Run Code Online (Sandbox Code Playgroud)
它们中的数据如下所示:
placeName = {"home", "away", "here"};
miles = {111, 11, 3};
Run Code Online (Sandbox Code Playgroud)
值的位置彼此匹配。例如,家= 111,客场= 11
我需要将这些数组排序在一起,以免丢失最低到最高数字对它们的匹配方式。做到这一点的最佳方法是什么?我需要先合并数组吗?
我有一个简单的地图,需要创建一个列表,该列表根据给定列表中的升序排序:
Map auto = new HashMap();
auto.put("Merc", 3);
auto.put("Citroen", 5);
auto.put("Opel", 10);
auto.put("BMW", 20);
List<String> given = new ArrayList<>();
given.add("Opel");
given.add("BMW");
given.add("Citroen");
Run Code Online (Sandbox Code Playgroud)
所以给定的清单需要排序,以便它按顺序排列:雪铁龙,欧宝,宝马.想到:
这看起来很可怕:/,任何建议,也许更好的数据结构使用?
我有一个Map<String, Long> map我希望Long使用Java 8的功能按相反顺序排序的值.使用Google我发现这个线程提供了这个解决方案
Map<String, Long> sortedMap = map.entrySet().stream()
.sorted(comparing(Entry::getValue))
.collect(toMap(Entry::getKey, Entry::getValue,
(e1,e2) -> e1, LinkedHashMap::new));
Run Code Online (Sandbox Code Playgroud)
如果我想在评论中将订单反转,则表示要使用comparing(Entry::getValue).reversed()而不是comparing(Entry::getValue).
但是,代码不起作用.但是通过这种小小的适应性,它可以:
Map<String, Long> sortedMap = map.entrySet().stream()
.sorted(Comparator.comparing(Entry::getValue))
.collect(Collectors.toMap(Entry::getKey, Entry::getValue,
(e1, e2) -> e1, LinkedHashMap::new));
Run Code Online (Sandbox Code Playgroud)
我是否必须先进行一些导入才能运行原始代码?
从那以后,仍然有什么可以得到颠倒的顺序
Map<String, Long> sortedMap = map.entrySet().stream()
.sorted(Comparator.comparing(Entry::getValue).reversed())
.collect(Collectors.toMap(Entry::getKey, Entry::getValue,
(e1, e2) -> e1, LinkedHashMap::new));
Run Code Online (Sandbox Code Playgroud)
给出了一条错误信息:
The type Map.Entry does not define getValue(Object) that is applicable here
Run Code Online (Sandbox Code Playgroud) 我有以下两个类:
class KeyClass {
private prop1;
private prop2;
hashcode() {
//implemented properly
}
equals() {
//implemented properly
}
}
class ValueClass {
private prop1;
private prop2;
hashcode() {
//implemented properly
}
equals() {
//implemented properly
}
}
Run Code Online (Sandbox Code Playgroud)
我试图从地图中找出最大对,其中这些类的对象分别是键和值对.我也有一个com.google.common.collect.Ordering<ValueClass>使用多个比较器.我可以使用这个排序轻松找出最大值,但我感兴趣的是最大值的关键.
我可以编写一个特定的实现,其中我可以跟踪循环中的值并使用排序来比较值(类似于查找最大值的传统方式),但我想知道我们是否已经有这样的情况由Guava任何其他图书馆处理?
我有一个地图条目列表
Map<String, Integer> map = new HashMap<>();
...(fill the map)...
List<Entry<String, Integer>> entries = new ArrayList<>(map.entrySet());
Run Code Online (Sandbox Code Playgroud)
我想根据这个答案按值对其进行排序,但顺序相反。当我做
Comparator<Entry<String, Integer>> cmp = Entry.comparingByValue();
entries.sort(cmp.reversed());
Run Code Online (Sandbox Code Playgroud)
一切正常。但是当我尝试将上述两行缩短为
entries.sort(Entry.comparingByValue().reversed());
Run Code Online (Sandbox Code Playgroud)
编译器返回
error: incompatible types: Comparator<Entry<Object,V>> cannot be converted to Comparator<? super Entry<String,Integer>>
entries.sort(Entry.comparingByValue().reversed());
where V is a type-variable:
V extends Comparable<? super V>
Run Code Online (Sandbox Code Playgroud)
这看起来很奇怪,因为 Comparator 的reversed()默认方法的实现只是将它转移到Collections.reverseOrder(),我可以将上面的行更改为
entries.sort(Collections.reverseOrder(Entry.comparingByValue()));
Run Code Online (Sandbox Code Playgroud)
上班。但是什么是正确的类型Entry.comparingByValue().reversed()?
Comparator<Entry<String, Integer>> cmp = Entry.comparingByValue().reversed();
Run Code Online (Sandbox Code Playgroud)
似乎不起作用。省略类型参数Entry<String, Integer>有效,但这会导致编译器稍后发出“未经检查的方法调用”警告。
java ×9
hashmap ×3
sorting ×3
collections ×2
treemap ×2
arrays ×1
comparator ×1
containskey ×1
dictionary ×1
generics ×1
guava ×1
java-8 ×1
java-stream ×1
keyset ×1
map ×1
sortedmap ×1