Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 遍历hashmap?_Java_List_Loops - Fatal编程技术网

Java 遍历hashmap?

Java 遍历hashmap?,java,list,loops,Java,List,Loops,我正在努力在Java方面取得高分。 基本上,我需要一个hashmap来保存double值(因此索引从最高的double开始,因此对高分排序更容易),然后第二个值将是客户机对象,如下所示: private HashMap<Double, TempClient> players = new HashMap<Double, TempClient>(); 现在,我当然不能遍历hashmap,因为它是通过键而不是索引获取列表项的 如何迭代hashmap?对我的案子有什么好主意吗

我正在努力在Java方面取得高分。 基本上,我需要一个hashmap来保存double值(因此索引从最高的double开始,因此对高分排序更容易),然后第二个值将是客户机对象,如下所示:

private HashMap<Double, TempClient> players = new HashMap<Double, TempClient>();
现在,我当然不能遍历hashmap,因为它是通过键而不是索引获取列表项的

如何迭代hashmap?对我的案子有什么好主意吗

我在Foo课堂上试过:

输出:

0.5
0.6
0.9
0.1
2.5
代码:

public class Foo {

    public static void main(String[] args) {
        HashMap<Double, String> map = new LinkedHashMap<Double, String>();
        map.put(0.5, "hey");
        map.put(0.6, "hey1");
        map.put(0.9, "hey2");
        map.put(0.1, "hey425");
        map.put(2.5, "hey36");
        for (Double lol : map.keySet()) {
            System.out.println(lol);
        }
    }

}
公共类Foo{
公共静态void main(字符串[]args){
HashMap=newLinkedHashMap();
map.put(0.5,“嘿”);
地图放置(0.6,“hey1”);
地图put(0.9,“hey2”);
map.put(0.1,“hey425”);
地图放置(2.5,“hey36”);
for(双lol:map.keySet()){
系统输出打印项次(lol);
}
}
}

您可以这样迭代

for (Double k : players.keySet())
{
    TempClient p = players.get(k);
    // do work with k and p
}
如果要对关键点进行排序,请使用树形图等。
如果要按插入的顺序保留钥匙

他们在那里,使用例如LinkedHashMap。你可以这样迭代

for (Double k : players.keySet())
{
    TempClient p = players.get(k);
    // do work with k and p
}
如果要对关键点进行排序,请使用树形图等。
如果要按插入的顺序保留钥匙

他们在那里,使用例如LinkedHashMap。你可以这样迭代

for (Double k : players.keySet())
{
    TempClient p = players.get(k);
    // do work with k and p
}
如果要对关键点进行排序,请使用树形图等。
如果要按插入的顺序保留钥匙

他们在那里,使用例如LinkedHashMap。你可以这样迭代

for (Double k : players.keySet())
{
    TempClient p = players.get(k);
    // do work with k and p
}
如果要对关键点进行排序,请使用树形图等。
如果要按插入的顺序保留钥匙

在其中,使用LinkedHashMap。最好的方法是使用EntrySet迭代hashmap

for (Map.Entry<Double, TempClient> entry : map.entrySet()) {
    Double  key= entry.getKey();
    TempClient value= entry.getValue();
    // ...
}
for(Map.Entry:Map.entrySet()){
Double key=entry.getKey();
TempClient value=entry.getValue();
// ...
}

迭代hashmap的最佳方法是使用EntrySet

for (Map.Entry<Double, TempClient> entry : map.entrySet()) {
    Double  key= entry.getKey();
    TempClient value= entry.getValue();
    // ...
}
for(Map.Entry:Map.entrySet()){
Double key=entry.getKey();
TempClient value=entry.getValue();
// ...
}

迭代hashmap的最佳方法是使用EntrySet

for (Map.Entry<Double, TempClient> entry : map.entrySet()) {
    Double  key= entry.getKey();
    TempClient value= entry.getValue();
    // ...
}
for(Map.Entry:Map.entrySet()){
Double key=entry.getKey();
TempClient value=entry.getValue();
// ...
}

迭代hashmap的最佳方法是使用EntrySet

for (Map.Entry<Double, TempClient> entry : map.entrySet()) {
    Double  key= entry.getKey();
    TempClient value= entry.getValue();
    // ...
}
for(Map.Entry:Map.entrySet()){
Double key=entry.getKey();
TempClient value=entry.getValue();
// ...
}

