Friday, July 22, 2011

Particular value exists in Java TreeSet

    /*
      Check if a particular value exists in Java TreeSet example
      This Java Example shows how to check if TreeSet object contains a particular
      value using contains method of TreeSet class.
    */
     package org.best.example;
    import java.util.TreeSet;
    
    public class CheckValueOfTreeSetExample {
    
    public static void main(String[] args) {
    
    //create TreeSet object
    TreeSet tSet = new TreeSet();
    
    //add elements to TreeSet
    tSet.add("1");
    tSet.add("3");
    tSet.add("2");
    tSet.add("5");
    tSet.add("4");
    
    /*
      To check whether a particular value exists in TreeSet use
      boolean contains(Object value) method of TreeSet class.
      It returns true if the TreeSet contains the value, otherwise false.
      */
    
    boolean blnExists = tSet.contains("3");
    System.out.println("3 exists in TreeSet ? : " + blnExists);
    }
    }
    
    /*
    Output would be
    3 exists in TreeSet ? : true
    */

Thursday, July 21, 2011

Java Questions - 18

How could Java classes direct program messages to the system console, but error messages, say to a file?

The class System has a variable out that represents the standard output, and the variable err that represent the standard error device. By default, they both point at the system console. This how the standard output could be re-directed:
Stream st = new Stream(new FileOutputStream("output.txt")); System.setErr(st); System.setOut(st);

Enumerate through a Vector using Java Enumeration Example

/*
Enumerate through a Vector using Java Enumeration Example
This Java Example shows how to enumerate through elements of a Vector
using Java Enumeration.
*/

package org.best.example;
import java.util.Vector;
import java.util.Enumeration;

public class EnumerateThroughVectorExample {

public static void main(String[] args) {

//create a Vector object

Vector v = new Vector();

//populate the Vector

v.add("One");
v.add("Two");
v.add("Three");
v.add("Four");

//Get Enumeration of Vector's elements using elements() method

Enumeration e = v.elements();

/*
Enumeration provides two methods to enumerate through the elements.
It's hasMoreElements method returns true if there are more elements to
enumerate through otherwise it returns false. Its nextElement method returns
the next element in enumeration.
*/

System.out.println("Elements of the Vector are : ");

while(e.hasMoreElements())
         System.out.println(e.nextElement());
}

}
/*
Output would be
Elements of the Vector are :
One
Two
Three
Four
*/

Wednesday, July 20, 2011

Java Questions - 17

Describe the wrapper classes in Java.

Wrapper class is wrapper around a primitive data type. An instance of a wrapper class contains, or wraps, a primitive value of the corresponding type.

Following table lists the primitive types and the corresponding wrapper classes:
Primitive Wrapper
boolean  - java.lang.Boolean
byte - java.lang.Byte
char - java.lang.Character
double - java.lang.Double
float - java.lang.Float
int - java.lang.Integer
long - java.lang.Long
short - java.lang.Short
void - java.lang.Void

Creating a Shared File Lock on a File

package org.best.example;

public static void main(String[] args)
    {
         try {
         // Obtain a file channel
         File file = new File("filename");
         FileChannel channel = new RandomAccessFile(file, "rw").getChannel();
       
         // Create a shared lock on the file.
         // This method blocks until it can retrieve the lock.
         FileLock lock = channel.lock(0, Long.MAX_VALUE, true);
       
         // Try acquiring a shared lock without blocking. This method returns
         // null or throws an exception if the file is already exclusively locked.
             try {
             lock = channel.tryLock(0, Long.MAX_VALUE, true);
             } catch (OverlappingFileLockException e) {
             // File is already locked in this thread or virtual machine
         }
       
         // Determine the type of the lock
         boolean isShared = lock.isShared();
       
         // Release the lock
         lock.release();
       
         // Close the file
         channel.close();
         } catch (Exception e) {
     }
}

Tuesday, July 19, 2011

Java Questions - 16

What's the main difference between a Vector and an Array List?

Java Vector class is internally synchronized and Array List is not synchronized.

Creating a File Lock on a File

package org.best.examples;

public static void main(String[] args)
     {
         try {
         // Get a file channel for the file
         File file = new File("Test.java");
         FileChannel channel = new RandomAccessFile(file, "rw").getChannel();
       
         // Use the file channel to create a lock on the file.
         // This method blocks until it can retrieve the lock.
         FileLock lock = channel.lock();
       
         // Try acquiring the lock without blocking. This method returns
         // null or throws an exception if the file is already locked.
             try {
             lock = channel.tryLock();
             } catch (OverlappingFileLockException e) {
             // File is already locked in this thread or virtual machine
         }
       
         // Release the lock
         lock.release();
       
         // Close the file
         channel.close();
         } catch (Exception e) {
     }
}