HashMap with multiple values under the same key

前端 未结 22 2089
旧时难觅i
旧时难觅i 2020-11-22 06:49

Is it possible for us to implement a HashMap with one key and two values. Just as HashMap?

Please do help me, also by telling (if there is no way) any other way to

22条回答
  •  一个人的身影
    2020-11-22 06:56

    You could:

    1. Use a map that has a list as the value. Map>.
    2. Create a new wrapper class and place instances of this wrapper in the map. Map.
    3. Use a tuple like class (saves creating lots of wrappers). Map>.
    4. Use mulitple maps side-by-side.

    Examples

    1. Map with list as the value

    // create our map
    Map> peopleByForename = new HashMap<>();    
    
    // populate it
    List people = new ArrayList<>();
    people.add(new Person("Bob Smith"));
    people.add(new Person("Bob Jones"));
    peopleByForename.put("Bob", people);
    
    // read from it
    List bobs = peopleByForename["Bob"];
    Person bob1 = bobs[0];
    Person bob2 = bobs[1];
    

    The disadvantage with this approach is that the list is not bound to exactly two values.

    2. Using wrapper class

    // define our wrapper
    class Wrapper {
        public Wrapper(Person person1, Person person2) {
           this.person1 = person1;
           this.person2 = person2;
        }
    
        public Person getPerson1 { return this.person1; }
        public Person getPerson2 { return this.person2; }
    
        private Person person1;
        private Person person2;
    }
    
    // create our map
    Map peopleByForename = new HashMap<>();
    
    // populate it
    Wrapper people = new Wrapper();
    peopleByForename.put("Bob", new Wrapper(new Person("Bob Smith"),
                                            new Person("Bob Jones"));
    
    // read from it
    Wrapper bobs = peopleByForename.get("Bob");
    Person bob1 = bobs.getPerson1;
    Person bob2 = bobs.getPerson2;
    

    The disadvantage to this approach is that you have to write a lot of boiler-plate code for all of these very simple container classes.

    3. Using a tuple

    // you'll have to write or download a Tuple class in Java, (.NET ships with one)
    
    // create our map
    Map peopleByForename = new HashMap<>();
    
    // populate it
    peopleByForename.put("Bob", new Tuple2(new Person("Bob Smith",
                                           new Person("Bob Jones"));
    
    // read from it
    Tuple bobs = peopleByForename["Bob"];
    Person bob1 = bobs.Item1;
    Person bob2 = bobs.Item2;
    

    This is the best solution in my opinion.

    4. Multiple maps

    // create our maps
    Map firstPersonByForename = new HashMap<>();
    Map secondPersonByForename = new HashMap<>();
    
    // populate them
    firstPersonByForename.put("Bob", new Person("Bob Smith"));
    secondPersonByForename.put("Bob", new Person("Bob Jones"));
    
    // read from them
    Person bob1 = firstPersonByForename["Bob"];
    Person bob2 = secondPersonByForename["Bob"];
    

    The disadvantage of this solution is that it's not obvious that the two maps are related, a programmatic error could see the two maps get out of sync.

提交回复
热议问题