Solved

HashMap - Converting long string values to smaller codes

Posted on 2014-10-24
7
197 Views
Last Modified: 2014-10-25
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.
0
Comment
Question by:royjayd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
7 Comments
 
LVL 32

Expert Comment

by:awking00
ID: 40402493
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?
0
 

Author Comment

by:royjayd
ID: 40402622
>>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.
0
 
LVL 32

Expert Comment

by:awking00
ID: 40402647
Also, are your keys actually Strings or are they ints?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 32

Accepted Solution

by:
awking00 earned 500 total points
ID: 40402758
>>Each key can only be mapped to one value.<<
DOH!  Of course they must. Guess I was having a little brain cramp. How was your map constructed (e.g. as HashMap or TreeMap, or whatever)?
0
 

Author Comment

by:royjayd
ID: 40403411
no worries , i got it working.
I will give you the points for your time.
Thank you.
0
 
LVL 32

Expert Comment

by:awking00
ID: 40404001
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?
0
 
LVL 32

Expert Comment

by:awking00
ID: 40404056
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;
	}
}

Open in new window

0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 160
SHA2 certs for IIS AND Java? 2 136
Html split(text) 2 48
Java pass by reference 3 74
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question