Friday, March 23, 2018

Most Popular Programming Languages between 1988 and 2018

Here is a list of the most popular programming languages in the last 30 years, published by the tiobe.com website. Note that these are the average positions for a period of 12 months. TIOBE website suggests visitors to inform site managers if there is a shortage. In case you can notice that there is a suggestion, and you can contact them at tpci@tiobe.com.






Sunday, January 31, 2016

TIOBE programming languages ranking between 1985 and 2015

Here is a list of the most popular programming languages in the last 30 years, published by the tiobe.com website. Note that these are the average positions for a period of 12 months. TIOBE website suggests visitors to inform site managers if there is a shortage. In case you can notice that there is a suggestion, and you can contact them at tpci@tiobe.com.

tiobe programming langage rank 1985-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

In this tutorial, we will show you how to get a random string using three methods in the examples bellow. Generation of random characters in Java does not exist, but there are several ways to do that. We will use java.util.Random.nextInt () method to generate random integers and then convert those integers to the conrresponding character according to the ASCII code.

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 3 
References:
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

In this tutorial, we will study the methods of sorting a table in the ascending and descending order in Java. We'll show you how to use the sort () method to perform the sorting task.

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: 5
References:
Java.util.Arrays.sort(int[]) Method

How to loop over HashMap in Java

In this tutorial, we will show you how to iterate over each entry in HashMap using two methods to display all elements of an HashMap in Java:
  1. Advanced for loop or for each
  2. Iterator + while loop 
In the example below, we are using for loop and iterator with while loop to print out elements:

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 while
Key: D | value: 1
Key: A | value: 2
Key: B | value: 3
Key: C | value: 4
In 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.

References:
Iterate through a HashMap

How to sort ArrayList in ascending/descending order in Java

Elements in ArrayList are displayed in the order of their inclusion in the list by defalut, but sometimes we need to iterate through the ArrayList to display elements in ascending or descending order. In this tutorial, we will implement a code that use Collections.sort () method that makes sorting a Arraylist in ascending and descending order.

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
000

Rerefences:
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

Iterate an ArrayList in Java is done by using three loops:
  • The for loop
  • The while loop or do..while
  • The while+iterator loop
The example below show how to use the three methods mentioned.

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

public class main{

 public static void main(String[] args) {
  
  ArrayList 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());
 }
}
Compilation and execution:

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) {
  
     ArrayList alist = 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