How to loop through a Map and get the String in Java?

Hi,
I have the following Map:

Map<Integer, String> checkNameMap

Open in new window


I would like to loop through this Map and and get the strings from it and concatenate these strings in one big string by putting -x in front of all of them.

Example:
If the strings that I get from this map are:

car
bus
plane
ship

Open in new window


The big string that I want to create will be like

-x car -x bus -x plane -x ship

Open in new window


Can you please show me how I can do it?
TolgarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TolgarAuthor Commented:
I did something like this:

            String checkList = "";
            Iterator it_checkNameMap = checkNameMap.entrySet().iterator();
            // loop through the hashmap and create the checks list
            while (it_checkNameMap.hasNext()) {
                Map.Entry pairs_checkNameMap = (Map.Entry)it_checkNameMap.next();
                checkList = checkList + " -guiCheck " + (pairs_checkNameMap.getValue()).toString();
                it_checkNameMap.remove(); // avoids a ConcurrentModificationException
            }

Open in new window



Any ideas?
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
The above looks like it should have worked. What was the result?

Anyway, it would be easier code to write if you use generics fully. It will save needing to do all the casting. And also, if you are building a string like this, you might want to use a StringBuilder rather than concatenating to a String variable constantly. Something like this...
            StringBuilder checkList = new StringBuilder();
            Iterator<Map.Entry<Integer, String>> it_checkNameMap = checkNameMap.entrySet().iterator();
            // loop through the hashmap and create the checks list
            while (it_checkNameMap.hasNext()) {
                Map.Entry<Integer, String> pairs_checkNameMap = it_checkNameMap.next();
                checkList.append(" -guiCheck ").append(pairs_checkNameMap.getValue());
                // You shouldn't need the below line. If you want to have an empty map after this whole loop finishes, just call checkNameMap.clear() after the loop...
                // it_checkNameMap.remove(); // avoids a ConcurrentModificationException
            }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.