Thursday, January 31, 2013

Go mobile with Java Magazine!

Java Magazine is an essential source of knowledge about Java technology, the Java programming language, and Java-based applications for people who rely on them in their professional careers, or who aspire to.

MAGAZINE FEATURES:
  • Current and past issues of Java Magazine, absolutely free!
  • Text-formatted articles designed for maximum mobile readability.
  • Download each issue, then return any time for offline reading.
  • Search the archive of available issues.
  • Bookmark your favorite articles.
  • Share your comments with other readers.
Issues will be available for download on a bimonthly basis.

Java Magazine on Android

Link:

Tuesday, January 29, 2013

Example of Collections.binarySearch(), in List of Date

Collections.binarySearch() searches the specified list for the specified object using the binary search algorithm. Details refer to the Java doc.

Example:

package javaapplication1;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaApplication1 {

    public static void main(String[] args) {
        
        Comparator<Date> comparator = new Comparator<Date>() {

            @Override
            public int compare(Date o1, Date o2) {
                return o1.compareTo(o2);
            }
        };
        
        List<Date> myList = new ArrayList<>();
        myList.add(new Date(2015-1900, 01, 01));
        myList.add(new Date(2013-1900, 01, 01));
        myList.add(new Date(2013-1900, 01, 02));
        myList.add(new Date(2012-1900, 10, 01));
        myList.add(new Date(2012-1900, 11, 01));
        myList.add(new Date(2015-1900, 01, 10));
        
        Collections.sort(myList, comparator);
        
        //Print all the elements in the List
        for(int i = 0; i < myList.size(); i++){
            System.out.println(myList.get(i).toString());
        }
        
        //find the closest element from the List
        Date target = new Date(2013-1900, 01, 02);
        int index = Collections.binarySearch(
                myList, 
                target, 
                comparator);
        System.out.println("closest to " + target.toString() + " : " + index);
        
        target = new Date(2013-1900, 01, 03);
        index = Collections.binarySearch(
                myList, 
                target, 
                comparator);
        System.out.println("closest to " + target.toString() + " : " + index);
        
        target = new Date(2012-1900, 10, 02);
        index = Collections.binarySearch(
                myList, 
                target, 
                comparator);
        System.out.println("closest to " + target.toString() + " : " + index);
    }

}

Example of Collections.binarySearch()
Example of Collections.binarySearch()


Sort List of Date

Example to sort a List of Date object using Collections.sort().

package javaapplication1;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaApplication1 {
     
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
        List<Date> myList = new ArrayList<>();
        myList.add(new Date(2015-1900, 01, 01));
        myList.add(new Date(2013-1900, 01, 01));
        myList.add(new Date(2013-1900, 01, 02));
        myList.add(new Date(2012-1900, 10, 01));
        myList.add(new Date(2012-1900, 11, 01));
        myList.add(new Date(2015-1900, 01, 10));
        
        Collections.sort(myList, new Comparator<Date>(){

            @Override
            public int compare(Date o1, Date o2) {
                return o1.compareTo(o2);
            }
        });
        
        for(int i = 0; i < myList.size(); i++){
            System.out.println(myList.get(i).toString());
        }

    }
    
}

Sort List of Date object
Sort List of Date object