how to get the map values with out iterating in java

Hi Experts,

I have written the below program to get "key values" from the map.
is it possible to get the map values with out using  for loop or while loop iteration.

can some one suggest me

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;


public class MapExample {
	
	@SuppressWarnings("unchecked")
	public static void main(String args[]){
		Practice pr = setToMap();
		 Map<Integer,String> map = pr.getMeasurements();
		 Set<Integer> set=map.keySet();	
		 System.out.println(set.size());
		
	}

	private static Practice setToMap() {
		Map<Integer,String> measurements = new HashMap<Integer, String>();
		measurements.put(111, "hello");
		measurements.put(222,"hiiii");
		Practice pr = new Practice();
		pr.setMeasurements(measurements);
		return pr;
	}

}


import java.util.Map;


public class Practice {
	
private Map<Integer,String> measurements;

public Map<Integer,String> getMeasurements() {
	return measurements;
}

public void setMeasurements(Map<Integer,String> measurements) {
	this.measurements = measurements;
}


} 

Open in new window

Thanks
LVL 2
srikoteshAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

srikoteshAuthor Commented:
i am getting the keys from the map with out iterating
gurpsbassiCommented:
Call values () method on the map.

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
CPColinSenior Java ArchitectCommented:
If all you're planning to do with the keys or the values is call size(), you can skip calling Map.keySet() or Map.values() and call size() on the map itself; all three calls will return the same value.
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

srikoteshAuthor Commented:
I don't want size I need list values which are stored in the map.
in the above example I have stored values hi and hello.
I want to display those with out iteration.
if I use map.values I am getting array of values like
[hi,hello ].
CPColinSenior Java ArchitectCommented:
Map.values() returns a Collection. If you want to display the contents of it, you're going to have to iterate over it. Even if you just call toString() on the collection, that code will have to iterate in order to get all the values.
gurpsbassiCommented:
Get the values through values() method as I suggested earlier and iterate over them. As CPColin stated, whatever code you use will need to iterate over the collection.

What is the problem with iteration? Do you have a billion entries in the map?
srikoteshAuthor Commented:
no...
actually my question is with out iteration I am getting keys from the map.
similarly is there any method to get the values from the map.
if not possible I will iterate it and get the values.
gurpsbassiCommented:
Doesn't make clear sense to me.

You say you are getting keys from the map without iteration. Yes you are using the keySet() method.

Similarly you can use the values() method to get the values out.

Job done!

If you want to do stuff with the key set or the value collection, you have to iterate.

Show me how you would process the key set without iterating.
You'r code is not doing anything significant  with the key set above other than getting its size.
srikoteshAuthor Commented:
sorry, I confused
even my program also if I want keys means  I have to iterate.after calling map.Keynes method.
yes.
map.values method will gives the list of values.
gurpsbassiCommented:
you are still making no sense.

What do you want to do with the values ?
CEHJCommented:
When you ask questions, you need to state your GOAL
srikoteshAuthor Commented:
Set<Integer> set=map.keySet();

actual o/p:
[222, 111]	

but i assumed wrongly like i am getting values like --->
o/p:
222
111
2

so that's why i have asked a question is it possible to get the map values w/o iteration.

with map.keySet() i thought i am getting the values with out iteration
my assumtion was wrong 

i have to iterate and get the values:

 Set<Integer> set=map.keySet();	
		 for (Integer integer : set) {
			 System.out.println(integer);
		}
		
o/p:
222
111
2

similarly to get the values we are using values()
Collection<String> collectionValues = map.values();
		for(String s: collectionValues){
			System.out.println(s);
		}
		

Open in new window

gurpsbassiCommented:
It seems you are hung up on using the tostring() method to give you your desired result. In fact looking at some of your previous posts, you have    used tostring() to do a lot of printing with collections. I need to warn you that this technique is seriously flawed!

Do not rely on the Java api tostring() to give you consistent results between versions. If you want to use tostring() then override it.
srikoteshAuthor Commented:
k
CPColinSenior Java ArchitectCommented:
Do you need more information from us on this question?
srikoteshAuthor Commented:
no
CPColinSenior Java ArchitectCommented:
Please accept one or more solutions that helped you get your answer.
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.