Results 1 to 18 of 18
Like Tree1Likes
  • 1 Post By JosAH

Thread: HashMap key from arrayList

  1. #1
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default HashMap key from arrayList

    I program a lot more these days.
    And I figured out so many things om my own.
    Got my program almost completly finished after 2 weeks of working on it, but this one thing here keeps annoying me right now.
    (And my Java programming buddies are free today and tomorow).

    The problem is:

    I have keys storred in an arrayList.
    But I also have a HashMap with a bunch of keys and some are the same and others aren't.

    Like:
    fileData2a = my arrayList with keys.
    mapFile1 = Hashmap.

    And I would like to know how do I get the ones who are the same printed on my screen for now.
    Then I can process them further.

    Java Code:
     
    				// Get the keys from the arrayList (fileData2a). 
    				for (int i = 0; i < fileData2a.size(); i++) {
    					System.out.println(mapFile1.get(fileData2a));  
    				}
    I only get null null null in return.

    Now I feel so silly for asking this if you would know what huge program I just made..... -_-

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,422
    Rep Power
    20

    Default Re: HashMap key from arrayList

    Quote Originally Posted by Lund01 View Post
    I have keys storred in an arrayList.
    But I also have a HashMap with a bunch of keys and some are the same and others aren't.
    The same instance or semantically equal? If the latter, does the class used for the list and the map keys override Object's equals(...) and hashCode() correctly?

    To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem. Not all your code, just enough to show the problem.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default Re: HashMap key from arrayList

    Get the Set of keys from the HashMap.
    Loops round either them or the List and do a contains() on the other one.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: HashMap key from arrayList

    Quote Originally Posted by Tolls View Post
    Get the Set of keys from the HashMap.
    Loops round either them or the List and do a contains() on the other one.
    Loops, *yuck*; better construct a Set out of that List and retainAll( ... ) elements that are also in the keySet() of that Map; we don't need no stinkin' loops.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default Re: HashMap key from arrayList

    You do realise that the Collection(Collection) constructor iterates over the parameter?
    :)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: HashMap key from arrayList

    Quote Originally Posted by Tolls View Post
    You do realise that the Collection(Collection) constructor iterates over the parameter?
    :)
    It does? Really? I want my money back! Fraud! It's a shameless fraud I'm telling you! Oh, I feel so disappointed ...

    kind regards,

    Jos ;-)
    DarrylBurke likes this.
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default Re: HashMap key from arrayList

    I now have this:

    Java Code:
     
    				for (int i = 0; i < ArrayListFileData2.size(); i++) { 
    
    					// Get the data out of the arrayList. 
    					String ArrayKey2 = ArrayListFileData2.get(i).toString(); 
    					// Get the keys from the HashMap, mapFile1. 
    					Set keys = mapFile1.keySet(); 
    					//System.out.println(keys);
    					
    					if (ArrayKey2.equals(keys)) {
    						System.out.println("Right!");
    					}
    					else {
    						System.out.println("Not");
    					}
    					
    				}
    I now do have found a way to get the keys out of the HashMap.

    But I only get "Not" as the result.
    While some are the same.


  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default Re: HashMap key from arrayList

    You're comparing a String to the Set of keys.
    You want to simply use keys.contains().
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default Re: HashMap key from arrayList

    So I changed that part you said.

    Java Code:
     
    				Set keys = mapFile1.keySet(); 
    				
    				// Get the keys from the arrayList (ArrayListFileData2). 
    				for (int i = 0; i < ArrayListFileData2.size(); i++) { 
    
    					// Get the data out of the arrayList. 
    					String ArrayKey2 = ArrayListFileData2.get(i).toString(); 
    					
    					//
    					if (keys.contains(ArrayKey2)) {
    						System.out.println("Right!");
    					}
    					else {
    						System.out.println("Not"); 
    					}
    					
    				}
    And then I still get more "Not", not one "Right!"

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default Re: HashMap key from arrayList

    What are the keys to the Map?
    And, for that matter, what does the ArrayList contain?

    You really need to use generics.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default Re: HashMap key from arrayList

    I have added some System.out.print() statements, and I think he looks at the whole set of keys if it all matches with a single ArrayKey2.
    Like:
    ArrayKey2 = 4.0
    keys = [4.0, 1.0, 11.0, 48.0]

    Could that maybe also be it?

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default Re: HashMap key from arrayList

    No.
    What are the keys and what does the ArrayList contain?
    If they are Integers then using a String to find them will not work as they will not match....ever.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  13. #13
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default Re: HashMap key from arrayList

    Java Code:
    ArrayList<Cell> ArrayListFileData2 = new ArrayList<Cell>();
    Java Code:
    Map<Cell, XSSFCell> mapFile1 = new HashMap<Cell, XSSFCell>()
    In the arrayList is data that I took from certairn cells from an excel file.
    For that I used Apache POI.

    So both are <Cell>

  14. #14
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,422
    Rep Power
    20

    Default Re: HashMap key from arrayList

    What determines the semantic equality of two Cells? Is it only the value? Check the API for the class.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default Re: HashMap key from arrayList

    You are not comparing Cells though (even if they have valid equals and hascode methods).
    Java Code:
    String ArrayKey2 = ArrayListFileData2.get(i).toString();
    You turned your Cell into a String...so it's never going to match.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  16. #16
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default Re: HashMap key from arrayList

    I changed my code, so now they are both Objects.
    And it grabs one key from the arrayList and it matches that against the keys from the HashMap keys, one at the time.

    Still t won't match.
    Ths drives me crazy...

    Java Code:
    // Get keys from hashMap1
    				Set keys = mapFile1.keySet(); 
    				//
    				Iterator keyIterator = keys.iterator(); 
    				
    				// Get the keys from the arrayList (ArrayListFileData2). 
    				for (int i = 0; i < ArrayListFileData2.size(); i++) { 
    					
    					//System.out.print(ArrayListFileData2.get(i) + " ");
    
    					Object ArrayKey2 = ArrayListFileData2.get(i); 
    
    					//System.out.println(keys);
    					
    					while(keyIterator.hasNext()) {
    						Object keyObj = keyIterator.next();
    						System.out.println( "Key: " + keyObj);
    						
    						if (ArrayKey2.equals(keyObj)) {
    							System.out.println("Ja"); 
    						}
    						else {
    							System.out.println("Nee");
    						}
    					}
    All but one should have as output "Ja".
    But everything is still "Nee".
    Last edited by Lund01; 09-13-2012 at 11:16 AM.

  17. #17
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default Re: HashMap key from arrayList

    Use GENERICS!
    For gods sake.
    That would solve any potential mismatches between types.
    Then we would at least know what these types were when looking at your code.
    It's not as if the compiler won't be warning you.

    Now assuming these are Cells, then you probably need to answer Darryl's question. What makes a Cell equal?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  18. #18
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default Re: HashMap key from arrayList

    I added the generics.
    And now I have it working for one part.
    It does match and it does that coorectly.
    Only thing is, it only grabs the first entry from the arrayList.
    So I only have to solve that part now.

    Thanks for the help. :)

Similar Threads

  1. Replies: 0
    Last Post: 02-27-2012, 03:39 AM
  2. Replies: 12
    Last Post: 07-07-2011, 09:49 PM
  3. how to read the ArrayList inside HashMap
    By koddy in forum New To Java
    Replies: 6
    Last Post: 07-15-2010, 02:41 PM
  4. Replies: 7
    Last Post: 12-08-2009, 08:17 PM
  5. ArrayList into hashMap
    By Preethi in forum New To Java
    Replies: 2
    Last Post: 02-11-2008, 09:13 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
  •