简体   繁体   English

关于在Java中选择合适的数据结构的问题

[英]questions about choosing suitable data structure in Java

My problem is that there are 3 variables, say a,b,c 我的问题是有3个变量,比如a,b,c

First, information a,b,c needed to be input and stored. 首先,需要输入和存储信息a,b,c。

For example a=Tom, b=school A, c=1402 例如a = Tom,b =学校A,c = 1402

Then, I can get the above information by entering "Tom", in this case, if there is another Tom in school B,that message would also be printed out. 然后,我可以通过输入“Tom”来获取上述信息,在这种情况下,如果学校B中有另一个Tom,那么该消息也将被打印出来。

or by entering "Tom, school A", then only that Tom would be printed out. 或者输入“Tom,A学校”,然后才会打印出Tom。 In this problem, their would be no duplicates if a and b are both locked. 在这个问题中,如果a和b都被锁定,它们将不会重复。

Well, my view is that a must be a key, and a+b can be a composite key. 好吧,我的观点是必须是一个键,而一个+ b可以是一个复合键。 So firstly I thought of a HashMap, however, HashMap has only one key to one value. 首先我想到了一个HashMap,但是,HashMap只有一个键到一个值。 So I intended to use 2 maps but this could not make use of composite key. 所以我打算使用2张地图,但这不能使用复合键。

So are my thoughts correct? 我的想法是否正确? Or are there any better data collections in Java could be used in this case? 或者,在这种情况下,是否可以使用Java中更好的数据集合?

Thank for your time! 谢谢你的时间!

There is no off the shelf data structure in Java for your need. Java中没有现成的数据结构供您根据需要使用。 There are multi valued map implementations in Apache Commons and Google Guava Collections but not in core Java. Apache Commons和Google Guava Collections中有多值的地图实现,但核心Java中却没有。

But , In my opinion, you can implement it with a map declaration like - Map<String, List<String>> or Map<String, List<Student>> depending on whether you are keeping your student details as concatenated strings or as a Student class. 但是,在我看来,您可以使用地图声明来实现它,例如 - Map<String, List<String>>Map<String, List<Student>>具体取决于您是将学生详细信息保存为连接字符串还是Student班。

While populating the map - you will make two entries in map for first row / student , once for a and another entry for a+b concatenated. 在填充地图时 - 您将在地图中为第一行/学生创建两个条目,一个用于a和另一个用于a+b连接的条目。

Then while making entries for subsequent rows , you should first check if key exists in map or not, if it exists, you get the value , append new value and store again. 然后在为后续行创建条目时,首先应检查映射中是否存在键,如果存在,则获取值,追加新值并再次存储。

Record a=Tom, b=school A, c=1402 will have two entries then comes record a=Tom, b=school B, c=1403 and you will append record for key = Tom and add a new entry for TomschoolB so in total three entries in map for keys - Tom , TomschoolA and TomschoolB . 记录a=Tom, b=school A, c=1402将有两个条目然后记录a=Tom, b=school B, c=1403并且你将追加key = Tom记录并为TomschoolB添加一个新条目所以地图中共有三个条目用于密钥 - TomTomschoolATomschoolB Key Tom will have two items in value list while rest two will have single items. 密钥Tom将在值列表中有两个项目,而其余两个将具有单个项目。

Your lookup code should follow key calculation logic as key creation logic ie string concatenation or any other things as you wish. 您的查找代码应遵循密钥计算逻辑作为密钥创建逻辑,即字符串连接或任何其他您希望的事情。

This is just give a rough idea that multiple values can be handled via list. 这只是粗略地了解可以通过列表处理多个值。

Edit : From your comments, you look confused so below is code to implement the idea. 编辑:从你的评论中,你看起来很困惑,所以下面是实现这个想法的代码。 You can tweak and extend it as you wish. 您可以根据需要调整和扩展它。

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MultiValuedMap {


    private static Map<String,List<String>> storage = new HashMap<>();

    public static void main(String[] args) {

        store("Tom","Tom,schoolA,1402");
        store("TomschoolA","Tom,schoolA,1402");
        store("Tom","Tom,schoolB,1402");
        store("TomschoolB","Tom,schoolB,1403");

        storage.forEach((key,value) -> System.out.println("key:"+key+",value:"+value));

    }

    private static void store(String key, String value){

        if(storage.containsKey(key)) {
            List<String> newList = storage.get(key);
            newList.add(value);
            storage.put(key,newList);
        }else{
            List<String> values = new ArrayList<String>();
            values.add(value);
            storage.put(key,values);
        }

    }

}

Output: 输出:

key:Tom,value:[Tom,schoolA,1402, Tom,schoolB,1402]
key:TomschoolB,value:[Tom,schoolB,1403]
key:TomschoolA,value:[Tom,schoolA,1402]

当您对使用任何现有数据结构感到不明确时,我建议您创建自己的数据结构。您可以根据需要创建任何方法和任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM