简体   繁体   中英

questions about choosing suitable data structure in Java

My problem is that there are 3 variables, say a,b,c

First, information a,b,c needed to be input and stored.

For example a=Tom, b=school 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.

or by entering "Tom, school A", then only that Tom would be printed out. In this problem, their would be no duplicates if a and b are both locked.

Well, my view is that a must be a key, and a+b can be a composite key. So firstly I thought of a HashMap, however, HashMap has only one key to one value. So I intended to use 2 maps but this could not make use of composite key.

So are my thoughts correct? Or are there any better data collections in Java could be used in this case?

Thank for your time!

There is no off the shelf data structure in Java for your need. There are multi valued map implementations in Apache Commons and Google Guava Collections but not in core 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.

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.

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 . Key Tom will have two items in value list while rest two will have single items.

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]

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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