Monday 19 January 2015

Java : Collection Framework : HashMap (ContainsKey)


Click here to watch in Youtube : 
https://www.youtube.com/watch?v=zvFQxPFv8G8&list=UUhwKlOVR041tngjerWxVccw

HashMapExample.java
import java.util.HashMap;

/*
 * Example of containsKey(Object key) method.
 */
public class HashMapExample
{
    public static void main( String[] args )
    {

        HashMap<Integer, String> hashMap = new HashMap<Integer, String>();

        hashMap.put(1, "Apple");
        hashMap.put(2, "Ball");
        hashMap.put(3, "Cat");

        System.out.println("hashMap : " + hashMap + "\n");

        /*
         * Returns true if this map contains a mapping for the specified key.
         */
        boolean isKeyExist = hashMap.containsKey(1);

        System.out.println("isKey '1' Exist : " + isKeyExist);

        isKeyExist = hashMap.containsKey(10);

        System.out.println("isKey '10' Exist : " + isKeyExist);
    }
}
Output
hashMap : {1=Apple, 2=Ball, 3=Cat}

isKey '1' Exist : true
isKey '10' Exist : false
To Download HashMapDemoContainsKey Project Click the below link
https://sites.google.com/site/javaee4321/java-collections/HashMapDemoContainsKey.zip?attredirects=0&d=1

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • No comments:

    Post a Comment