最有效的执行方式

时间:2014-11-29 08:47:39

标签: java arraylist while-loop hashmap

我目前正在使用Bukkit API,但这更多地与普通Java有关,所以我在这里问。我有两个HashMaps来存储数据,并且需要能够比较两者。

public HashMap<Scoreboard, ArrayList<PlayerScore>> lastList = new HashMap<Scoreboard, ArrayList<PlayerScore>>();
public HashMap<Scoreboard, ArrayList<PlayerScore>> currentList = new HashMap<Scoreboard, ArrayList<PlayerScore>>();

我可以使用while循环进行迭代,这样可行,但是这有问题,因为我必须遍历循环中的另一个ArrayList,并且由于有两个散列图,我最终做了4个散列图总计......这是我目前的代码:

    public void remove(Scoreboard board) {
    Iterator<Entry<Scoreboard, ArrayList<PlayerScore>>> lastIt = lastList.entrySet().iterator();
    Iterator<Entry<Scoreboard, ArrayList<PlayerScore>>> currentIt = currentList.entrySet().iterator();
    while (lastIt.hasNext()) {
        System.out.println("dbg1");
        Entry<Scoreboard, ArrayList<PlayerScore>> nextLast = lastIt.next();
        if (nextLast.getKey().equals(board)) {
            System.out.println("dbg2");
            while (currentIt.hasNext()) {
                Entry<Scoreboard, ArrayList<PlayerScore>> nextCurrent = currentIt.next();

                ArrayList<PlayerScore> lastArray = nextLast.getValue();
                ArrayList<PlayerScore> currentArray = nextCurrent.getValue();

                Iterator<PlayerScore> lastArrayIt = lastArray.iterator();
                Iterator<PlayerScore> currentArrayIt = currentArray.iterator();
                while (lastArrayIt.hasNext()) {
                    System.out.println("dbg3");
                    PlayerScore nextCurrentArray = currentArrayIt.next();

                    while (currentArrayIt.hasNext()) {
                        System.out.println("dbg4");
                        if (!lastArray.contains(nextCurrentArray)) {
                            System.out.println("dbg5");
                            board.resetScores(nextCurrentArray.getString());
                            lastArrayIt.remove();
                            currentArrayIt.remove();
                            break;
                        }
                    }

                    break;
                }

                break;
            }
        }

        break;
    }
}

我知道,它非常混乱,但我真的不知道还能为此做些什么。 while循环执行得太多,以至于服务器控制台只填充了&#34; dbg4&#34;因为它的输出速度如此之快。这也会使服务器崩溃。

任何人都知道更好的方法吗?

1 个答案:

答案 0 :(得分:3)

您不需要遍历hashmap。你可以这么做:

ArrayList<PlayerScore> lastArray = lastList.remove(board);
ArrayList<PlayerScore> currentArray = currentList.remove(board);