Results 1 to 13 of 13
  1. #1
    greet_java is offline Member
    Join Date
    Jan 2015
    Posts
    7
    Rep Power
    0

    Question Hash of hashmap - create arraylist in hash

    Hi,

    Following code does what i want. However, i do not know if second hash values. Here i simulated manually creating arr . I get arr List from another method/variable. ( it contains duplicates ie same type SATA, IDE occurs multiple times etc so i need to consider it , i guess).

    I would like to know if there is any way ,
    1) i can create Arraylist , inside a hash and ( get & add to the list , from another variable for example )
    2) While creating Arraylist , need to avoid duplicates as well . ( correct me, if i am wrong ).
    3) compare the values at the end ( like this : sourceInp.get("temp").get(arr[0]) == sourceInp.get("perm").get(arr[0]) ) .
    I have one problem here , HashMap will not be in same order , so if i have two it is ok. what if i have list with 4 ?

    Java Code:
    Map<String, Map<String, Integer>> sourceInp = new HashMap<String, Map<String, Integer>>();
    sourceInp.put("perm",new HashMap<String, Integer>());
    sourceInp.get("perm").put("SATA", 12);
    sourceInp.get("perm").put("IDE", 10);
    sourceInp.put("temp",new HashMap<String, Integer>());
    sourceInp.get("temp").put("IDE", 55);
    sourceInp.get("temp").put("SATA", 44);
    sourceInp.put("hddType",new HashMap<String, Integer>());
    sourceInp.get("hddType").put("IDE", 12);
    sourceInp.get("hddType").put("SATA", 10);
    String[] arr = new String[2];
    arr[0] = "SATA";
    arr[1] = "IDE";
    System.out.println("value " + sourceInp.get("temp").get(arr[0]));
    System.out.println("value " + sourceInp.get("temp").get(arr[1]));
    // <For ex. check above mentioned values are same or not >. TBD
    // sourceInp.get("temp").get(arr[0]) == sourceInp.get("perm").get(arr[0]) // Note temp , perm here.
    i get correct output:
    value 44
    value 55
    Note:
    String[] arr = new String[3];
    arr[0] = "SATA";
    arr[1] = "IDE";
    arr[2] = "SATA";
    It is accepting the arr[2] .

    Thanks in advance.
    Last edited by greet_java; 10-02-2016 at 02:33 PM. Reason: Updated comparision code.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Hash of hashmap - create arraylist in hash

    i can create Arraylist , inside a hash and
    Yes an ArrayList could be the value stored in a HashMap

    While creating Arraylist , need to avoid duplicates as well
    Test if the ArrayList contains a value before adding a duplicate.

    HashMap will not be in same order
    Yes, the order of HashMaps is not in sequence. However the ArrayLists that are the values in the HashMap can be ordered as desired.

    Another option would be to save the values in a SortedSet - in order and no duplicates
    Last edited by Norm; 10-02-2016 at 02:58 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    greet_java is offline Member
    Join Date
    Jan 2015
    Posts
    7
    Rep Power
    0

    Default Re: Hash of hashmap - create arraylist in hash

    Java Code:
    Map<String, Map<TreeSet<String>, Integer>> mainhash = new HashMap<String, Map<TreeSet<String>, Integer>>();
    TreeSet<String> setx = new TreeSet<String>(); 
    mainhash.put("perm",new HashMap<setx, Integer>());
    i get following error in eclipse :

    - setx cannot be resolved to a type
    - The method put(String, Map<TreeSet<String>,Integer>) in the type Map<String,Map<TreeSet<String>,Integer>> is not applicable for the arguments (String,
    HashMap<setx,Integer>)

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Hash of hashmap - create arraylist in hash

    setx cannot be resolved to a type
    setx is not a type, its a variable. Replace setx with a valid type.
    Last edited by Norm; 10-02-2016 at 06:22 PM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    greet_java is offline Member
    Join Date
    Jan 2015
    Posts
    7
    Rep Power
    0

    Default Re: Hash of hashmap - create arraylist in hash

    when I add
    Java Code:
    mainhash.put("perm",new HashMap<TreeSet<String>, Integer>());
    mainhash.get("perm").put("SATA", 12);
    I get the following:
    The method put(TreeSet<String>, Integer) in the type Map<TreeSet<String>,Integer> is not applicable for the arguments (String, int)
    I am not sure , how to add values in TreeSet . Hence I tried with variable which seems wrong.
    Can you share some example and link for some tutorial on this subject ?

    PS:
    I had hard time to login when I choose Reply button to add a comment . May be it is just random.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Hash of hashmap - create arraylist in hash

    The method put(TreeSet<String>, Integer) in the type Map<TreeSet<String>,Integer> is not applicable for the arguments (String, int)
    The args in the put need to be a TreeSet not a String

    To see what get() returns, print its class:
    Java Code:
     System.out.println(mainhash.get("perm").getClass());
    The HashMap is supposed to contain a TreeSet, Define one and add it to mainhash with the put
    Last edited by Norm; 10-02-2016 at 08:26 PM.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    greet_java is offline Member
    Join Date
    Jan 2015
    Posts
    7
    Rep Power
    0

    Default Re: Hash of hashmap - create arraylist in hash

    Hi,

    " Define one and add it to mainhash with the put"
    Did you mean below one ?
    Java Code:
    mainhash.put("perm",new HashMap<TreeSet<String>, Integer>());
    int x=0;
    mainhash.get("perm").put(new TreeSet<String>(), x); # no errors 
    mainhash.get("perm").getClass();
    mainhash.get("perm").put("IDE", 10); # NOK
    I still get the following when i try to add the values in TreeSet.
    "The method put(TreeSet<String>, Integer) in the type Map<TreeSet<String>,Integer> is not applicable for the arguments (String, int)"
    I am not getting how to define a type as you said and add the values later on.
    getclass gives class name . i didn't get your intention here.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Hash of hashmap - create arraylist in hash

    how to define a type as you said and add the values later on.
    Java Code:
      AType<args>  aType = new AType<>();   // define an instance
      ...
      aType.add(somethingValue); // add values later
    aType could be added to a Map.


    The put in line 3 has a TreeSet for the key and that works
    The put in line 5 has a String for the key and the compiler flags that
    Change line 5 to use a TreeSet for the key like in line 3

    Having a TreeSet for a key makes no sense to me. I think of keys as being immutable. A Set usually has elements being added to it.
    Last edited by Norm; 10-02-2016 at 10:10 PM.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Hash of hashmap - create arraylist in hash

    Quote Originally Posted by Norm View Post
    Having a TreeSet for a key makes no sense to me. I think of keys as being immutable. A Set usually has elements being added to it.
    Yep! If you add elements to the set, then the hashcode changes and thus, the key changes so you won't be able to retrieve the elements.
    It could be made to work by overriding hashCode and equals method of the set and using their default implementations
    which are based on the actual reference as opposed to the contents of the set. However, that is merely an academic suggestion/discussion.
    I think the OP needs to rethink the design. In general, mutable keys are a bad idea as they can lead to hard to find bugs.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    greet_java is offline Member
    Join Date
    Jan 2015
    Posts
    7
    Rep Power
    0

    Default Re: Hash of hashmap - create arraylist in hash

    Hi,

    I misinterpret what Norm said before. i got it better now , i guess.
    Java Code:
    mainhash.put("perm",new HashMap<TreeSet<String>, Integer>());
    TreeSet<String> aSet = new TreeSet<String>();
    aSet.add("IDE");
    aSet.add("XYZ");
    mainhash.get("perm").put(aSet, 11);
    mainhash.get("perm").put(aSet, 12);
    System.out.println("looking in temp " + mainhash.get("perm").get("IDE"));
    System.out.println(mainhash.get("perm").get(aSet));
    However this is not what i wanted . Moreover it is not correct , i can see it is overwriting it.
    I go back to my post#1 for reference as follows:
    Java Code:
    Map<String, Map<String, Integer>> sourceInp = new HashMap<String, Map<String, Integer>>();
    sourceInp.put("perm",new HashMap<String, Integer>());
    sourceInp.get("perm").put("SATA", 12);
    sourceInp.get("perm").put("IDE", 10);
    sourceInp.put("temp",new HashMap<String, Integer>());
    sourceInp.get("temp").put("IDE", 55);
    sourceInp.get("temp").put("SATA", 44);
    From above mentioned code sample, i want to compare the values as follows:
    Java Code:
    sourceInp.get("temp").get(??) == sourceInp.get("perm").get(??)  # Here ?? meant for SATA
    sourceInp.get("temp").get(??) == sourceInp.get("perm").get(??)  # Here ?? meant for IDE
    How can i achieve that when i don't know what values will be in place of ?? . Is it possible to get like array index of 0 ,1 in array for ex ?
    I know that second map contains only 2 values ie SATA, IDE . ( for ex )
    Last edited by greet_java; 10-02-2016 at 11:20 PM. Reason: Updated text

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Hash of hashmap - create arraylist in hash

    Can you please explain, not using code, what you want to do? What type of data do you have and how to you want to use it?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    greet_java is offline Member
    Join Date
    Jan 2015
    Posts
    7
    Rep Power
    0

    Default Re: Hash of hashmap - create arraylist in hash

    Hi,
    I want to compare the values from keys of map in a map.
    Assuming, i do not know the key name for the second Map.
    In that case, how do i compare them and print if they are same or not.
    First Map contains, String as a key. (second) Map as value.
    Second Map contains, String as a key. Integer as a Value.
    Hope it is clear.

    Hope below code explains , what i wrote above ( except comparision ):
    Java Code:
    Map<String, Map<String, Integer>> sourceInp = new HashMap<String, Map<String, Integer>>();
    sourceInp.put("perm",new HashMap<String, Integer>());
    sourceInp.get("perm").put("SATA", 12);
    sourceInp.get("perm").put("IDE", 10);
    sourceInp.put("temp",new HashMap<String, Integer>());
    sourceInp.get("temp").put("IDE", 55);
    sourceInp.get("temp").put("SATA", 44);
    or please see my post#1 for complete code i tried to simulate by creating list manually.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Hash of hashmap - create arraylist in hash

    Can you explain what you want to do without using the names of any java classes or any code?
    Quote Originally Posted by jim829
    What type of data do you have and how to you want to use it?
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. trying to create a hash map
    By wildcardcrow in forum New To Java
    Replies: 9
    Last Post: 05-05-2014, 11:47 PM
  2. Hash
    By CodeX Pro in forum New To Java
    Replies: 7
    Last Post: 06-07-2013, 05:32 PM
  3. Replies: 9
    Last Post: 11-12-2012, 11:38 AM
  4. Hash set
    By lakshmibvaraprasad in forum New To Java
    Replies: 2
    Last Post: 07-20-2011, 06:58 PM
  5. Hash
    By sandy1028 in forum New To Java
    Replies: 4
    Last Post: 04-17-2009, 10:36 AM

Posting Permissions

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