Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

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

public class Main {
    public static Integer getKeyByValue(Map<Integer, String> map, Object value) {
        Integer key = -1;
        Iterator<Entry<Integer, String>> it = map.entrySet().iterator();
        while (it.hasNext()) {
            Entry<Integer, String> entry = (Entry<Integer, String>) it.next();
            String obj = entry.getValue();
            if (obj != null && obj.equals(value)) {
                // break as find the first key, assuming key and value are one-to-one
                key = (Integer) entry.getKey();
                break;
            }
        }
        return key;
    }
}