[英]How to put data i read from file to hashmap <String, Set<String>>?
I need to store data from file i have read, the data structure is: ip adress and id (1.10.186.214;2812432), and id can have multiple ip.我需要存储我读过的文件中的数据,数据结构是:ip地址和id(1.10.186.214;2812432),id可以有多个ip。
here is the code i use to read file.这是我用来读取文件的代码。 I use Treemap userdb to store information about users.我使用 Treemap userdb 来存储有关用户的信息。 Also i need another map to store id and ip.我还需要另一个地图来存储 id 和 ip。
File file = new File(path);
Scanner scanner = new Scanner(file);
Map<String, User> userdb = new TreeMap<>();
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String ip = line.split(";")[0];
String id = line.split(";")[1];
String fio = line.split(";")[2];
String adress = line.split(";")[3];
User user = new User(id, fio, adress);
userdb.put(id, user);
}
scanner.close();
I have decided to use Hashmap id as key and set of ip as value.我决定使用 Hashmap id 作为键,使用一组 ip 作为值。 Is this the right way?这是正确的方法吗? And if it is not, what are the other options?如果不是,还有什么其他选择?
You already have the User
that the IP address belongs to I would suggest you use that as the key for your second data structure:您已经拥有 IP 地址所属的User
,我建议您将其用作第二个数据结构的键:
Map<User, Set<InetAddress>> mappings = new HashMap<>();
(I have used InetAddress
here but you may already have a class for this?) (我在这里使用了InetAddress
,但您可能已经有一个用于此的类?)
You can then look up or create the set of IP addresses for a given user and add the new entry:然后,您可以查找或创建给定用户的 IP 地址集并添加新条目:
InetAddress address = InetAddress.getByName(ip);
mappings.computeIfAbsent(user, ignored -> new HashSet<>()).add(address);
Your code has a couple of other possible issues:您的代码还有其他几个可能的问题:
You are splitting each line into four components but your example data only has two?您将每一行分成四个部分,但您的示例数据只有两个?
You create a new user for every entry when you have stated that a user can have multiple IP addresses (which is the point of the second data structure).当您声明一个用户可以拥有多个 IP 地址(这是第二个数据结构的点)时,您将为每个条目创建一个新用户。 You want to check whether you already have the user in userdb
first, ie:您要先检查userdb
是否已经有该用户,即:
final User user;
if(users.containsKey(id)) {
user = users.get(id);
}
else {
user = new User(id);
users.put(id, user);
}
(You could use the computeIfAbsent
approach again here to simply the logic). (您可以在computeIfAbsent
再次使用computeIfAbsent
方法来简化逻辑)。
Your User
class should correctly implement hashCode
and equals
if you decide to use it as the key.如果您决定使用它作为键,您的User
类应该正确实现hashCode
和equals
。 Alternative #1 just the user ID as the key.替代方案 #1 只是用户 ID 作为密钥。 Alternative #2 move the Set<InetAddress>
into the User
class where it logically lives anyway.备选方案#2 将Set<InetAddress>
移动到它逻辑上无论如何都存在的User
类中。
Finally there's not much point splitting the line four times, it won't matter for this exercise I imagine but it's a good habit to get into: final String[] parts = line.split(";"); String ip = parts[0]; ...
最后,将线分割四次没什么意义,我想这对这个练习来说无关紧要,但养成一个好习惯: final String[] parts = line.split(";"); String ip = parts[0]; ...
final String[] parts = line.split(";"); String ip = parts[0]; ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.