TreeMap 中的 TreeMap,无法从第二张地图中获取价值

TreeMap in TreeMap , can't get value from second map

我有这个任务。 输入是

IP=192.23.30.40 message='Hello&derps.' user=destroyer
IP=192.23.30.41 message='Hello&yall.' user=destroyer
IP=192.23.30.40 message='Hello&hi.' user=destroyer
IP=192.23.30.42 message='Hello&Dudes.' user=destroyer
end

输出为:

child0: 
192.23.33.40 => 1.
child1: 
192.23.30.40 => 1.
destroyer: 
192.23.30.42 => 2.
mother: 
FE80:0000:0000:0000:0202:B3FF:FE1E:8329 => 1.
unknown: 
192.23.155.40 => 1.
yetAnotherUsername: 
192.23.50.40 => 2.

基本上我必须打印每个用户的每个 IP 以及他们发送的消息数量

username: 
IP => count, IP => count… (last must be with dot in end)

问题是我不知道如何添加(代码行异常中的第27行,也不知道为什么)+1到IP。 以及如何捕获最后一个键的最后一个值。

package notReady;

import java.util.Scanner;
import java.util.TreeMap;

/**
 * Created by Philip on 10-Jun-16.
 */
public class Problem09_02_UserLogs {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        TreeMap<String, TreeMap<String, Integer>> map = new TreeMap<>();

        while (true) {
            String in = scanner.nextLine();
            if (in.equals("end")) {
                break;
            }
            String[] input = in.split(" ");
            String ip = input[0].replaceAll("IP=", "");
            String user = input[2].replaceAll("user=", "");

            if (!map.containsKey(user)) {
                map.put(user, new TreeMap<>());
                map.get(user).put(ip, 1);
            }else {
                Integer tempCount = (map.get(user).get(ip)) + 1;
                map.get(user).put(ip, tempCount);
            }
        }

        for (String person : map.keySet()) {
            System.out.printf("%s: \n", person);
            for (String ips : map.get(person).keySet()) {
                System.out.printf("%s => %d.", ips, map.get(person).get(ips));
            }
            System.out.println();
        }
    }
}

这是下一次测试,这里一切正常。 输入:

IP=FE80:0000:0000:0000:0202:B3FF:FE1E:8329 message='Hey&son' user=mother
IP=192.23.33.40 message='Hi&mom!' user=child0
IP=192.23.30.40 message='Hi&from&me&too' user=child1
IP=192.23.30.42 message='spam' user=destroyer
IP=192.23.30.42 message='spam' user=destroyer
IP=192.23.50.40 message='' user=yetAnotherUsername
IP=192.23.50.40 message='comment' user=yetAnotherUsername
IP=192.23.155.40 message='Hello.' user=unknown
end

输出:

destroyer:
192.23.30.40 => 2, 192.23.30.41 => 1, 192.23.30.42 => 1.

我不得不猜测(我不会计算行数以查看第 27 行是什么)但可能是这一行:Integer tempCount = (map.get(user).get(ip)) + 1;。看看您的代码:在上面的 3 行中,您将 empty 树状图放入 map 中,用户是关键。然后你添加一些 ip 的计数。 但是 如果该用户的下一个 ip 是 不同的 您现在将尝试从地图中获取一个不存在的键的值并加 1。

示例:

user = "ThomasGo"
ip1 = "1.2.3.4" 

填满地图后你会得到这样的东西:

Map[key:"ThomasGo"->Map[key:"1.2.3.4"->1]]

现在下面的调用应该会成功:

Integer tempCount = map.get("ThomasGo").get("1.2.3.4") + 1;

但是,考虑下一次调用使用不同的 ip,例如“2.2.2.2”:

Integer tempCount = map.get("ThomasGo").get("2.2.2.2") + 1;

由于内部映射不包含该 ip map.get("ThomasGo").get("2.2.2.2") 将 return 为空。接下来,系统将尝试拆箱 null 值以便将其加 1,但拆箱失败并出现 NullPointerException。

要解决这个问题,您必须检查您是否真的获得了 ip 的计数,如果没有,只需将 1 放入内部映射。

示例:

Map<String, Integer> ipMap = map.get("ThomasGo");

//no map found for the username
if( ipMap == null ) { 
 //create a new inner map and put it into the outer map and keep the reference for further use
 ipMap = new TreeMap<>();
 map.put("ThomasGo", ipMap);
}

//check whether the ip is in the inner map
Integer ipCount = ipMap.get(ip);
if( ipCount == null) {
  //no count in the map so just put 1
  ipMap.put(ip, 1);
} 
else {
  //already a count in the map so overwrite it with count + 1
  ipMap.put(ip, ipCount  + 1);
}

And how to catch last value of last key.

这其实很简单,至少如果我正确理解了您的要求:您不需要。只需打印每个 ip 映射并在除第一个以外的所有之前添加一个逗号(您可以为此使用布尔标志)并在循环之后而不是 System.out.println(); 您调用 System.out.println("."); 打印一个点然后一行休息。