您最好让TempClient对象实现可比性,将它们添加到列表中,然后只使用Collections.sort()。

您最好让TempClient对象实现可比性,将它们添加到列表中,然后只使用Collections.sort()).

您最好让TempClient对象实现可比性,将它们添加到列表中,然后只使用Collections.sort()。

您最好让TempClient对象实现可比性,将它们添加到列表中,然后只使用Collections.sort()。

因为您无法在
HashMap
中对项目进行排序,您也不能在
树映射中按值对它们进行排序
您可以将
树集
与自定义类一起使用:

class Score implements Comparable<Score>
{
  final Player player;
  final int score;

  Score(Player player, int score) {
    this.player = player;
    this.score = score;
  }

  public int compareTo(Score other) {
    return Integer.compare(this.score, other.score);
  }

  public int hashCode() { return player.hashCode(); }
  public boolean equals(Object o) { return this.player.equals(...); }
}

TreeSet<Score> scores = new TreeSet<Score>();
score.add(new Score(player, 500));

for (Score s : scores) {
  ..
}
班级成绩可比
{
最后一名选手;
最终智力得分;
分数(玩家,整数分数){
this.player=player;
这个分数=分数;
}
公共整数比较(其他分数){
返回整数.compare(this.score,other.score);
}
public int hashCode(){return player.hashCode();}
公共布尔等于(对象o){返回this.player.equals(…);}
}
树集分数=新树集();
分数。添加(新分数(玩家,500));
分数(s:分数){
..
}
这将有两个优点:

  • 这将是难以忍受的
  • 它将保持分数自动排序

它应该可以轻松地处理
equals
hashCode
compareTo
之间的一致性,但也许您应该调整一些内容(因为它是未经测试的代码)。

因为您无法在
HashMap
中对项目进行排序,您也不能在
树映射中按值对它们进行排序
您可以将
树集
与自定义类一起使用:

class Score implements Comparable<Score>
{
  final Player player;
  final int score;

  Score(Player player, int score) {
    this.player = player;
    this.score = score;
  }

  public int compareTo(Score other) {
    return Integer.compare(this.score, other.score);
  }

  public int hashCode() { return player.hashCode(); }
  public boolean equals(Object o) { return this.player.equals(...); }
}

TreeSet<Score> scores = new TreeSet<Score>();
score.add(new Score(player, 500));

for (Score s : scores) {
  ..
}
班级成绩可比
{
最后一名选手;
最终智力得分;
分数(玩家,整数分数){
this.player=player;
这个分数=分数;
}
公共整数比较(其他分数){
返回整数.compare(this.score,other.score);
}
public int hashCode(){return player.hashCode();}
公共布尔等于(对象o){返回this.player.equals(…);}
}
树集分数=新树集();
分数。添加(新分数(玩家,500));
分数(s:分数){
..
}
这将有两个优点:

  • 这将是难以忍受的
  • 它将保持分数自动排序

它应该可以轻松地处理
equals
hashCode
compareTo
之间的一致性,但也许您应该调整一些内容(因为它是未经测试的代码)。

因为您无法在
HashMap
中对项目进行排序,您也不能在
树映射中按值对它们进行排序
您可以将
树集
与自定义类一起使用:

class Score implements Comparable<Score>
{
  final Player player;
  final int score;

  Score(Player player, int score) {
    this.player = player;
    this.score = score;
  }

  public int compareTo(Score other) {
    return Integer.compare(this.score, other.score);
  }

  public int hashCode() { return player.hashCode(); }
  public boolean equals(Object o) { return this.player.equals(...); }
}

TreeSet<Score> scores = new TreeSet<Score>();
score.add(new Score(player, 500));

for (Score s : scores) {
  ..
}
班级成绩可比
{
最后一名选手;
最终智力得分;
分数(玩家,整数分数){
this.player=player;
这个分数=分数;
}
公共整数比较(其他分数){
返回整数.compare(this.score,other.score);
}
public int hashCode(){return player.hashCode();}
公共布尔等于(对象o){返回this.player.equals(…);}
}
树集分数=新树集();
分数。添加(新分数(玩家,500));
分数(s:分数){
..
}
这将有两个优点:

  • 这将是难以忍受的
  • 它将保持分数自动排序
它应该可以轻松地与
等于
hashCo之间的一致性一起工作