簡體   English   中英

使用字符串Integer映射ArrayList和另一個內部Map

[英]Mapping an ArrayList and another internal Map with String, Integer

編輯:我只是將TreeMap更改為HashMap,現在它可以正常工作了,但仍然無法滿足我的需要。

嘿,這是我堅持了很長時間的事情。

我的目標是從sequence.txt文件中獲取文本,然后調用傳遞掃描器s和整數n的構造函數。 在構造函數內部,我的目標是從sequence讀取n單詞並將其保存在ArrayList中,然后內部映射應該能夠在緊接n單詞的序列之后立即獲取該單詞,並計數n + thisWord存在。

因此,一個示例將是這n個(或5個)單詞:

so an to me for

然后第六個可能是and ,因此在原始地圖中它將是:

map.put(arrList,innerMap);

其中,arrList將包含在其中so, an, to, me, for和innerMap看起來像這樣and | 1 and | 1

然后從那里開始,每增加一個新對象,對於相同的序列,它將使整數增加一。 我會擔心第二部分,但是我什至無法使我的代碼正常工作,而且我不知道自己做錯了什么。 這是我的構造函數:

public RandomWriter(Scanner s, int n) {
        Map<String, Integer> valMap = new HashMap<String, Integer>();
        this.s = s;
        this.n = n;
        map = new HashMap<>(); `

        Queue<String> tmp = new LinkedList<String>();
        for (int i = 0; i < n; i++) {
            tmp.add(s.next());
        }

        while (s.hasNext()) {
            ArrayList<String> tmpList = new ArrayList<String>();
            tmpList.addAll(tmp);
            String current = s.next();

            if (!valMap.containsKey(current)) {
                valMap.put(current, 1);

            } else {
                valMap.put(current, valMap.get(current + 1));
            }

            tmp.add(current);
            tmp.remove();
            map.put(tmpList, valMap);
        }

       // s.next();
        System.out.println(map.keySet());
    }

更改為HashMap后,它現在如下打印:

[[today, day], [me, and], [so, if], [in, for], [for, you], [top, in], [sent, receive], [for, many], [on, top], [or, on], [do, do], [side, so], [so, when], [under, on], [many, much], [to, do], [row, on], [for, when], [when, become], [dog, house], [toy, your], [many, is], [won, do], [become, inside], [why, to], [to, spell], [me, you], [school, under], [too, your], [hit, high], [can, do], [is, to], [for, how], [receive, get], [on, me], [animal, your], [to, animal], [inside, to], [your, for], [cat, many], [who, me], [spell, word], [do, cat], [house, live], [how, can], [your, who], [word, sent], [to, today], [canvas, word], [on, for], [low, ball], [which, many], [animal, if], [much, mouse], [live, living], [your, sent], [if, how], [high, kill], [ball, hit], [if, so], [in, side], [get, which], [you, if], [so, for], [do, if], [get, toy], [so, row], [if, to], [to, if], [win, won], [how, when], [inside, in], [dog, squirrel], [you, to], [and, you], [so, or], [word, school], [mouse, for], [for, why], [day, dog], [squirrel, fish], [your, if], [cat, dog], [for, which], [to, when], [more, for], [to, your], [which, cat], [living, canvas], [kill, win], [you, low], [when, inside], [fish, animal], [do, so], [many, more], [to, too], [when, to]]

這是它正在進入的地圖:

private Map<ArrayList<String>, Map<String, Integer>> map;

這是錯誤的:

        if (!valMap.containsKey(current)) {
            valMap.put(current, 1);

        } else {
            valMap.put(current, valMap.get(current + 1));
        }

您可能想要:

        if (!valMap.containsKey(current)) {
            valMap.put(current, 1);

        } else {
            valMap.put(current, valMap.get(current) + 1);
        }

因為如果映射包含鍵current ,那么您想要在該鍵的值上加1而不用鍵current+1的值覆蓋它,甚至可能不存在。

暫無
暫無

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

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