on 02-12-2007 11:25 AM
how could i able to iterate the hashmap object and display only the object with the values "true"............
below code....is wrong...
it display all the items whether it is true / false
Set keySet=m2.entrySet();
Iterator it=keySet.iterator();
while(it.hasNext()){
Map.Entry en=(Map.Entry)it.next();
if(m2.containsValue("true")){
System.out.println("checking:" +en.getKey()+"="+en.getValue());
}
}
Map m=new HashMap();
m.put("a","true");
m.put("c","true");
m.put("d","false");
return m;
yzme,
final Map m=new HashMap();
m.put("a","true");
m.put("c","true");
m.put("d","false");
for (final Iterator i = m.entrySet().iterator(); i.hasNext(); ) {
final Map.Entry e = (Map.Entry)i.next();
if ( "true".equals( e.getValue() ) ) {
System.out.println("Key " + e.getKey() + " has true value");
}
else {
System.out.println("Skipping key " + e.getKey() );
}
}
Valery Silaev
SaM Solutions
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
try this:
Map map = new LinkedHashMap();
// Add some elements
map.put("1", "value1");
map.put("2", "value2");
map.put("3", "value3");
map.put("2", "value4");
// List the entries
for (Iterator it=map.keySet().iterator(); it.hasNext(); ) {
Object key = it.next();
Object value = map.get(key);
}
// [1=value1, 2=value4, 3=value3]
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
83 | |
10 | |
10 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.