1 comments

Java Concepts to revise before coding interview

Published on Wednesday 30 September 2015 in

1. StringTokenizer example

In the old days, Java developers like to use the StringTokenizer class to split a string. This is because the StringTokenizer class is available since JDK 1.0 and the String.split() is available since JDK 1.4
TestSplit.java
package com.mkyong.test

import java.util.StringTokenizer;

public class TestSplit {

 public static void main(String[] args) {

  String test = "abc.def.123";

  StringTokenizer token = new StringTokenizer(test, ".");

  while (token.hasMoreTokens()) {
   System.out.println(token.nextToken());
  }
  
 }

}
Output
abc
def
123
 
reference: www.mkyong.com/java/java-how-to-split-a-string/ 
 
 
2. HASHMAP
 
package com.mkyong.test;

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

public class TestMap {

 public static void main(String[] args) {
  
  Map<String, Integer> fruits = new HashMap<>();
  fruits.put("apple", 1);
  fruits.put("orange", 2);
  fruits.put("banana", 3);
  
  if(fruits.containsKey("apple")){
   //key exists
   System.out.println(fruits.get("apple"));
  }else{
   //key not exists
  }
  
 }

}
 

3. Integer.parseInt() Examples

Example to convert a String “10” to an primitive int.

String number = "10"; 
int result = Integer.parseInt(number); 
System.out.println(result);
 
Output
10 


4. Java Leap Year Example

Java example to determine if the given year is a leap year.
DateTimeExample.java
package com.mkyong.utils;

public class DateTimeExample {

    public static void main(String[] args) {

 DateTimeExample obj = new DateTimeExample();
 System.out.println("1993 is a leap year : " + obj.isLeapYear(1993));
 System.out.println("1996 is a leap year : " + obj.isLeapYear(1996));
 System.out.println("2012 is a leap year : " + obj.isLeapYear(2012));

    }

    public boolean isLeapYear(int year) {

 if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {
  return true;
 } else {
  return false;
 }
    }
 
} 
 

5 How to join two Lists in Java

 

List.addAll()

 
package com.mkyong.example;

import java.util.ArrayList;
import java.util.List;

public class JoinListsExample {

 public static void main(String[] args) {
 
  List<String> listA = new ArrayList<String>();
  listA.add("A");
  
  List<String> listB = new ArrayList<String>();
  listB.add("B");
  
  List<String> listFinal = new ArrayList<String>();
  listFinal.addAll(listA);
  listFinal.addAll(listB);
  
  //same result
  //List listFinal = new ArrayList(listA);
  //listFinal.addAll(listB);
  
  System.out.println("listA : " + listA);
  System.out.println("listB : " + listB);
  System.out.println("listFinal : " + listFinal);
  
 }

}
 

6 How to count duplicated items in Java List


 1.
        Map<String, Integer> map = new HashMap<String, Integer>();

 for (String temp : list) {
  Integer count = map.get(temp);
  map.put(temp, (count == null) ? 1 : count + 1);
 }
 printMap(map); 
 2
 
        System.out.println("\nSorted Map");
 Map<String, Integer> treeMap = new TreeMap<String, Integer>(map);
 printMap(treeMap);
 
 

7 How to loop ArrayList in Java

 
 
package com.mkyong.core;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

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

  List<String> list = new ArrayList<String>();
  list.add("Text 1");
  list.add("Text 2");
  list.add("Text 3");

  System.out.println("#1 normal for loop");
  for (int i = 0; i < list.size(); i++) {
   System.out.println(list.get(i));
  }

  System.out.println("#2 advance for loop");
  for (String temp : list) {
   System.out.println(temp);
  }

  System.out.println("#3 while loop");
  int j = 0;
  while (list.size() > j) {
   System.out.println(list.get(j));
   j++;
  }

  System.out.println("#4 iterator");
  Iterator<String> iterator = list.iterator();
  while (iterator.hasNext()) {
   System.out.println(iterator.next());
  }
 }
}
 
 

8 Convert char array to String in Java

 

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

  char[] charArrays = new char[] {'1', '2', '3', 'A', 'B', 'C'};
  
  String newString1 = new String(charArrays);
  System.out.println("newString1 : " + newString1);
       
  String newString2;
  newString2 = String.valueOf(charArrays);
  System.out.println("newString2 : " + newString2);
 }
} 
 
 

9 How to sort an ArrayList in java

 
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class SortArrayList{
 
 public static void main(String args[]){
  
  List<String> unsortList = new ArrayList<String>();
  
  unsortList.add("CCC");
  unsortList.add("111");
  unsortList.add("AAA");
  unsortList.add("BBB");
  unsortList.add("ccc");
  unsortList.add("bbb");
  unsortList.add("aaa");
  unsortList.add("333");
  unsortList.add("222");
  
  //before sort
  System.out.println("ArrayList is unsort");
  for(String temp: unsortList){
   System.out.println(temp);
  }
  
  //sort the list
  Collections.sort(unsortList);
  
  //after sorted
  System.out.println("ArrayList is sorted");
  for(String temp: unsortList){
   System.out.println(temp);
  }
 }
 
}
 

10 How to sort a Map in Java

http://www.mkyong.com/java/how-to-sort-a-map-in-java/

package com.mkyong;

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

public class SortMapOnKeyStringExample {

 public static void main(String[] args) {

  Map<String, String> unsortMap = new HashMap<String, String>();
  unsortMap.put("Z", "z");
  unsortMap.put("B", "b");
  unsortMap.put("A", "a");
  unsortMap.put("C", "c");
  unsortMap.put("D", "d");
  unsortMap.put("E", "e");
  unsortMap.put("Y", "y");
  unsortMap.put("N", "n");
  unsortMap.put("J", "j");
  unsortMap.put("M", "m");
  unsortMap.put("F", "f");

  System.out.println("Unsort Map......");
  printMap(unsortMap);

  System.out.println("\nSorted Map......");
  Map<String, String> treeMap = new TreeMap<String, String>(unsortMap);
  printMap(treeMap);

 }

 public static void printMap(Map<String, String> map) {
  for (Map.Entry<String, String> entry : map.entrySet()) {
   System.out.println("Key : " + entry.getKey() 
                                      + " Value : " + entry.getValue());
  }
 }

} 
 

11 How to loop a Map in Java

Map<String, String> map = new HashMap<String, String>();
 map.put("1", "Jan");
 map.put("2", "Feb");
 map.put("3", "Mar");
 
 //loop a Map
 for (Map.Entry<String, String> entry : map.entrySet()) {
  System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue());
 }
  
 //Java 8 only, forEach and Lambda
 map.forEach((k,v)->System.out.println("Key : " + k + " Value : " + v));

 
 
 

12 How to convert List to Set (ArrayList to HashSet)

 
Convert List to Set
Set set = new HashSet(list);
Convert Set to List
List list = new ArrayList(set);
 
 

13 Java HashMap example

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

   Map mMap = new HashMap();
   mMap.put("PostgreSQL", "Free Open Source Enterprise Database");
   mMap.put("DB2", "Enterprise Database , It's expensive");
   mMap.put("Oracle", "Enterprise Database , It's expensive");
   mMap.put("MySQL", "Free Open SourceDatabase");

   Iterator iter = mMap.entrySet().iterator();

   while (iter.hasNext()) {
    Map.Entry mEntry = (Map.Entry) iter.next();
    System.out.println(mEntry.getKey() + " : " + mEntry.getValue());
   }

   mMap.put("Oracle", "Enterprise Database , It's free now ! (hope)");

   System.out.println("One day Oracle.. : " + mMap.get("Oracle"));

  } catch (Exception e) {
   System.out.println(e.toString());
  }
 }
} 

1 comments

compiler

Published on Tuesday 29 July 2014 in

1 comments

18 secret tricks and shortcuts in Windows 7

Published on Wednesday 18 January 2012 in , , , , , , , ,


Here is a first collection of tips and tricks related to Windows 7, complete with a key combination:
  1. In Windows 7 you can dock windows by dragging the right or left side of the desktop.Operation that can run faster through the following combinations: 
    Win + sn
     and Win + Right Arrow 
    Win + Up Arrow
     and Win + Down Arrow to minimize and maximize the current window 
    Win + Shift + Up Arrow
     and Win + Shift + Down Arrow maximizes and restores the vertical size
  2. If you want to display your desktop on an external monitor or projector, simply press the key combination Win + P , or start the application displayswitch.exe . You will be presented a screen like the one pictured above.
  3. To highlight the current window, hide all other currently open press Win + Home .
  4. Win + Shift + Sn Arrow and Win + Shift + Right Arrow to move windows from one monitor to another.
  5. To open a DOS window from its current location, simply press Shift while selecting an item from the context menu.
  6. Some of the backgrounds available are contextualized according to the country, others are in a hidden folder. To view them you all just go to C: \ Windows \ Globalization \ MCT.
  7. You can record the actions you perform within the operating system through the program psr.exe also found that in the Control Panel under "Record steps to reproduce a problem".
  8. Windows 7 will find the character of Gabriola , a beautiful decorative font.
  9. If Explorer 8 seems too heavy on startup, then you have to remove some addons. Go to the Tools menu, select Manage Add-ons and try to remove some plugin.
  10. If you can, use a USB stick (you only need 4 GB) to install Windows 7, it is really fast, much more than using a DVD. FAT32 format it and copy the ISO to install with the following command xcopy e: \ f: \ / e / f .
  11. If you miss the toolbar Quick Launch in the task bar, ripristinatela writing the following location % userprofile% \ AppData \ Roaming \ Microsoft \ Internet Explorer \ Quick Launch window to add new toolbar.
  12. With the command Win + Space to operate the ' aero peek , to see all the icons and gadgets installed on your desktop.
  13. Ctrl + Shift will allow you to run a program as an administrator directly.
  14. If you need to open an instance of an application you are using, not resort to the Start menu ... click the icon in the taskbar of the program by pressing Shift .
  15. To start directly on the screen of our computer's file manager to change the target of the link explorer % SystemRoot% \ explorer.exe / root,:: {20D04FE0-3AEA-1069-A2D8-08002B30309D}
  16. When you press double-click on a CD or DVD image files (eg. ISO) will open a window Burning ISO that will allow you to proceed with burning
  17. Now you can optimize the display of text cttune.exe and color calibration with dccw.exealso on your PC monitor
  18. Drag your favorite applications in the taskbar
Enhanced by Zemanta

0 comments

Windows Tips: how to find the product key

Published on in , , , , , , , ,


windows_product_key
The product key is the " activation key "of the original software. Every copy of Windows is characterized by a unique key that is used to activate and verify the authenticity of the software. This key is required by Windows during installation or after. Many times, however, computers are sold with Windows pre-installed software, it follows that the product key was entered by the seller and, if it has no warranty sticker pasted on the printed product key, we need a trick to track him down.
Following a format it, to install and reactivate our original copy of Windows, we will need the 25-character code that certifies the uniqueness and authenticity. How to find?
Keyfinder
Simple KeyFinder is a small utility that instantly finds the operating system product key. It works with Windows 7, Windows Vista, Windows XP, Windows 2003 and Windows 2000.
Just open it from the command line will generate a text file with the activation key.
productkeyfinder2
Alternative: Product Key Finder , small utility that receives, in addition to the product key, the license keys of installed products. Tested and works with Windows 7, Windows Vista and Windows XP.


Enhanced by Zemanta