Monday 29 June 2015

How to find all occurrences of string in a string Java

 Find all occurrences of string in a string. for this we will use one loop and indexof() method

public class StringCheck {
   
    public static void main(String[] args) {
       
        String str = "delhi is good city in india and india also good. there are many city good and people also good" +
                "so india is good country ";
        String mystr = "good";
        int index =0;
   while(index != -1){
       index = str.indexOf(mystr, index+1);
        System.out.println("index is = "+index);
   };
       
    }
}

Output:

index is = 9

index is = 43

index is = 69

index is = 90

index is = 106

index is = -1

Friday 19 June 2015

How to delete all elements from TreeMap

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

public class TreeMapDeleteAll {

   
    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("Before Clear TreeMap Is="+tm);
       
        //Removes all of the mappings from this map
        tm.clear();
       
        System.out.println("After Clear TreeMap Is ="+tm);
       
    }
   
}




Output:


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

After Clear TreeMap Is ={}

How to get entry set from TreeMap

import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

public class TreeMapEntrySet {
   
    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);
       
        //Returns a Set view of the mappings contained in this
        // map
        Set<Entry<Integer, String>> entry =tm.entrySet();
       
        for (Entry<Integer, String> entry2 : entry) {
            System.out.println("key ="+entry2.getKey()+" value ="+entry2.getValue());
        }
       
    }

}




Output:


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

key =1 value =java

key =2 value =cpp

key =3 value =php

key =4 value =c

key =5 value =mysql

How to get size of TreeMap

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

public class TreeMapKeyCount {
   
    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);

        //Returns the number of key-value mappings in this map
       
        System.out.println("size of treemap = "+tm.size());
       
    }

}

Output:

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

size of treemap = 5








How to get all keys of TreeMap

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

public class TreeMapGetAllKeys {
   
    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");
       
        //Returns a Set view of the keys contained in this map.
       
        Set<Integer> keys = tm.keySet();
       
        for (Integer key : keys) {
            System.out.println("key = "+key);
        }
       
    }

}







Output:



key = 1

key = 2

key = 3

key = 4

key = 5

How to get value of key in TreeMap

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

public class TreeMapGetValueOfkey {
   
    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");
       
    /*
     * Returns the value to which the specified key is
       mapped, or null if this map contains no mapping for
       the key.
     */
      System.out.println("key =1  and value ="+tm.get(1));
      System.out.println("key =1 and value ="+tm.get(5));
      System.out.println("key =1 and value ="+tm.get(10));
      
       
    }

}

Output:

key =1  and value =java

key =1 and value =mysql

key =1 and value =null





Thursday 18 June 2015

How to Search Value in TreeMap Java

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

public class TreeMapSearchValue {

    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.containsValue("java"));
        System.out.println(tm.containsValue("data design"));
        System.out.println(tm.containsValue("html"));
        System.out.println(tm.containsValue("php"));
       
    }
   
   
}

Output:

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

true

false

false

true



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

How to storing Java Object In TreeMap

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

public class TreeMapStoreObject {

    public static void main(String[] args) {
       
    Map<Integer, Emp> tm = new TreeMap<Integer, Emp>();
   
    tm.put(1, new Emp("yogesh", 100));
    tm.put(2, new Emp("shivam", 200));
    tm.put(3, new Emp("ankush",250));
    tm.put(4, new Emp("anurag",300));
    tm.put(5, new Emp("ravi", 370));
   
    //Returns a Set view of the keys contained in this map.
    Set<Integer> keys = tm.keySet();
   
    for (Integer key : keys) {
        Emp emp = tm.get(key);
        System.out.println(" key ="+ key +" name ="+emp.getName()+" id = "+ emp.getName());
    }
 }
}

class Emp{
    private String name;
    private int id;
   
    public Emp(String name, int id) {
        this.name = name;
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
   
}

Output:

 key =1 name =yogesh id = yogesh

 key =2 name =shivam id = shivam

 key =3 name =ankush id = ankush

 key =4 name =anurag id = anurag

 key =5 name =ravi id = ravi


How to iterate through TreeMap

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

public class TreeMapIterate {
   
    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");
       
        //Returns a Set view of the keys contained in this map
       
        Set<Integer> keys = tm.keySet();
       
        for (Integer key : keys) {
            System.out.println(" key ="+key+" value ="+tm.get(key));
        }
       
    }

}

Output:

 key =1 value =java

 key =2 value =cpp

 key =3 value =php

 key =4 value =c

 key =5 value =mysql


How to create TreeMap In Java

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

public class CreateTreeMap {

    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);
       
    }
   
}





Output:


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

Java TreeMap Examples

Wednesday 17 June 2015

How to delete all elements from HashMap

import java.util.HashMap;
import java.util.Map;

public class HashMapDeleteAll {
   
    public static void main(String[] args) {
       
    Map<Integer,String> hm = new HashMap<Integer,String>();
       
    /*
     * Associates the specified value with the specified key in
       this map (optional operation). If the map previously
       contained a mapping for the key, the old value is
       replaced by the specified value
     */
        hm.put(1,"ankush");
        hm.put(2, "amit");
        hm.put(3,"shivam");
        hm.put(4,"ankit");
        hm.put(5, "yogesh");
       
        //print hashmap
        System.out.println("Before Clear HashMap is = "+hm);
       
        /*
         * The map will be empty after this call returns.
         */
       
        hm.clear();
       
        System.out.println("After Clear HashMap is ="+hm);
       
    }

}

OutPut:

Before Clear HashMap is = {1=ankush, 2=amit, 3=shivam, 4=ankit, 5=yogesh}

After Clear HashMap is ={}

Tuesday 16 June 2015

How to get entry set from HashMap

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class HashMapEntrySet {
   
    public static void main(String[] args) {
       
    Map<Integer,String> hm = new HashMap<Integer,String>();
       
    /*
     * Associates the specified value with the specified key in
       this map (optional operation). If the map previously
       contained a mapping for the key, the old value is
       replaced by the specified value
     */
        hm.put(1,"ankush");
        hm.put(2, "amit");
        hm.put(3,"shivam");
        hm.put(4,"ankit");
        hm.put(5, "yogesh");
       
        //print hashmap
        System.out.println("HashMap = "+hm);
       
        Set<Entry<Integer, String>> entry =hm.entrySet();
       
        for (Entry<Integer, String> entry2 : entry) {
            System.out.println(" Key = "+entry2.getKey()+" Value = "+entry2.getValue());
        }
    }

}




Output:

 HashMap = {1=ankush, 2=amit, 3=shivam, 4=ankit, 5=yogesh}

 Key = 1 Value = ankush

 Key = 2 Value = amit
 
 Key = 3 Value = shivam

 Key = 4 Value = ankit

 Key = 5 Value = yogesh

Sunday 14 June 2015

How to get size of HashMap

import java.util.HashMap;
import java.util.Map;

public class HashMapKeyCount {
   
    public static void main(String[] args) {
       
        Map<Integer,String> hm = new HashMap<Integer,String>();
           
        /*
         * Associates the specified value with the specified key in
           this map (optional operation). If the map previously
           contained a mapping for the key, the old value is
           replaced by the specified value
         */
            hm.put(1,"ankush");
            hm.put(2, "amit");
            hm.put(3,"shivam");
            hm.put(4,"ankit");
            hm.put(5, "yogesh");
           
            //Returns the number of key-value mappings in this map
           
            System.out.println("HashMap size = "+hm.size());
           
           
        }
   
}

Output:

HashMap size = 5

How to get all keys of HashMap

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class HashMapGetAllKeys {



        public static void main(String[] args) {
           
        Map<Integer,String> hm = new HashMap<Integer,String>();
           
        /*
         * Associates the specified value with the specified key in
           this map (optional operation). If the map previously
           contained a mapping for the key, the old value is
           replaced by the specified value
         */
            hm.put(1,"ankush");
            hm.put(2, "amit");
            hm.put(3,"shivam");
            hm.put(4,"ankit");
            hm.put(5, "yogesh");
           
            /*
             * Returns a Set view of the keys contained in this map
             */
           
            Set<Integer> keys =hm.keySet();
           
            for (Integer key : keys) {
                System.out.println(" key ="+key);
            }
        }
   
}

Output:

  key =1

  key =2

  key =3

  key =4
 
  key =5

How to get value of key in HashMap

import java.util.HashMap;
import java.util.Map;

public class HashMapGetValueOfkey {
    public static void main(String[] args) {
       
    Map<Integer,String> hm = new HashMap<Integer,String>();
       
    /*
     * Associates the specified value with the specified key in
       this map (optional operation). If the map previously
       contained a mapping for the key, the old value is
       replaced by the specified value
     */
        hm.put(1,"ankush");
        hm.put(2, "amit");
        hm.put(3,"shivam");
        hm.put(4,"ankit");
        hm.put(5, "yogesh");
       
        /*
         * Returns the value to which the specified
         * key is mapped, or null if this map contains
         *  no mapping for the key.
         */
       
        System.out.println(" key = 1 and value = "+hm.get(1));
        System.out.println(" key = 1 and value = "+hm.get(10));
       
        System.out.println(" key = 1 and value = "+hm.get(5));
        System.out.println(" key = 1 and value = "+hm.get(4));
       
    }

}

Output:

 key = 1 and value = ankush

 key = 1 and value = null

 key = 1 and value = yogesh

 key = 1 and value = ankit


How to Search Value in HashMap Java

import java.util.HashMap;
import java.util.Map;

public class HashMapSearchValue {

    public static void main(String[] args) {
      
        Map<Integer,String> hm = new HashMap<Integer,String>();
          
        /*
         * Associates the specified value with the specified key in
           this map (optional operation). If the map previously
           contained a mapping for the key, the old value is
           replaced by the specified value
         */
            hm.put(1,"ankush");
            hm.put(2, "amit");
            hm.put(3,"shivam");
            hm.put(4,"ankit");
            hm.put(5, "yogesh");
          
            System.out.println(" value john ="+hm.containsValue("john"));
            System.out.println(" value amit ="+hm.containsValue("amit"));
          
            System.out.println(" value piyush ="+hm.containsValue("piyush"));
            System.out.println(" value shivam ="+hm.containsValue("shivam"));
          
        }

}

Output;

 value john =false

 value amit =true

 value piyush =false

 value shivam =true

How to Search key in HashMap Java


import java.util.HashMap;
import java.util.Map;

public class HashMapSearchKey {
    public static void main(String[] args) {
       
    Map<Integer,String> hm = new HashMap<Integer,String>();
       
    /*
     * Associates the specified value with the specified key in
       this map (optional operation). If the map previously
       contained a mapping for the key, the old value is
       replaced by the specified value
     */
        hm.put(1,"ankush");
        hm.put(2, "amit");
        hm.put(3,"shivam");
        hm.put(4,"ankit");
        hm.put(5, "yogesh");
       
        /*
         * Returns true if this map contains a
         *  mapping for the specified key
         */
   
    System.out.println("key  1   ="+hm.containsKey(1));
    System.out.println("key  20  ="+hm.containsKey(20));
    System.out.println("key 9    ="+hm.containsKey(9));
    System.out.println("key  5   ="+ hm.containsKey(5));
       
    }
}


Output:
key  1   =true

key  20  =false

key 9    =false

key  5   =true


How to storing Java Object In HashMap

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class HashMapStoreObject {
   
    public static void main(String[] args) {
       
        Map<Integer,Emp> hm = new HashMap<Integer, Emp>();
       
        hm.put(1, new Emp(100, "dk") );
        hm.put(2, new Emp(102, "kapil") );
        hm.put(3, new Emp(105, "anup") );
        hm.put(4, new Emp(205, "kapil") );
        hm.put(5, new Emp(110, "pk") );
       
           Set<Integer> keys = hm.keySet();
      
           for (Integer key : keys) {
              
               Emp emp = hm.get(key);
            System.out.println(" Id = "+emp.getId()+" name = "+emp.getName());
        }
          
    }

}


class Emp{
   
        private int id;
        private String name;
       
        public Emp(int id, String name) {
            this.id = id;
            this.name = name;
        }
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
       
}




Output:

 Id = 100 name = dk

 Id = 102 name = kapil

 Id = 105 name = anup

 Id = 205 name = kapil

 Id = 110 name = pk


How to iterate through HashMap



import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class HashMapIterate {
   
    public static void main(String[] args) {
       
    Map<Integer,String> hm = new HashMap<Integer,String>();
       
    /*
     * Associates the specified value with the specified key in
       this map (optional operation). If the map previously
       contained a mapping for the key, the old value is
       replaced by the specified value
     */
        hm.put(1,"ankush");
        hm.put(2, "amit");
        hm.put(3,"shivam");
        hm.put(4,"ankit");
        hm.put(5, "yogesh");
       
       Set<Integer> keys = hm.keySet();
      
       for (Integer key : keys) {
        System.out.println("key ="+key+" value = "+hm.get(key));
    }
       
       
    }
   

}


Output:

key =1 value = ankush

key =2 value = amit

key =3 value = shivam

key =4 value = ankit

key =5 value = yogesh


How to create HashMap In Java

import java.util.HashMap;
import java.util.Map;

public class CreateHashMap {

    public static void main(String[] args) {
       
    Map<Integer,String> map = new HashMap<Integer,String>();
       
    /*
     * Associates the specified value with the specified key in
       this map (optional operation). If the map previously
       contained a mapping for the key, the old value is
       replaced by the specified value
     */
        map.put(1,"ankush");
        map.put(2, "amit");
        map.put(3,"shivam");
        map.put(4,"ankit");
        map.put(5, "yogesh");
       
        //print hashmap
        System.out.println("HashMap = "+map);
       
       
    }
   
}




Output:


HashMap = {1=ankush, 2=amit, 3=shivam, 4=ankit, 5=yogesh}

Java HashMap Examples

How to create shallow copy of Hashtable

import java.util.Hashtable;

public class HashTableShallowCopy {
    public static void main(String[] args) {
       
        Hashtable<Integer,String> ht = new Hashtable<Integer,String>();
       
        /*
         * Maps the specified key to the specified
         * value in this hashtable
         */
        ht.put(1, "java");
        ht.put(2, "jersey");
        ht.put(3, "spring");
        ht.put(4, "c");
        ht.put(5, "php");
       
        //print hashtable
        System.out.println(" Hash Table is ="+ht);
       
      //Creates a shallow copy of this hashtable.
      Hashtable<Integer,String> myHt = (Hashtable<Integer, String>) ht.clone();
   
      System.out.println(" shallow copy ="+myHt);
     
    }   

}


Output:

 Hash Table is ={5=php, 4=c, 3=spring, 2=jersey, 1=java}

 shallow copy ={5=php, 4=c, 3=spring, 2=jersey, 1=java}

How to get elements using Enumeration from Hashtable

import java.util.Enumeration;
import java.util.Hashtable;

public class HashTableGetEnumeration {

    public static void main(String[] args) {

        Hashtable<Integer, String> ht = new Hashtable<Integer, String>();

        /*
         * Maps the specified key to the specified value in this hashtable
         */
        ht.put(1, "java");
        ht.put(2, "jersey");
        ht.put(3, "spring");
        ht.put(4, "c");
        ht.put(5, "php");

        // Returns an enumeration of the keys in this hashtable

        Enumeration<Integer> keys = ht.keys();

        while (keys.hasMoreElements()) {
            int key = keys.nextElement();

            System.out.println(" key = " + key + " value = " + ht.get(key));

        }

    }

}



Output:

 key = 5 value = php

 key = 4 value = c

 key = 3 value = spring

 key = 2 value = jersey

 key = 1 value = java

How to delete all elements from Hashtable

import java.util.Hashtable;

public class HashtableDeleteAll {

    public static void main(String[] args) {
       
        Hashtable<Integer,String> ht = new Hashtable<Integer,String>();
       
        /*
         * Maps the specified key to the specified
         * value in this hashtable
         */
        ht.put(1, "java");
        ht.put(2, "jersey");
        ht.put(3, "spring");
        ht.put(4, "c");
        ht.put(5, "php");
       
        //print hashtable
        System.out.println("Before delete Hash Table is ="+ht);
       
        //Clears this hashtable so that it contains no keys.
        ht.clear();
       
        System.out.println("After delete all element hashtable is = "+ht);
       
         }   
   
}

 Output:

 Before delete Hash Table is ={5=php, 4=c, 3=spring, 2=jersey, 1=java}

After delete all element hashtable is = {}





How to get entry set from Hashtable

import java.util.Hashtable;
import java.util.Map.Entry;
import java.util.Set;

public class HashtableEntrySet {
   
    public static void main(String[] args) {
       
        Hashtable<Integer,String> ht = new Hashtable<Integer,String>();
       
        /*
         * Maps the specified key to the specified
         * value in this hashtable
         */
        ht.put(1, "java");
        ht.put(2, "jersey");
        ht.put(3, "spring");
        ht.put(4, "c");
        ht.put(5, "php");
       
        //print hashtable
        System.out.println("Hash Table is ="+ht);
       
        //Returns a Set view of the mappings contained in this
        // map
        Set<Entry<Integer, String>> entry = ht.entrySet();
       
        for (Entry<Integer, String> ent : entry) {
            System.out.println(" key = "+ent.getKey()+" value = "+ent.getValue());
        }
    }   
   
}

Output:

Hash Table is ={5=php, 4=c, 3=spring, 2=jersey, 1=java}

key = 5 value = php

key = 4 value = c

key = 3 value = spring

key = 2 value = jersey

key = 1 value = java


How to get size of hashtable


import java.util.Hashtable;

public class HashTableKeyCount {
   
    public static void main(String[] args) {
       
        Hashtable<Integer,String> ht = new Hashtable<Integer,String>();
       
        /*
         * Maps the specified key to the specified
         * value in this hashtable
         */
        ht.put(1, "java");
        ht.put(2, "jersey");
        ht.put(3, "spring");
        ht.put(4, "c");
        ht.put(5, "php");
       
        //Returns the number of keys in this hashtable
       
        System.out.println("Hash Table size  ="+ht.size());
       
       
         }   

}

Output:

Hash Table size  =5




How to Free up RAM On Ubuntu/Debian

 Firstly  see ,how much memory is free on your system, open the Terminal and run this command:

free -m

I got this output:





Now run

sudo -i

echo 3 > /proc/sys/vm/drop_caches

Now ,Again run

free -m







Thursday 11 June 2015

How to get all keys of HashTable

import java.util.Hashtable;
import java.util.Set;

public class HashTableGetAllKeys {

    public static void main(String[] args) {
       
        Hashtable<Integer,String> ht = new Hashtable<Integer,String>();
       
        /*
         * Maps the specified key to the specified
         * value in this hashtable
         */
        ht.put(1, "java");
        ht.put(2, "jersey");
        ht.put(3, "spring");
        ht.put(4, "c");
        ht.put(5, "php");
       
        /*
         * Returns a Set view of the keys contained in this map.
         */
        Set<Integer> keys =ht.keySet();
       
        for (Integer key : keys) {
            System.out.println("key ="+key);
        }
   
    }
   
   
}


Output:

key =5

key =4

key =3

key =2

key =1



How to get value of key in HashTable


import java.util.Hashtable;

public class HashTableGetValueOfkey {
   
    public static void main(String[] args) {
       
        Hashtable<Integer,String> ht = new Hashtable<Integer,String>();
       
        /*
         * Maps the specified key to the specified
         * value in this hashtable
         */
        ht.put(1, "java");
        ht.put(2, "jersey");
        ht.put(3, "spring");
        ht.put(4, "c");
        ht.put(5, "php");
       
        System.out.println(" key = 1 and value ="+ht.get(1));
       
        System.out.println(" key = 2 and value ="+ht.get(2));
       
        System.out.println(" key = 9 and value ="+ht.get(9));
       
   
         }   

}

Output:

 key = 1 and value =java

 key = 2 and value =jersey

 key = 9 and value =null

How to Search Value in Hashtable Java

import java.util.Hashtable;

public class HashTableSearchValue {
   
    public static void main(String[] args) {
       
        Hashtable<Integer,String> ht = new Hashtable<Integer,String>();
       
        /*
         * Maps the specified key to the specified
         * value in this hashtable
         */
        ht.put(1, "java");
        ht.put(2, "jersey");
        ht.put(3, "spring");
        ht.put(4, "c");
        ht.put(5, "php");
       
        System.out.println(" value=java ="+ht.containsValue("java"));
        System.out.println(" value=ankush ="+ht.containsValue("ankush"));
        System.out.println(" value=java ="+ht.containsValue("java"));
       
    }   
}

Output:

 value=java =true

 value=ankush =false

 value=java =true


How to Search key in Hashtable Java

import java.util.Hashtable;

public class HashTableSearchKey {
   
    public static void main(String[] args) {
       
        Hashtable<Integer,String> ht = new Hashtable<Integer,String>();
       
        /*
         * Maps the specified key to the specified
         * value in this hashtable
         */
        ht.put(1, "java");
        ht.put(2, "jersey");
        ht.put(3, "spring");
        ht.put(4, "c");
        ht.put(5, "php");
       
         /*
          * Returns: true if and only if the specified object
          * is a key in this hashtable, as determined by the equals method;
             false otherwise.
          */
        System.out.println(" key =1   ="+ht.containsKey(1));
        System.out.println(" key =10   ="+ht.containsKey(10));
        System.out.println(" key =5   ="+ht.containsKey(5));
  }   

}





Output:


key =1   =true

key =10   =false

key =5   =true

How to iterate through Hashtable

import java.util.Hashtable;
import java.util.Set;

public class HashTableIterate {
   
    public static void main(String[] args) {
       
        Hashtable<Integer,String> ht = new Hashtable<Integer,String>();
       
        /*
         * Maps the specified key to the specified
         * value in this hashtable
         */
        ht.put(1, "java");
        ht.put(2, "jersey");
        ht.put(3, "spring");
        ht.put(4, "c");
        ht.put(5, "php");
       
        /*
         * Returns a Set view of the keys contained in this map.
         */
        Set<Integer> keys =ht.keySet();
       
        for (Integer key : keys) {
            System.out.println(" key = "+key+" value ="+ht.get(key));
        }
    }   
}

Output:

 key = 5 value =php

 key = 4 value =c

 key = 3 value =spring

 key = 2 value =jersey

 key = 1 value =java


How to storing Java Object In HashTable

import java.util.Hashtable;
import java.util.Set;

public class HashTableOfObject {

    public static void main(String[] args) {

        Hashtable<Integer,User> ht = new Hashtable<Integer,User>();
       
        ht.put(1,new User("ankush",100));
        ht.put(2,new User("shivam",200));
        ht.put(3,new User("raja", 500));
        ht.put(5,new User("ram",900));
        ht.put(6,new User("ravi",750));

        /*
         * Returns a Set view of the keys contained in this map.
         */
        Set<Integer> keys =ht.keySet();
       
        for (Integer key : keys) {
           
            /*
             * Returns the value to which the
             * specified key is  mapped, or null if
             *  this map contains no mapping for the key.
             */
            User user = ht.get(key);
            System.out.println(" key ="+key+" id ="+user.getId()+" name ="+user.getName());
        }

       
    }
}

class User {
    private String name;
    private int id;

    public User(String name, int id) {
        super();
        this.name = name;
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

}

Output:

 key =6 id =750 name =ravi

 key =5 id =900 name =ram

 key =3 id =500 name =raja

 key =2 id =200 name =shivam

 key =1 id =100 name =ankush

How to create HashTable In Java

import java.util.Hashtable;

public class CreateHashTable {
   
public static void main(String[] args) {
   
    Hashtable<Integer,String> ht = new Hashtable<Integer,String>();
   
    /*
     * Maps the specified key to the specified
     * value in this hashtable
     */
    ht.put(1, "java");
    ht.put(2, "jersey");
    ht.put(3, "spring");
    ht.put(4, "c");
    ht.put(5, "php");
   
    //print hashtable
    System.out.println("Hash Table is ="+ht);
     }   
}

Output:

Hash Table is ={5=php, 4=c, 3=spring, 2=jersey, 1=java}

Monday 8 June 2015

Select Data From Cassandra Using Java

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;

public class SelectFromCassandra {
    public static void main(String[] args) {

        Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1").build();
        Session session = cluster.connect("demo");
       
        ResultSet rs = session.execute("SELECT *FROM emp1 WHERE emp_id=500");
        for (Row row : rs) {
            System.out.println("emp_id = "+row.getInt("emp_id")+" name = "+row.getString("name"));
        }
    }

}

Insert Data Into Cassandra Example Set 2

import com.datastax.driver.core.BoundStatement;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.Session;

public class CassandraExample {
   
    public static void main(String[] args) {
       
        Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1").build();
       
        Session session = cluster.connect("demo");
       
        PreparedStatement ps = session.prepare
                ("insert into emp1(emp_id,ss_id,name) values(?,?,?)");
       
        BoundStatement bs = new BoundStatement(ps);
       
        session.execute(bs.bind(255,320,"javaproficiency"));

        System.out.println("ok");
       
    }
}

Insert Data Into Cassandra Example

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Session;






Connect return cassandra connection

public class Connect {

    public static Session getConection(){
       
        Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1").build();

        Session session = cluster.connect("demo");

        return session;

    }
   
}

In test class we will insert data into cassandra database

import com.datastax.driver.core.Session;

public class Test {

    public static void main(String[] args) {

        Session session = Connect.getConection();
        String query = "insert into emp1(emp_id,ss_id,name) values(500,205,'raja')";
        session.execute(query);
        System.out.println("ok");
    }
}

Saturday 6 June 2015

How to check TreeSet Empty Or not

import java.util.Set;
import java.util.TreeSet;

public class IsEmpty {
   
    public static void main(String[] args) {
       
           Set<String> ts = new TreeSet<String>();
            ts.add("mumbai");
            ts.add("delhi");
            ts.add("kolkata");
            ts.add("chandigarh");
            ts.add("dehradun");

            // print treeset
            System.out.println(" ts ="+ts);
           
            System.out.println("Is Empty = "+ts.isEmpty());
           
            Set<String> set = new TreeSet<String>();
           
            System.out.println("Is Empty = "+set.isEmpty());
       
        }

}



Output:

ts =[chandigarh, dehradun, delhi, kolkata, mumbai]

Is Empty = false

Is Empty = true



Related Posts: 


How to count number of element in TreeSet

import java.util.Set;
import java.util.TreeSet;

public class CountElement {
   
    public static void main(String[] args) {
       
           Set<String> ts = new TreeSet<String>();
            ts.add("mumbai");
            ts.add("delhi");
            ts.add("kolkata");
            ts.add("chandigarh");
            ts.add("dehradun");

            // print treeset
            System.out.println(" ts ="+ts);
           
            /*
             * Returns the number of elements in this set (its
                cardinality). If this set contains more than
                Integer.MAX_VALUE elements, returns
                Integer.MAX_VALUE.
             */
           
            System.out.println("Size of treeset = "+ts.size());
       
        }

}



Output:

 ts =[chandigarh, dehradun, delhi, kolkata, mumbai]

Size of treeset = 5



Related Posts: 



How to find does TreeSet contains elements or not

Returns true if this set contains the specified element.More formally, returns true if and only if this set contains an element e such that (o==null ? e==null :
 o.equals(e)).


import java.util.Set;
import java.util.TreeSet;

public class ContailsElement {
   
    public static void main(String[] args) {
       
           Set<String> ts = new TreeSet<String>();
            ts.add("mumbai");
            ts.add("delhi");
            ts.add("kolkata");
            ts.add("chandigarh");
            ts.add("dehradun");

            // print treeset
            System.out.println(" ts ="+ts);
           
            System.out.println(ts.contains("java"));
            System.out.println(ts.contains("delhi"));
            System.out.println(ts.contains("php"));
       
        }

}

Output:

ts =[chandigarh, dehradun, delhi, kolkata, mumbai]

false

true

false


Related Posts: 

How To Convert Treeset To Array

import java.util.Set;
import java.util.TreeSet;

public class TreeSetToArray {
   
    public static void main(String[] args) {
       
           Set<String> ts = new TreeSet<String>();
            ts.add("mumbai");
            ts.add("delhi");
            ts.add("kolkata");
            ts.add("chandigarh");
            ts.add("dehradun");

            // print treeset
            System.out.println("tree set is ="+ts);
           
            String [] arr = new  String[ts.size()];
       
            /*
             * Returns an array containing all of
             * the elements in this set
             */
           
            ts.toArray(arr);
       
            System.out.println("Print Array Element ");
           
            for (String str : arr) {
                 System.out.println("Array Element is = "+str);   
            }
   
    }
   

}




Output:



tree set is =[chandigarh, dehradun, delhi, kolkata, mumbai]

Print Array Element

Array Element is = chandigarh

Array Element is = dehradun

Array Element is = delhi

Array Element is = kolkata

Array Element is = mumbai



Related Posts: