簡體   English   中英

執行此操作的最有效方法

[英]Most efficient way to execute this

我目前正在使用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循環執行得如此之多,以至於服務器控制台僅輸出“ dbg4”,因為它的輸出是如此之快。 這也會使服務器崩潰。

有人知道更好的方法嗎?

您不需要遍歷哈希圖。 您可以這樣做:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM