search Map - Java Collection Framework

Java examples for Collection Framework:Map

Description

search Map

Demo Code


//package com.java2s;

import java.util.Iterator;
import java.util.Map;

public class Main {
    public static String searchMap(Map hm, String searchFor) {

        Iterator i = hm.entrySet().iterator();
        String newName = null;/*  w  ww.j  ava 2s  . c  om*/

        while (i.hasNext()) {

            Map.Entry entry = (Map.Entry) i.next();
            String key = (String) entry.getKey();
            String value = (String) entry.getValue();

            if (key.equals(searchFor)) {
                newName = value;
            }

            if (value.equals(searchFor)) {
                newName = key;
            }

        }

        return newName;

    }
}

Related Tutorials