Saturday 28 February 2015

Java : Find Fastest Way to iterate ArrayList


Hi friends, I'm sharing little code here, this might help you to find fastest way to iterate Java ArrayList. 

I'm using JDK 8. You need a timer to print the time elapsed since timer is started.
This timer might not 100% accurate but will help to choose correct iterator method.
File 1 : CustomTimer.java


package com.raju.demo;

public class CustomTimer {

 private long startTime = 0l;

 public CustomTimer() {
  this.startTime = System.currentTimeMillis();
 }

 public void stop(String msg) {
  long totalTime = (System.currentTimeMillis() - startTime);
  String logMessage = null;
  if (null == msg) {
   logMessage = "##End time : " + totalTime + " ms";
  } else {
   logMessage = "##End time : " + totalTime
     + " ms for class/method : " + msg;
  }
  System.out.println(logMessage);
 }
}

File 2: Main.java

package com.raju.demo;

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

public class Main {
 public static void main(String... args) {
  List<Integer> myList = new ArrayList<>();
  CustomTimer customTimer = new CustomTimer();
  for (int x = 0; x < 10000000; x++) {
   myList.add(x);
  }
  customTimer.stop("After List Creation");

  customTimer = new CustomTimer();
  for (int i = 0, size = myList.size(); i < size; i++) {
   Integer s = myList.get(i);
  }
  customTimer.stop("After Traditional for");

  customTimer = new CustomTimer();
  for (Integer s : myList) {
  }
  customTimer.stop("After foreach");

  customTimer = new CustomTimer();
  Iterator<Integer> iterator = myList.iterator();
  while (iterator.hasNext()) {
   iterator.next();
  }
  customTimer.stop("After Iterator");

  customTimer = new CustomTimer();
  myList.stream().forEach((value) -> {
   // System.out.println(value);
   });
  customTimer.stop("After new stream collection");
 }
}


Here is the sample output.

##End time : 3247 ms for class/method : After List Creation
##End time : 95 ms for class/method : After Traditional for
##End time : 172 ms for class/method : After foreach
##End time : 124 ms for class/method : After Iterator
##End time : 141 ms for class/method : After new stream collection

You might want to run this few more times !.

If you want to try LinkedList   then you should not use traditional for loop !!

Thanks for reading.

Friday 30 May 2014

Netbeans GUI and Code Editor Font Problem on Linux Mint and Ubuntu

          I used Netbeans IDE for Java development from college days.
I was working on Windows for long time. But my new company
uses Linux mint for development.
          I installed Netbeans IDE on Linux Mint and the IDE font and
 editor was looking very bad. It was not possible to work 8 hours a day
by looking at ugly font.
(Open Image in new Tab to see the actual image and zoom it to 100% : older look on Linux Mint 17).
















(Disclaimer : Do this process at your own risk, you must be able to revert back these 
changes if it fails for you.) 
Note that GUI font and editor font are two different things.
GUI font= Overall IDE font
Editor font= Text editor where you type code.
I did some font tricks as follows ,
First I changed editor font to "Droid Sans Mono 15"

To change GUI font you have to change system wide font .
Following process will change font of whole desktop environment and not just 
Netbeans IDE. Please note and remember your current font setting before applying
changes mentioned below. Also this will work only on "native" look and feel. 
e.g.  Nimbus laf was not looking good. I am using Oracle JDK 8.
To change GUI font , I have added the font "Open Sans" from "http://www.google.com/fonts/specimen/Open+Sans" (Use OpenSans-Regular.ttf and
 just double click and click install button.)
(See http://stackoverflow.com/questions/14170113/download-googles-open-sans-font
for download instructions.) to my Linux installation and changed default system font to
 "Open Sans 11"

You may prefer to choose different font size.

For Linux Mint : Go to System Settings => Fonts =>Default font to Open Sans 11

To change default font on Ubuntu
http://askubuntu.com/questions/351595/change-default-font-in-ubuntu-13-04

Remember above will change system wide font  therefore do remember your current font 
settings to revert back changes in case of failure, but this font was OK for me.
Log off and log in again.

Now IDE is looking as follows.
(Open Image in new Tab to see the actual image and zoom it to 100%
: new look in Linux Mint).















(Open Image in new Tab to see the actual image and zoom it to 100%: new look in Ubuntu).














Please feel free to ask any queries if above process is complicated for you.
Happy codding !