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

TreeSetExample

import java.util.*;

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

 TreeSet <Integer>tree = new TreeSet<Integer>();
 tree.add(12);
 tree.add(03);
 tree.add(41);
 tree.add(36);

 Iterator iterator = tree.iterator();


 while (iterator.hasNext()){
 System.out.print("++++>>>> "+iterator.next() + " ");
 }
  }
}

Console Result Is : 

++++>>>> 3 ++++>>>> 12 ++++>>>> 36 ++++>>>> 41 

java.util.NoSuchElementException

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

       }
}

Monday, April 7, 2014

System Slow how to rectify

Do you feel your PC is slow or hanging on very frequently you try the following simple procedures before you think of reinstalling the OS or upgrading your RAM or PC.
It is out of my experience.                                                
1.  Go to Start | RUN and type the following in the RUN textbox: %temp%    Now delete all temp files.  
2.   Then go to My Computer and select all the Hard Disk Drives right-click and select properties, then start disk cleanup wizard. It will remove all the unwanted programs present in your PC/system.                  
3.   Again, go to Start | RUN and type "msconfig" in the RUN textbox. This command will open the System Configuration. Go to Services | Start up and deselect all the possible programs you don't want to be run by your PC automatically when initializing your System. You touch this option only you are very confident and expert with the system.                        
4.   Next, go to Windows folder located in drive C and then open System32 (a sub-folder in Windows folder). Now look for the folder namely "Prefetch". Once you find it, straightly right-click on it and hit delete. Note if “prefetch” folder is not in System32 folder, you will surely find the same in windows folder and Delete it!                                                                                        
5.   Also if you are facing some critical problems/issues occurring from internet temp file, clear the entire history prevailing in that file.
6.   Pl see your PC's processor located in the CPU, whether it is properly or not.
7.   Use a good priced Antivirus software instead of free downloads.
8.   Above all you keep your computer dusts free especially RAM.