使用Java对HashMap类中的值进行排序 [英] Sorting by values in HashMap class using Java
问题描述
这是HashMap的键和值:
map.put(ertu,5);
map.put(burak,4);
map.put(selin,2);
map.put(can,1);
我尝试获得如下结果:
1 = can
2 = selin
4 = burak
5 = ertu
以下是我的代码:
$ p $ import java.util。*;
public class mapTers {
public static void main(String [] args){
HashMap< String,Integer> map = new HashMap< String,Integer>();
map.put(ertu,5);
map.put(burak,4);
map.put(selin,2);
map.put(can,1);
Integer dizi [] = new Integer [map.size()];
Set anahtarlar = map.keySet();
Iterator t = anahtarlar.iterator();
int a = 0; (t.hasNext()){
dizi [a] = map.get(t.next());
while
a ++;
}
Arrays.sort(dizi);
。对于(INT I = 0; I< map.size();我++){
而(t.hasNext()){
如果(笛子[I] .equals(map.get(t.next()))){
System.out.println(dizi [i] +=+ t.next());
}
}
}
}
}
每次调用t.next()时,迭代器的指针都会向前移动。最终,迭代器到达最后。您需要重置迭代器。此外,调用t.next()两次将指针移动两次。
这是我的解决方案:
import java.util。*;
public class mapTers
{
public static void main(String [] args)
{
HashMap< String,Integer> map = new HashMap< String,Integer>();
map.put(ertu,5);
map.put(burak,4);
map.put(selin,2);
map.put(can,1);
Integer dizi [] = new Integer [map.size()];
Set anahtarlar = map.keySet();
Iterator t = anahtarlar.iterator();
int a = 0;
while(t.hasNext())
{
dizi [a] = map.get(t.next());
a ++;
}
Arrays.sort(dizi);
for(int i = 0; i< map.size(); i ++)
{
t = anahtarlar.iterator();
while(t.hasNext())
{
String temp =(String)t.next();
如果(笛子[I] .equals(map.get(TEMP)))
{
的System.out.println(笛子[I] + = +温度);
}
}
}
}
}
I'm trying to get results HashMap sorted by value.
This is HashMap's keys and values:
map.put("ertu", 5);
map.put("burak", 4);
map.put("selin", 2);
map.put("can", 1);
I try to get results like this:
1 = can
2 = selin
4 = burak
5 = ertu
Here is my code:
import java.util.*;
public class mapTers {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("ertu", 5);
map.put("burak", 4);
map.put("selin", 2);
map.put("can", 1);
Integer dizi[] = new Integer[map.size()];
Set anahtarlar = map.keySet();
Iterator t = anahtarlar.iterator();
int a = 0;
while (t.hasNext()) {
dizi[a] = map.get(t.next());
a++;
}
Arrays.sort(dizi);
for (int i = 0; i < map.size(); i++) {
while (t.hasNext()) {
if (dizi[i].equals(map.get(t.next()))) {
System.out.println(dizi[i] + " = " + t.next());
}
}
}
}
}
Every time that you call t.next(), the iterator's pointer is moved forward. Eventually, the iterator reaches the end. You need to reset the iterator. Also, calling t.next() twice moves the pointer twice.
Here's my solution:
import java.util.*;
public class mapTers
{
public static void main(String[] args)
{
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("ertu", 5);
map.put("burak", 4);
map.put("selin", 2);
map.put("can", 1);
Integer dizi[] = new Integer[map.size()];
Set anahtarlar = map.keySet();
Iterator t = anahtarlar.iterator();
int a = 0;
while (t.hasNext())
{
dizi[a] = map.get(t.next());
a++;
}
Arrays.sort(dizi);
for (int i = 0; i < map.size(); i++)
{
t = anahtarlar.iterator();
while (t.hasNext())
{
String temp = (String)t.next();
if (dizi[i].equals(map.get(temp)))
{
System.out.println(dizi[i] + " = " + temp);
}
}
}
}
}
这篇关于使用Java对HashMap类中的值进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!