References:
http://www.javacademia.com/2015/08/classement-tiobe-des-langages-de-programmation-1985-2015.html
Java programming tutorial, java examples and interview questions, Design Pattern, Hibernate and JDBC
Random rand = new Random(); char c = (char)(rand.nextInt(26) + 97); System.out.println(c);
public static void main(String[] args) {
Random rand = new Random();
String str="";
for(int i = 0 ; i < 15 ; i++){
char c = (char)(rand.nextInt(26) + 97);
str += c;
System.out.print(c+" ");
}
}
q v r i v g z a w b b d n x y
Random rand = new Random(); String set = "abcd1235"; int length = set.length(); for(int i = 0; i < 20; i++) { int k = rand.nextInt(length); System.out.print(set.charAt(k)+" "); } d 5 1 b 1 b 2 3 c a b b 5 5 d d b 2 c 3References:
import java.util.Arrays;Here is an example:
import java.util.Arrays; public class ArraysTest { public static void main(String[] args) { // array init int array[] = {11,87,14,5,63,24}; // display all the elements before sorting for (int value : array) { System.out.println("number: " + value); } // Call Arrays.sort() method Arrays.sort(array); // display all the elements after sorting System.out.println("Sorted array\n"); for (int value : array) { System.out.println("number: " + value); } } }After complilation and execution, we should get that result:
number: 11 number: 87 number: 14 number: 5 number: 63 number: 24 Sorted array number: 5 number: 11 number: 14 number: 24 number: 63 number: 87
import java.util.Arrays; import java.util.Collections; public class ArraysTest { public static void main(String[] args) { // array init int array[] = {11,87,14,5,63,24}; // display all the elements before sorting for (int value : array) { System.out.println("number: " + value); } //create array of integer to store integer objects Integer[] integerArray = new Integer[array.length]; for (int i=0; i < array.length; i++) { //create an new integer object and store it integerArray[i] = new Integer(array[i]); } // sort the array then inverse it using reverseOrder() method Arrays.sort(integerArray, Collections.reverseOrder()); // display all the elements after sorting System.out.println("Sorted array\n"); for (int value : integerArray) { System.out.println("number: " + value); } } }Let us compile and run this program:
number: 11 number: 87 number: 14 number: 5 number: 63 number: 24 Sorted array number: 87 number: 63 number: 24 number: 14 number: 11 number: 5References:
import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class IterateHashMap{ public static void main(String[] args) { HashMap<String, Integer> map = new HashMap<String, Integer>(); map.put("A",1); map.put("B",2); map.put("C",3); map.put("D",4); //for each loop System.out.println("for each:"); for (Map.Entry mapentry : map.entrySet()) { System.out.println("key: "+mapentry.getKey() + " | value: " + mapentry.getValue()); } //iterator and while loop System.out.println("Iterator + while loop"); Iterator iterator = map.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry mapentry = (Map.Entry) iterator.next(); System.out.println("key: "+mapentry.getKey() + " | value: " + mapentry.getValue()); } } }Execution:
Boucle for: Key: D | value: 1 Key: A | value: 2 Key: B | value: 3 Key: C | value: 4 Boucle whileIn both cases, we get a set map of key-value data in the object Map.Entry. In for loop, we used entrySet () method of the Map class. In the while loop, was recovered an Iterator object and after obtaining all key-value, then we cast Map.Entry to print the keys and values with both getKey () and getValue () method.Key: D | value: 1 Key: A | value: 2 Key: B | value: 3 Key: C | value: 4
import java.util.ArrayList; import java.util.Collections; public class ArrayListAscendingOrder { public static void main(String[] args) { ArrayList unsorted = new ArrayList(); unsorted.add("Ee"); unsorted.add("5a"); unsorted.add("c8"); unsorted.add("41"); unsorted.add("09"); System.out.println("Before sort"); for(int i=0; i < unsorted.size(); i++) System.out.println(unsorted.get(i)); System.out.println("\nAfter sort"); Collections.sort(unsorted); for(int i=0; i < unsorted.size(); i++) System.out.println(unsorted.get(i)); } }Execution:
Avant le tri Ee 5a c8 41 09 Après le tri 09 41 5a c8 Ee
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; public class ArrayListDescendingOrder{ public static void main(String[] args) { ArrayList unsorted= new ArrayList(); unsorted.add("000"); unsorted.add("110"); unsorted.add("101"); unsorted.add("100"); unsorted.add("111"); System.out.println("Before sort"); for(int i=0; i < unsorted.size(); i++) System.out.println(unsorted.get(i)+" "); System.out.println("\nAfter sort"); Collections.sort(unsorted, new Comparator() { @Override public int compare(String s1, String s2) { /*to get the descending order, we shall compare s2 with s1 return s2.compareTo(s1); } }); for(int i=0; i < unsorted.size(); i++) System.out.println(unsorted.get(i)+" "); } }Execution:
Avant le tri 000 110 101 100 111 Après le tri 111 110 101 100 000Rerefences:
import java.util.ArrayList; import java.util.Iterator; public class main{ public static void main(String[] args) { ArrayListCompilation and execution:arraylist = new ArrayList (); arraylist .add(1); arraylist .add(2); arraylist .add(3); System.out.println("for loop"); for(int i = 0 ; i < arraylist .size(); i++) System.out.println(arraylist .get(i)); System.out.println("Advanced for loop"); for(Integer n : arraylist) System.out.println(n); System.out.println("while+iterator loop"); int i = 0; while(i iterator = arraylist.iterator(); while(iterator.hasNext()) System.out.println(iterator.next()); } }
for loop 1 2 3 Advanced for loop 1 2 3 while+iterator loop 1 2 3
import java.util.ArrayList; import java.util.Collections; import java.util.Enumeration; public class main{ public static void main(String[] args) { ArrayListalist = new ArrayList (); alist.add("a"); alist.add("b"); alist.add("c"); // get Enumeratioin object Enumeration enumeration = Collections.enumeration(alist); // iterate while(enumeration.hasMoreElements()) System.out.println(enumeration.nextElement()); } }
a b c
import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; import java.util.ArrayList; public class SerializeArrayList { public static void main(String[] args) { ArrayListAfter compilation and execution:arraylist=new ArrayList (); arraylist.add("hello world"); try { FileOutputStream fileOut = new FileOutputStream("file"); ObjectOutputStream out = new ObjectOutputStream(fileOut); out.writeObject(arraylist); out.close(); fileOut.close(); System.out.println("\nSerialization completed successfully...\n"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
Serialization completed successfully
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.ObjectInputStream; import java.util.ArrayList; public class main{ public static void main(String[] args) { ArrayList<String> arraylist= new ArrayList<String>(); try { FileInputStream fileis = new FileInputStream("file"); ObjectInputStream ois = new ObjectInputStream(fileis); arraylist = (ArrayList) ois.readObject(); ois.close(); fileIn.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("Reading data: \n"); for(String o:arraylist) System.out.println(o); } }
hello world