Showing posts with label java collection framework programs. Show all posts
Showing posts with label java collection framework programs. Show all posts

Thursday, 18 June 2015

How to Search key in TreeMap Java

import java.util.Map;
import java.util.TreeMap;

public class TreeMapSearchKey {
   
    public static void main(String[] args) {
       
        Map<Integer, String> tm = new TreeMap<Integer,String>();
       
        // add key and values
        tm.put(1,"java");
        tm.put(2,"cpp");
        tm.put(3,"php");
        tm.put(4,"c");
        tm.put(5, "mysql");
       
        //print treemap
   
        System.out.println("TreeMap Is="+tm);
        System.out.println(tm.containsKey(1));
        System.out.println(tm.containsKey(18));
        System.out.println(tm.containsKey(3));
        System.out.println(tm.containsKey(25));
       
       
    }

}

Output:

TreeMap Is={1=java, 2=cpp, 3=php, 4=c, 5=mysql}

true

false

true

false

Monday, 25 May 2015

How To Remove Element of Vector

import java.util.Vector;

public class RemoveElement {

    public static void main(String[] args) {
      
        Vector<String> vector = new Vector<String>();
        //add element in vector
        vector.add("cricket");
        vector.add("hockey");
        vector.add("football");
        vector.add("tennish");
      
        // print vector
        System.out.println("Vector = "+vector);
      
      
        /*
         * Removes the element at the specified position
         * in this Vector. Shifts any subsequent elements
         * to the left (subtracts one from their indices).
         * Returns the element that was removed from the
         *  Vector.
         */
        vector.remove(1);
      
        System.out.println("After remove element vector is = "+vector);
               
            }
     
}

Output:

Vector = [cricket, hockey, football, tennish]

After remove element vector is = [cricket, football, tennish]