cancel
Showing results for 
Search instead for 
Did you mean: 

iterate throught the hashmap

Former Member
0 Kudos

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;

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

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

http://www.sam-solutions.net

Answers (1)

Answers (1)

Former Member
0 Kudos

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]