【发布时间】:2016-10-13 18:03:16
【问题描述】:
我需要直接使用 hashmap 值检查我的变量值。
我的 hashmap 值在字符串中有两个条目,我需要使用 hashmap 的第一个条目检查我的变量值。
上面是小版本的代码。
HashMap<String , String> directory = new HashMap<String , String>();
directory.put("AFG","Afghanistan");
directory.put("GBR","United Kingdom of Great Britain and Northern Ireland");
directory.put("IDN","Indonesia");
directory.put("IND","India");
接下来我使用扫描器类来获取用户的值。然后我需要知道的是如何将此用户的值与哈希图的第一个条目(即 AFG、GBR 等)进行比较
整个程序的示例代码是:
import java.util.*;
public class hashmapdemo {
public static void main(String args[]) {
HashMap<String , String> directory = new HashMap<String , String>();
directory.put("AFG","Afghanistan");
directory.put("GBR","United Kingdom of Great Britain and Northern Ireland");
directory.put("IDN","Indonesia");
directory.put("IND","India");
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
System.out.println(name);
// comparing logic ?
}
}
【问题讨论】:
-
用户的输入是什么?键还是值?
-
hashmap 中没有顺序,如果顺序很重要,您应该查看其他数据结构。
-
用户的输入将是关键部分,即 IND 等,我需要将其与 hashmap 值@Saravana 进行比较
-
不,订单对我来说并不重要,我只想将该用户的输入与哈希图值进行比较@EduardoDennis
-
您似乎混合了条目、键和值,这让您的问题难以理解