下面列出了java.util.LinkedHashMap#containsValue ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void main(String[] args) {
//create LinkedHashMap object
LinkedHashMap lHashMap = new LinkedHashMap();
//add key value pairs to LinkedHashMap
lHashMap.put("1", "One");
lHashMap.put("2", "Two");
lHashMap.put("3", "Three");
/*
To check whether a particular value exists in LinkedHashMap use
boolean containsValue(Object key) method of LinkedHashMap class.
It returns true if the value is mapped to one or more keys in the
LinkedHashMap otherwise false.
*/
boolean blnExists = lHashMap.containsValue("Two");
System.out.println("Two exists in LinkedHashMap ? : " + blnExists);
}
public static void main(String[] args) {
//create LinkedHashMap object
LinkedHashMap lHashMap = new LinkedHashMap();
//add key value pairs to LinkedHashMap
lHashMap.put("1", "One");
lHashMap.put("2", "Two");
lHashMap.put("3", "Three");
/*
To check whether a particular value exists in LinkedHashMap use
boolean containsValue(Object key) method of LinkedHashMap class.
It returns true if the value is mapped to one or more keys in the
LinkedHashMap otherwise false.
*/
boolean blnExists = lHashMap.containsValue("Two");
System.out.println("Two exists in LinkedHashMap ? : " + blnExists);
}
public static boolean dataSourceObjectsContainsUri(Object[] dataSourceObjects, Object object) {
LinkedHashMap<String, Object> map = (LinkedHashMap) dataSourceObjects[0];
if (map != null && object != null) {
return map.containsValue(object);
}
return false;
}
public static void main(String[] args) throws IOException, ClassNotFoundException {
MyKey key = new MyKey(1);
LinkedHashMap<MyKey, String> ht = new LinkedHashMap<>();
ht.put(key, "Value");
System.out.println("Entry: " + ht.get(key));
// Retrieve using the key, works
// Serialize the hash table object
try (FileOutputStream fos = new FileOutputStream("hashdata.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos)) {
oos.writeObject(ht);
}
// Deserialize the hash table object
LinkedHashMap<MyKey, String> ht_in;
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("hashdata.ser"))) {
ht_in = (LinkedHashMap <MyKey, String>) ois.readObject();
}
if (ht_in.containsValue("Value"))
// Check whether the object actually exists in the hash table
System.out.println("Value was found in deserialized object.");
// Can fail after serialization and deserialization. Consequently,
// it may be impossible to retrieve the value of the object after
// deserialization by using the original key.
if (ht_in.get(key) == null) // Gets printed
System.out.println("Object was not found when retrieved using the key.");
}