1. StringTokenizer example
In the old days, Java developers like to use theStringTokenizer
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());
}
}
}
Outputabc
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