Mapping a set to a string
Hello,
I wrote a code that takes a text file and split it in 2 parts. I'm storing the first part as the key while the other part is stored as the value. It all worked fine until I had to deal with a text file that resulted in multiple same keys with different values. Now when I try to get the value of one of the duplicated keys, I don't get the correct value.
my map looks something like this:
map.add( "1" , "2")
map.add("1" , "3")
map.add("1", "4")
so when I want to retrieve the value of the first key, I don't get 2, I get 4.
I'm looking for a way in which I can eliminate duplicated keys and map the one remaining to a set of strings that holds all the possible values for that key. Is there any clue of how I can do that after inputting the text file and splitting its content?
Re: Mapping a set to a string
The first thought that comes to mind is storing a LinkedList instead of a String. It's a bit more complicated though; instead of just adding, you'd have to get the LinkedList from the map, and if none existed, create a new one and add it to the map. Then add the new String to the LinkedList. There's no reason to add the LinkedList back to the map, since it's already stored there, although it doesn't hurt anything if you do anyway.
Re: Mapping a set to a string
Thank you for your reply! Can you explain it a little more.
Here is the code I'm using to add the keys and values to the map:
Quote:
while ((strLine = br.readLine()) != null) {
//System.out.println(strLine);
String [] splitOut = strLine.split(":");
//Adding the ID & value to the map
value.add(splitOut[1]);
key.add(splitOut[0]);
ID_MAP.put( splitOut[1] , splitOut[0]);
}
Re: Mapping a set to a string
What is the class of ID_MAP? Is it something you've done yourself, or is it one of the standard map classes?
Re: Mapping a set to a string
Any given key can only map to one value. The way to solve your problem is to have a map of arrays of type String. So when you have a specific key, you either grab the array at the location and add the string, create the array, and add the string and then store it in the map.
Here would be an example declaration:
Code:
Map<String,List<String>> map = new HashMap<>();
Regards,
Jim
Re: Mapping a set to a string
Thanks Jim for your answer! This is what I'm trying to do. But I'm having difficulty creating the list that will map to that one key. should I first add the keys and values to a map and then loop through the map to check for duplicated keys and then store the values is a list or set? Can you show me with code how to do this?
@Toll: thanks for your attention, this is what i'm using to define the map: [/QUOTE]Map<String, String> ID_MAP = new HashMap<>();[/QUOTE]
Re: Mapping a set to a string
Ah, excellent. Instead of making a HashMap<String, String>, you can make a HashMap<String, LinkedList<String>>. As I said though, the adding is a bit more complex.
First, you get the value stored for the key you just read. If this value is null, you create a new LinkedList<String> and put it in the map as the value for the key you just read.
After that, you add the newly-read value to the LinkedList stored in the Map.
It is a bit more complicated, as I said, so if I'm unclear anywhere, I should be able to clarify.
Re: Mapping a set to a string
You don't have to add the Lists (probably an ArrayList) ahead of time. As Toll had already suggested, just use the key to get the List. If the list is null, create one and add it to the map and add the string to the list. If it is already there, just grab the list reference and add the string to the list.
Regards,
Jim
Re: Mapping a set to a string
Toll and Jim,
Thank for your ideas. But I couldn't understand exactly what you were pointing to. Do u mean that I only need to creat one map and compare the keys that will be added to the map befor adding them or after? What do u mean by saying "if the list is null"?
Re: Mapping a set to a string
You only need one map, yes.
When it comes to adding values, you don't add them directly, but rather get the list for that key and add the value to that list. What's important to know here is that if no list has been created for that key yet, then it will return null. If so you need to create the list and add it to the map.
Re: Mapping a set to a string
Ok! My issue is in the step of creating the list for each key. I have a really big file of Strings, and what i'm doing is splitting it into keys and values. Both keys and values are stored in arrays. So my issue is how to eliminate duplicated values in the Keys array and at the same time store their corresponding values in a list?
Re: Mapping a set to a string
You can only have one key per value in a map. And since your data (according to you) has a one to many mapping of keys to data you need to store the data in another List (which is the data for the key). The duplicate keys will go away by the nature of the way maps behave. Here is a visual reference of what I believe you want to do:
"A", "Apple"
"B", "Bear"
"A","Anteater"
"B", "Boar"
"A", "Aphid"
"C", "Camera"
After creating the map:
"A" -> ["Apple", "Anteater", "Aphid"]
"B" -> ["Bear", "Boar"]
"C" -> ["Camera"]
Regards,
Jim
Re: Mapping a set to a string
If you associate the tulple <K,V> in your map, the key K will be associated with a List that contains V; but what do you want to happen when you associate the tuple <K,V> twice (or more) in the map? Do you want the value List to contain two (or more) values V? If not, better use a Map<K, Set<V>> instead of a Map<K, List<V>>.
kind regards,
Jos
Re: Mapping a set to a string
What I want to do is first eliminate duplicated key, and have the values of each unique key stored in a Set of Strings.
Re: Mapping a set to a string
Good point. But we hadn't gotten that far in the discussion yet (at least not as far as I could discern).
Regards,
Jim