Friday, December 9, 2016

Inner class - Example2

class Outer {
/*static class Inner {
static void m1(){
System.out.println("Hi I am Inside m1() method");
}
public void m2(){
System.out.println("Will this static and non static work without main() method ?");
}
public static void main(String args[]) {
System.out.println("Hi Balaji how are you ?");
new Inner().m2();
Inner.m1();
}
}
public static void main(String args[]) {
Inner inner = new Inner();
}*/

static void m3(){
System.out.println("How are you ? are you Good ?ccccccccccccccc " +x);
x = 20;
}
static void m4(){
System.out.println("How are you ? are you Good ?ddddddddddddddd " +x);
}
static {
System.out.println("Hi Balaji................  ?aaaaaaaaaaaaaaa ");
m3();
}
static {
System.out.println("Hi Balaji................  ?bbbbbbbbbbbbbbb ");
m4();
}
static int x = 10;
static int y = x;
public static void main(String args[]) {
System.out.println("Hi Balaji how are you ? " + y);
}
}

//Execute this and see
Run using
javac Outer.java
java Outer$Inner

Inner class - Example1

class Outer {
static class Inner {
static void m1(){
System.out.println("Hi I am Inside m1() method");
}
public void m2(){
System.out.println("Will this static and non static work without main() method ?");
}
public static void main(String args[]) {
System.out.println("Hi Balaji how are you ?");
new Inner().m2();
Inner.m1();
}
}

public static void main(String args[]) {
Inner inner = new Inner();
}
}

//Execute this and see
Run using
javac Outer.java
java Outer$Inner

Sunday, April 13, 2014

How to get the key and values of various Map classes ?

import java.util.*;

public class MapKeysandValues {

@SuppressWarnings("unchecked")
public static void main(String[] args) {

Map<String, String> tm = new TreeMap();
tm.put("1", "one");
tm.put("2", "two");
tm.put("3", "three");
tm.put("4", "four");
System.out.println("TreeMap key values are " + tm.keySet());
System.out.println("TreeMap values are " + tm.values());

}

}

Result Is : 
TreeMap key values are [1, 2, 3, 4]
TreeMap values are [one, two, three, four]

The same applies for all the Map classes, like HashMap, HashTable etc.

Example for String.format


import java.util.*;

public class replaceQuestionMarkwithValues {

       static String query = "select * from table where col1 = ?, col2= ?, col3=? ";
    
       public static void main(String[] args) {

         replaceQuestionMarkwithValues re = new replaceQuestionMarkwithValues();
         List list = new ArrayList();
         list.add("1");
         list.add("2");
         list.add("3");
         list.add("4");
     System.out.println("The Output Is " + extracted(re, list));

       }

private static String extracted(replaceQuestionMarkwithValues re, List list) {
     return re.formatQuery(query, list);
}

        public static String formatQuery(String query, List params){
         Object[] args=params.toArray();
         System.out.println("Total values present are " + args.length);
     return String.format(query.replaceAll("\\?", "%s"), args);
}

}

Result Is : 
--------------
Total values present are 4
The Output Is select * from table where col1 = 1, col2= 2, col3=3 

Example for java.util.MissingFormatArgumentException


import java.util.*;

public class replaceQuestionMarkwithValues {

static String query = "select * from table where col1 = ?, col2= ?, col3=? ";
    
        public static void main(String[] args) {

         replaceQuestionMarkwithValues re = new replaceQuestionMarkwithValues();
         List list = new ArrayList();
         list.add("1");
         list.add("2");
         //list.add("3");
         //list.add("4");    
             System.out.println("The Output Is " + extracted(re, list));
}

private static String extracted(replaceQuestionMarkwithValues re, List list) {
return re.formatQuery(query, list);
}

        public static String formatQuery(String query, List params){
        Object[] args=params.toArray();
        System.out.println("Total values present are " + args.length);
   return String.format(query.replaceAll("\\?", "%s"), args);
}

}

Result Is : 

Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier 's'
at java.util.Formatter.format(Unknown Source)
at java.util.Formatter.format(Unknown Source)
at java.lang.String.format(Unknown Source)
at replaceQuestionMarkwithValues.formatQuery(replaceQuestionMarkwithValues.java:26)
at replaceQuestionMarkwithValues.extracted(replaceQuestionMarkwithValues.java:20)
at replaceQuestionMarkwithValues.main(replaceQuestionMarkwithValues.java:15)

Wednesday, April 9, 2014

SetIteratorTreeMapExample

import java.util.*;

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

        TreeMap <Integer, String>tMap = new TreeMap<Integer, String>();
         tMap.put(1, "Sunday");
         tMap.put(2, "Monday");
         tMap.put(3, "Tuesday");
         tMap.put(4, "Wednesday");
         tMap.put(5, "Thursday");
         tMap.put(6, "Friday");
         tMap.put(7, "Saturday");

        Set s = tMap.keySet();
        System.out.println("TreeMap Set value Is " + tMap.keySet());

        Iterator itr= s.iterator();
        try {
              while(itr.hasNext()){
             System.out.println("iterator.hasnext Value Is " + tMap.get(itr.next()));
             System.out.println("tMap Value Is " + tMap.get(itr.next()));
             }
       }
       catch(Exception e){
     System.out.println("Exception Value Is " + e);
       }

       }
}

Console Answer Is :

TreeMap Set value Is [1, 2, 3, 4, 5, 6, 7]
iterator.hasnext Value Is Sunday
tMap Value Is Monday
iterator.hasnext Value Is Tuesday
tMap Value Is Wednesday
iterator.hasnext Value Is Thursday
tMap Value Is Friday
iterator.hasnext Value Is Saturday

Storing HashKey and Values

import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

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

ConcurrentHashMap<CheckPojo, Integer> hm = new ConcurrentHashMap<CheckPojo,Integer>();
hm.put(new CheckPojo("1","One"), 99);
hm.put(new CheckPojo("2","Two"), 100);
Set<CheckPojo> s = hm.keySet();

Iterator itr= s.iterator();
while(itr.hasNext()){
System.out.println("Iterator.hasNext Value Is " + hm.get(itr.next()));
}

Iterator it = hm.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
CheckPojo key = (CheckPojo)entry.getKey();
Integer val = (Integer)entry.getValue();
System.out.println("key Is : " + key.getMethod1() +", " + key.getMethod2() + " and value Is : " + val);
}

}
}

beanClass Is

public class CheckPojo {
 
private String method1;
private String method2;

public CheckPojo(String method1, String method2){
this.method1=method1;
this.method2=method2;
}
public String getMethod1() {
return method1;
}
public String getMethod2() {
return method2;
}

}

Console Result Is : 

Iterator.hasNext Value Is 99
Iterator.hasNext Value Is 100
key Is : 1, One and value Is : 99
key Is : 2, Two and value Is : 100