Thursday, September 15, 2011

Useful Java util Classes

There are some useful classes defined in java util package. Lets visit them:

Arrays

This class contains methods for manipulating arrays (such as sorting and searching). This class also contains a static factory that allows arrays to be viewed as lists.

Integer[] array = new Integer[] {5, 3, 1, 4, 2, 4, 9};

// sort array
Arrays.sort(array);

// search integer 4 in the array and return the index using binary search algorithm
// note that the array must be in sort
int index = Arrays.binarySearch(array, 4);

// fill or replace index 2 (inclusive) until index 5 (exclusive) with integer 100
Arrays.fill(array, 2, 5, 100);

// create a List from an array
List list = Arrays.asList(array);

Collections

This class consists of methods that operate on or return collections.

List src = new ArrayList();
src.add(new Integer(5));
src.add(new Integer(3));
src.add(new Integer(1));
src.add(new Integer(4));
src.add(new Integer(2));
src.add(new Integer(4));
src.add(new Integer(9));

// reverse the order of the elements in the list
Collections.reverse(src);

// get max Integer element in the list
Integer result = Collections.max(src);

// get min Integer element in the list
Integer result = Collections.min(src);

// replace all integer 4 with integer 1000 in the list
Collections.replaceAll(src, new Integer(4), new Integer(1000));

// replace oldBean with newBean, they are compared using Object.equals() method
Collections.replaceAll(src, oldBean, newBean);

// swap objects in index 0 and 6
Collections.swap(src, 0, 6);

// replace all objects in the list with the new integer object
Collections.fill(src, new Integer(1999));

// sort objects in the list
Collections.sort(src);

// search an element in the list. The list must be sorted first
Collections.binarySearch(src, key);

// shuffle the list
Collections.shuffle(src);

StringBuffer & StringBuilder

Both classes are used to deal with string, however, StringBuffer is thread-safe because it performs thread synchronization. StringBuilder should be used when we don't need thread synchronization.

SimpleDateFormat

SimpleDateFormat is a class for formatting and parsing date (date to text and vice versa). Date and time formats are specified by date and time pattern strings.



// create SimpleDateFormat object
SimpleDateFormat format = new SimpleDateFormat("'on' dd/MMMM/yyyy");

// format date to string
String formattedDate = format.format(new Date()); // result: on 19/September/2011

// format string to date
Date date = format.parse("on 12/August/2001");

Properties

This class represents a persistent set of properties. The Properties can be saved to a stream or loaded from a stream. Each key and its corresponding value in the property list is a string.

// create Properties object
Properties properties = new Properties();

// load Properties from input stream
InputStream inStream = new FileInputStream("properties.properties");
properties.load(inStream);

// get property
String name = properties.getProperty("property.name");
String size = properties.getProperty("property.size");

// set property
properties.setProperty("property.size", "101");

// persistent property
OutputStream out = new FileOutputStream("properties.properties");
properties.store(out, "Comments here");

The content of properties file:

property.name=Rochmat Santoso
property.size=101

0 comments:

 

©2009 Stay the Same | by TNB