Results 1 to 18 of 18
Thread: HashMap key from arrayList
- 09-11-2012, 02:28 PM #1
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.
I only get null null null in return.Java Code:// Get the keys from the arrayList (fileData2a). for (int i = 0; i < fileData2a.size(); i++) { System.out.println(mapFile1.get(fileData2a)); }
Now I feel so silly for asking this if you would know what huge program I just made..... -_-
- 09-11-2012, 03:47 PM #2
Re: HashMap key from arrayList
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 09-11-2012, 04:59 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 09-11-2012, 05:52 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Re: HashMap key from arrayList
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 09-11-2012, 06:13 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 09-11-2012, 08:26 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
- 09-12-2012, 10:42 AM #7
Re: HashMap key from arrayList
I now have this:
I now do have found a way to get the keys out of the HashMap.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"); } }
But I only get "Not" as the result.
While some are the same.
- 09-12-2012, 11:03 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 09-12-2012, 11:15 AM #9
Re: HashMap key from arrayList
So I changed that part you said.
And then I still get more "Not", not one "Right!"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"); } }
- 09-12-2012, 11:27 AM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 09-12-2012, 12:42 PM #11
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?
- 09-12-2012, 01:44 PM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 09-12-2012, 02:10 PM #13
Re: HashMap key from arrayList
Java Code:ArrayList<Cell> ArrayListFileData2 = new ArrayList<Cell>();
In the arrayList is data that I took from certairn cells from an excel file.Java Code:Map<Cell, XSSFCell> mapFile1 = new HashMap<Cell, XSSFCell>()
For that I used Apache POI.
So both are <Cell>
- 09-12-2012, 02:26 PM #14
Re: HashMap key from arrayList
What determines the semantic equality of two Cells? Is it only the value? Check the API for the class.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 09-12-2012, 05:34 PM #15
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: HashMap key from arrayList
You are not comparing Cells though (even if they have valid equals and hascode methods).
You turned your Cell into a String...so it's never going to match.Java Code:String ArrayKey2 = ArrayListFileData2.get(i).toString();
Please do not ask for code as refusal often offends.
- 09-13-2012, 10:08 AM #16
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...
All but one should have as output "Ja".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"); } }
But everything is still "Nee".Last edited by Lund01; 09-13-2012 at 10:16 AM.
- 09-13-2012, 10:24 AM #17
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 09-13-2012, 11:10 AM #18
Similar Threads
-
Issue adding elements to an ArrayList while looping through a HashMap
By vb89 in forum New To JavaReplies: 0Last Post: 02-27-2012, 02:39 AM -
ArrayList copy some of the element from one arraylist tnto another arraylist
By ralf in forum New To JavaReplies: 12Last Post: 07-07-2011, 08:49 PM -
how to read the ArrayList inside HashMap
By koddy in forum New To JavaReplies: 6Last Post: 07-15-2010, 01:41 PM -
How to create a new HashMap from a HashMap entries of other methods
By pandeyalok in forum Advanced JavaReplies: 7Last Post: 12-08-2009, 07:17 PM -
ArrayList into hashMap
By Preethi in forum New To JavaReplies: 2Last Post: 02-11-2008, 08:13 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks