Saturday, 4 July 2015

How To Convert Integer To String In Java


public class IntToString {

    public static void main(String[] args) {
         int num = 10;
      
       // method first
     
       String str1 = Integer.toString(num);
     
        System.out.println(" String is ="+str1);
       
        // method second
   
       String str2 = String.valueOf(num);
   
        System.out.println(" Striing is ="+str2);
  
   }

}

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