简体   繁体   English

如何将我从文件中读取的数据放入哈希图<String, Set<String> &gt;?

[英]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:您的代码还有其他几个可能的问题:

  1. You are splitting each line into four components but your example data only has two?您将每一行分成四个部分,但您的示例数据只有两个?

  2. 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类应该正确实现hashCodeequals 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.

相关问题 如何从文件中读取信息并将其放回字符串数组? - How to read information from file and put it back into String array? 读取并存储带有字符串的文本文件,并将数据浮动到哈希图中 - Read and Store text file with string and float data into a hashmap 如何将文件中的多行读入字符串以放入jTextArea? - How do i read multiple lines from a file into a string to put in jTextArea? 如何从文件中读取字符串,将其拆分为字符并放入2D数组中? - How can I read a String from a file split it into characters and put it in to a 2D array? Java - 从文本文件中读取并将 item( string , arrayList) 添加到 hashmap - Java -read from a text file and Add item( string , arrayList) to hashmap 从HashMap检索数据 <String, ArrayList<HashMap<String, String> &gt;&gt; - Retrieve data from HashMap<String, ArrayList<HashMap<String, String>>> 如何读取文件,以便对于包含特定字符串的每一行,将下面的行保存到 HashMap? - How do I read in a file such that for each line that contains a certain string, save the lines underneath to a HashMap? 如何从文件中读取字符串并拆分以获取数据字符串? - How to read a string from file and split to get a data string? 如何将txt文件读取到字节[]和字节[]到Hashmap <String, Object> ? - how to read txt file to byte [] and byte[] to Hashmap<String, Object>? 如何将哈希图放入数组中? conf [0] =新的HashMap <String, Object> (); - How can I put a hashmap inside an array? conf[0] = new HashMap<String, Object>();
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM