Jay Roy
asked on
HashMap - Converting long string values to smaller codes
hi experts
I have a Map<String,String>
Key value
1 Prl-transaction Inc. Adjust,Prl-transaction Inc,Prl-transaction POPO Inc.,eer yy uuu,iii - TESTCLUSTER
2 Prl-transaction Inc. Adjust,Prl-transaction Inc,Prl-transaction POPO Inc.,eer yy uuu,iii - TESTCLUSTER_GMC,RRRRR
3 Prl-transaction Inc. Adjust,Prl-transaction Inc,Prl-transaction POPO Inc.,eer yy uuu,iii - TESTCLUSTER
4 Prl-transaction Inc. Adjust,Prl-OGRMAA Inc,Prl-transaction POPO Inc.,eer yy uuu,iii - TESTCLUSTER
As you can see The 'value' in the Map can be very long.
What i want to do is create a new Map with short codes for values something like
Key value
1 Prl-transaction1
2 Prl-transaction2
3 Prl-transaction1
4 Prl-transaction3
where
Prl-transaction1 = Prl-transaction Inc. Adjust,Prl-transaction Inc,Prl-transaction POPO Inc.,eer yy uuu,iii - TESTCLUSTER
Prl-transaction2 = Prl-transaction Inc. Adjust,Prl-transaction Inc,Prl-transaction POPO Inc.,eer yy uuu,iii - TESTCLUSTER_GMC,RRRRR
Prl-transaction3 = Prl-transaction Inc. Adjust,Prl-OGRMAA Inc,Prl-transaction POPO Inc.,eer yy uuu,iii - TESTCLUSTER
* The value for key 1 and 3 are same since they are the same string.
Any idea how i can do that?
Any help will be greatly appreciated
thanks.
I have a Map<String,String>
Key value
1 Prl-transaction Inc. Adjust,Prl-transaction Inc,Prl-transaction POPO Inc.,eer yy uuu,iii - TESTCLUSTER
2 Prl-transaction Inc. Adjust,Prl-transaction Inc,Prl-transaction POPO Inc.,eer yy uuu,iii - TESTCLUSTER_GMC,RRRRR
3 Prl-transaction Inc. Adjust,Prl-transaction Inc,Prl-transaction POPO Inc.,eer yy uuu,iii - TESTCLUSTER
4 Prl-transaction Inc. Adjust,Prl-OGRMAA Inc,Prl-transaction POPO Inc.,eer yy uuu,iii - TESTCLUSTER
As you can see The 'value' in the Map can be very long.
What i want to do is create a new Map with short codes for values something like
Key value
1 Prl-transaction1
2 Prl-transaction2
3 Prl-transaction1
4 Prl-transaction3
where
Prl-transaction1 = Prl-transaction Inc. Adjust,Prl-transaction Inc,Prl-transaction POPO Inc.,eer yy uuu,iii - TESTCLUSTER
Prl-transaction2 = Prl-transaction Inc. Adjust,Prl-transaction Inc,Prl-transaction POPO Inc.,eer yy uuu,iii - TESTCLUSTER_GMC,RRRRR
Prl-transaction3 = Prl-transaction Inc. Adjust,Prl-OGRMAA Inc,Prl-transaction POPO Inc.,eer yy uuu,iii - TESTCLUSTER
* The value for key 1 and 3 are same since they are the same string.
Any idea how i can do that?
Any help will be greatly appreciated
thanks.
Are the keys in the map unique? Your example shows different keys mapping to the same value, but can you have the same key mapping to different values?
ASKER
>>Are the keys in the map unique?
Key in Map is always unique, isint it ?
In my code above Key is 1,2,3,4
Each key can only be mapped to one value.
Key in Map is always unique, isint it ?
In my code above Key is 1,2,3,4
Each key can only be mapped to one value.
Also, are your keys actually Strings or are they ints?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
no worries , i got it working.
I will give you the points for your time.
Thank you.
I will give you the points for your time.
Thank you.
Thanks for the points. Could I see what you ended up with as I have been working on the solution but it's not been completed yet?
Not very elegant, but I got this to work using a temporary map -
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
@SuppressWarnings("unused")
public class TestMap {
public static Map<String, String> map = new TreeMap<String, String>();
public static Map<String, String> tempMap = new TreeMap<String, String>();
public static Map<String, String> newMap = new TreeMap<String, String>();
public static Collection values;
public static void main(String[] args) {
map.put("1","Prl-transaction Inc. Adjust,Prl-transaction Inc,Prl-transaction POPO Inc.,eer yy uuu,iii - TESTCLUSTER");
map.put("2","Prl-transaction Inc. Adjust,Prl-transaction Inc,Prl-transaction POPO Inc.,eer yy uuu,iii - TESTCLUSTER_GMC,RRRRR");
map.put("3","Prl-transaction Inc. Adjust,Prl-transaction Inc,Prl-transaction POPO Inc.,eer yy uuu,iii - TESTCLUSTER");
map.put("4","Prl-transaction Inc. Adjust,Prl-OGRMAA Inc,Prl-transaction POPO Inc.,eer yy uuu,iii - TESTCLUSTER");
values = map.values();
String[] vals = (String[]) values.toArray(new String[0]);
for (String key : map.keySet()) {
for (String val : vals) {
if (map.get(key).toString().equals(val) && !tempMap.containsValue(val))
{
tempMap.put(key, val);
newMap.put(key, val.substring(0, val.indexOf(" ")) + key);
} else if (map.get(key).toString().equals(val) && tempMap.containsValue(val))
{
String oldKey = getOldKey(val);
newMap.put(key, val.substring(0, val.indexOf(" ")) + oldKey);
}
}
}
System.out.println(newMap.toString());
}
public static String getOldKey(String val) {
String replaceKey = null;
for (String oldKey : tempMap.keySet()) {
if (tempMap.get(oldKey).toString().equals(val)) {
replaceKey = oldKey;
}
}
return replaceKey;
}
}