如何hashmap按整数值排序a ,我找到的答案之一就在这里
由Evgeniy Dorofeev撰写,他的回答是这样的
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("a", 4);
map.put("c", 6);
map.put("b", 2);
Object[] a = map.entrySet().toArray();
Arrays.sort(a, new Comparator() {
public int compare(Object o1, Object o2) {
return ((Map.Entry<String, Integer>) o2).getValue().compareTo(
((Map.Entry<String, Integer>) o1).getValue());
}
});
for (Object e : a) {
System.out.println(((Map.Entry<String, Integer>) e).getKey() + " : "
+ ((Map.Entry<String, Integer>) e).getValue());
}
Run Code Online (Sandbox Code Playgroud)
产量
c : 6
a : 4
b : 2
Run Code Online (Sandbox Code Playgroud)
我的问题是如何成为描述?如果我想对HashMap Asc进行排序我该怎么做? …
我正在使用ELKI我KMeansLloyd<NumberVector> with k=3 每次运行我的java代码时使用的数据集群我得到完全不同的集群结果,这是正常的还是我应该做些什么来使我的输出几乎稳定?这里是我从elki教程获得的代码
DatabaseConnection dbc = new ArrayAdapterDatabaseConnection(a);
// Create a database (which may contain multiple relations!)
Database db = new StaticArrayDatabase(dbc, null);
// Load the data into the database (do NOT forget to initialize...)
db.initialize();
// Relation containing the number vectors:
Relation<NumberVector> rel = db.getRelation(TypeUtil.NUMBER_VECTOR_FIELD);
// We know that the ids must be a continuous range:
DBIDRange ids = (DBIDRange) rel.getDBIDs();
// K-means should be used with squared Euclidean (least squares):
//SquaredEuclideanDistanceFunction dist = SquaredEuclideanDistanceFunction.STATIC; …Run Code Online (Sandbox Code Playgroud)