Java Programming Interview Questions
Java programming tutorial, java examples and interview questions, Design Pattern, Hibernate and JDBC
Friday, March 23, 2018
Most Popular Programming Languages between 1988 and 2018
Sunday, January 31, 2016
TIOBE programming languages ranking between 1985 and 2015
References:
http://www.javacademia.com/2015/08/classement-tiobe-des-langages-de-programmation-1985-2015.html
Monday, January 25, 2016
Java - How to generate random alpha-numeric strings
Generate type character
The ASCII code of the first character is lowercase alphabetic 65 (a) and the last is 97 + 26 = 122 (z). The generated number is in the interval [97, 122] or in the range [0.26] + 97.Random rand = new Random(); char c = (char)(rand.nextInt(26) + 97); System.out.println(c);
Generate random string
For n characters, you need to use for loop: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
Generate alpha-numeric string from a set
This example show how to generate alphanumeric characters from a defined set of characters. Proceed as follows:- Create a String with all that you want
- Get the length of this string
- Call Rand.nextInt() method that returns the random length between 0 and n.
- Print out the char using alphabet.charAt (k) method
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:
How to generate a random String in Java
Java doc: java.util.Random class
Extended ascii code
Java array sort in ascending and descending order
Several methods in Java allow you to sort your tables and use these methods of sorting tables, you will primarily import a library named Arrays. You do it with the keyword import:
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
Sort array in descending order
Sorting in descending order is only possible if you write your own code or convert the array into an array of objects, then import collections librairy and call Collections.sort() method. This method sort the array in the ascendeng order, to sort it in the descending order, we call reverse() method that should reverse the result array.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:
Java.util.Arrays.sort(int[]) Method
How to loop over HashMap in Java
- Advanced for loop or for each
- Iterator + while loop
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
References:
Iterate through a HashMap
How to sort ArrayList in ascending/descending order in Java
Sort ArrayList in ascending order
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
Sort ArrayList in descending order
The Collections class has another method Collections.sort (List <T>, Comparator <T>). This method sort an ArrayList in both ascending and descending order. sort() method uses a comparator of objects that compare two objects using compareTo() method.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:
stackOverFlow:How to sort a ArrayList in java
How to sort ArrayList in Java - BeginnersBook
Saturday, January 9, 2016
How to loop over ArrayList in Java
- The for loop
- The while loop or do..while
- The while+iterator loop
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
How to browse an ArrayList using the Enumeration interface
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()); } }
After compilation and execution of this code:
a b c
Subscribe to:
Posts (Atom)