简体   繁体   English

执行此操作的最有效方法

[英]Most efficient way to execute this

I'm currently working with the Bukkit API, but this has more to do with general Java, so I'm asking here. 我目前正在使用Bukkit API,但这与通用Java有更多关系,所以我在这里问。 I have two HashMaps to store data into, and need to be able to compare the two. 我有两个HashMaps将数据存储到其中,并且需要能够比较两者。

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

I can iterate using while loops, and that works, but there's a problem with this because I then have to iterate through another ArrayList within the loop, and since there are two hashmaps, I end up doing 4 hashmaps in total... This is my current code: 我可以使用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;
    }
}

I know, it's very messy, but I don't really know what else to do for this. 我知道,这很混乱,但是我真的不知道该怎么做。 The while loops execute so much that the server console is filled with only "dbg4" because it's outputting so fast. while循环执行得如此之多,以至于服务器控制台仅输出“ dbg4”,因为它的输出是如此之快。 This also crashes the server. 这也会使服务器崩溃。

Anyone know a better way to do this? 有人知道更好的方法吗?

You dont need to iterate over the hashmap. 您不需要遍历哈希图。 You could just do: 您可以这样做:

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