我需要搜索存储在LinkedHashMap中的匹配值对。
我尝试了以下代码,但它为存在的任何值都给出了true,但我只希望当相应的值与键值匹配时返回true。
bt2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
if(CheckValueExample.checkRelationship(txtKey.getText(),txtValue.getText())==true)
System.out.println("Pair Match");
else
System.out.println("No-Pair Match");
} catch (Exception ex) {
Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
匹配对的方法:
public static boolean checkRelationship(String key, String value) {
HashMap<String, String> hashmap = new LinkedHashMap<String, String>();
// Adding Key and Value pairs to HashMap
hashmap.put("Bus","Land_Vehicle");
hashmap.put("SchoolBus","Bus");
hashmap.put("Truck","Land_Vehicle");
hashmap.put("Land_Vehicle","Vehicle");
boolean flag=false;
if(hashmap.containsKey(key)&&hashmap.containsValue(value))
flag=true;
else
flag=false;
return flag;
}
假设输入密钥时为“Bus”,输入的值为“Land_Vehicle”;只有这样,它才应该返回 true。
任何其他替代方法来做到这一点也是可观的,基本上我必须匹配存储在json文件中的对。
森栏
慕桂英4014372
随时随地看视频慕课网APP
相关分类