1.Basic Java example program to check particular value exists in hashmap
Output:
- package com.javacheckvaluehashmap;
- import java.util.Hashmap;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Map.Entry;
- public class HashmapExample{
- public static void main(String[] args) {
- //create an Hashmap object
- HashMap<String, String> hashmap = new HashMap();
- //Add key values pairs to hashmap
- hashmap.put("1","One");
- hashmap.put("2","Two");
- hashmap.put("3","Three");
- hashmap.put("4","Four");
- hashmap.put("5","Five");
- hashmap.put("6","Six");
- /*
- To check whether a particular value exists in HashMap use
- boolean containsValue(Object value) method of HashMap class.
- containsValue(Object value) returns true if the HashMap contains mapping for specified
- value otherwise false.*/
- boolean isExists = hashmap.containsValue("Six");
- System.out.println("The value Six exists in HashMap ? : " + isExists);
- if(!hashmap.isEmpty()){
- Iterator it=hashmap.entrySet().iterator();
- while(it.hasNext()){
- Map.Entry obj=(Entry) it.next();
- System.out.print(obj.getKey()+" ");
- System.out.println(obj.getValue());
- }
- }
- }
- }
Output:
- The value Six exists in HashMap ? : true
- 1 One
- 2 Two
- 3 Three
- 4 Four
- 5 Five
- 6 Six
Bagikan
Java Basic example program to check particular value exists in hashmap
4/
5
Oleh
Kris Kimcil