Results 1 to 10 of 10
Like Tree5Likes
  • 1 Post By KevinWorkman
  • 1 Post By zFollette
  • 1 Post By jim829
  • 1 Post By JosAH
  • 1 Post By kjkrum

Thread: How to store three variables in a HashMap?

  1. #1
    gorgon777 is offline Member
    Join Date
    Jan 2014
    Posts
    15
    Rep Power
    0

    Default How to store three variables in a HashMap?

    Hello everyone,

    So I just want to store a Key in a HashMap which can related to two values. For example, the Key "ABC" related to "Fire" which in turn relates to "Heat".

    How can I code this in a HashMap?

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,960
    Rep Power
    8

    Default Re: How to store three variables in a HashMap?

    You could use a map of maps: Map<String, Map<String, String>>

    Although, if the key "ABC" will only ever relate to a single value "Fire" which relates to a single value "Heat", you might be better off using something closer to a LinkedList.
    gorgon777 likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: How to store three variables in a HashMap?

    You could make a custom object. I.E:

    Java Code:
    public class NewMap<A, B, C> {
    
    }
    And organize A's, B's, and C's with either Sets or ArrayLists I.E:

    Java Code:
    private Set<A> aSet = new HashSet<>();
    private ArrayList<B> bList = new ArrayList<>();
    ..etc
    gorgon777 likes this.
    1000011 1100001 1101110 100000 1111001 1101111 1110101 100000 1110010 1100101 1100001 1100100 100000 1000010 1101001 1101110 1100001 1110010 1111001 111111

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: How to store three variables in a HashMap?

    After reading this a second time, do you want to store one key which refers to another key which refers to a value? Or do you simply want to store multiple values in a map for the same key? If the latter, you can have the value of the key be an array list.

    Java Code:
    Map<String, List<String>> m = new HashMap<>();
    List<String> list = new ArrayList<>();
    list.add("fire");
    list.add("heat");
    m.put("ABC", list);
    Regards,
    Jim
    gorgon777 likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to store three variables in a HashMap?

    Quote Originally Posted by zFollette View Post
    Java Code:
    private Set<A> aSet = new HashSet<>();
    private ArrayList<B> bList = new ArrayList<>();
    ..etc
    Your generics are incorrect.

    kind regards,

    Jos
    gorgon777 likes this.
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    gorgon777 is offline Member
    Join Date
    Jan 2014
    Posts
    15
    Rep Power
    0

    Default Re: How to store three variables in a HashMap?

    Quote Originally Posted by zFollette View Post
    You could make a custom object. I.E:

    Java Code:
    public class NewMap<A, B, C> {
    
    }
    And organize A's, B's, and C's with either Sets or ArrayLists I.E:

    Java Code:
    private Set<A> aSet = new HashSet<>();
    private ArrayList<B> bList = new ArrayList<>();
    ..etc
    This is ideally what I wanted to do, but I figured I could still do this in one HashMap<String, String> object by combining A and B into one key, and referencing it to C.

    Some something like this allows me to reference uniquely each C value:
    Java Code:
    String tempName = stringParameter.substring(0, stringParameter.length()-4);
    map.put(tempName + "-" + params[0], params[1]);
    						
    System.out.println(tempName + "-" + params[0] + ", " + params[1]);
    Thank you all for your suggestions:)

  7. #7
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: How to store three variables in a HashMap?

    Quote Originally Posted by JosAH View Post
    Your generics are incorrect.

    kind regards,

    Jos
    How so? Is it the fact that I made bList of type 'ArrayList' instead of type 'List'? I do not know anything about generics.
    1000011 1100001 1101110 100000 1111001 1101111 1110101 100000 1110010 1100101 1100001 1100100 100000 1000010 1101001 1101110 1100001 1110010 1111001 111111

  8. #8
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: How to store three variables in a HashMap?

    Quote Originally Posted by zFollette View Post
    How so? Is it the fact that I made bList of type 'ArrayList' instead of type 'List'? I do not know anything about generics.
    Your generics were incorrect because you specified a formal type parameter as an actual type. So it should be something like this:

    Java Code:
    class Foo<T> { // T is a formal type parameter
       private T val;
       public T getValue() {
           return val;
       }
       public void setValue(T val) {
           this.val = val;
       }
    }
    
    // Then later...
    
    Foo<Integer> foo = new Foo<>();
    foo.setValue(10);
    Integer a = foo.getValue();
    
    // or
    
    Foo<String> stringFoo = new Foo<>();
    stringFoo.setValue("Hello");
    Generics are extremely useful and sort of fun to write but not always intuitive. They are used to enforce proper type handling at compile time, thus mitigating errors that otherwise might have occurred at run time. I would recommend you taking the time to learn about them.

    Regards,
    Jim
    Last edited by jim829; 02-12-2014 at 03:16 AM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: How to store three variables in a HashMap?

    Quote Originally Posted by zFollette View Post
    You could make a custom object. I.E:

    Java Code:
    public class NewMap<A, B, C> {
    
    }
    It would be extremely confusing to name such a class SomethingMap. Map is a well-known interface, and any maintenance programmer who sees a class called SomethingMap is going to assume it's a Map.
    zFollette likes this.
    Get in the habit of using standard Java naming conventions!

  10. #10
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: How to store three variables in a HashMap?

    Quote Originally Posted by kjkrum View Post
    It would be extremely confusing to name such a class SomethingMap. Map is a well-known interface, and any maintenance programmer who sees a class called SomethingMap is going to assume it's a Map.
    Well it was just an example. I think OP gets the point.
    1000011 1100001 1101110 100000 1111001 1101111 1110101 100000 1110010 1100101 1100001 1100100 100000 1000010 1101001 1101110 1100001 1110010 1111001 111111

Similar Threads

  1. Store methods (or pointers) in variables?
    By Xyexs in forum New To Java
    Replies: 6
    Last Post: 11-12-2013, 08:49 AM
  2. Replies: 7
    Last Post: 11-04-2012, 11:04 PM
  3. How To Store Variables In A Text File That Aren't Constant
    By anthropamorphic in forum New To Java
    Replies: 6
    Last Post: 08-10-2011, 11:56 PM
  4. Store links in text file and read into variables
    By africanhacker in forum New To Java
    Replies: 1
    Last Post: 03-31-2011, 05:18 PM
  5. Replies: 4
    Last Post: 06-09-2008, 04:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •