The Artima Developer Community
Sponsored Link

Java Buzz Forum
Java Basic example program to check particular value exists in hashmap

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Java Basic example program to check particular value exists in hashmap Posted: Mar 13, 2016 5:42 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Java Basic example program to check particular value exists in hashmap
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
1.Basic Java example program to check particular value exists in hashmap


  1. package com.javacheckvaluehashmap;
  2.  
  3. import java.util.Hashmap;
  4. import java.util.Iterator;
  5. import java.util.Map;
  6. import java.util.Map.Entry;
  7.  
  8. public class HashmapExample{
  9.  
  10. public static void main(String[] args) {
  11.   
  12. //create an Hashmap object
  13. HashMap<String, String> hashmap = new HashMap();
  14.        
  15. //Add key values pairs to hashmap
  16.         
  17. hashmap.put("1","One");
  18. hashmap.put("2","Two");
  19. hashmap.put("3","Three");
  20. hashmap.put("4","Four");
  21. hashmap.put("5","Five");
  22. hashmap.put("6","Six");       
  23.         
  24.  
  25.  /*
  26.  To check whether a particular value exists in HashMap use
  27.  boolean containsValue(Object value) method of HashMap class.
  28. containsValue(Object value) returns true if the HashMap contains mapping for specified
  29. value otherwise false.*/
  30.        
  31. boolean isExists = hashmap.containsValue("Six");
  32. System.out.println("The value Six exists in HashMap ? : " + isExists);
  33.     
  34.  
  35. if(!hashmap.isEmpty()){
  36.  
  37. Iterator it=hashmap.entrySet().iterator();
  38.              
  39. while(it.hasNext()){

  40. Map.Entry obj=(Entry) it.next();
  41. System.out.print(obj.getKey()+" ");
  42. System.out.println(obj.getValue());
  43.              
  44. }           
  45. }   
  46.  
  47. }
  48.  
  49. }
     



Output:

  1. The value Six exists in HashMap ? : true
  2. 1 One
  3. 2 Two
  4. 3 Three
  5. 4 Four
  6. 5 Five
  7. 6 Six

check particular value exits in hashmap

Read: Java Basic example program to check particular value exists in hashmap

Topic: C3p0 Connection Pooling Example Previous Topic   Next Topic Topic: 5 different ways to iterate list in java

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use