Wednesday, July 29, 2015

How to Read and Write from RandomAccessFile in Java

A FileInputStream lets you read data from a file whereas a FileOutputStream lets you write data to a file. A random access file is a combination of both. Using a random access file, you can read from a file as well as write to the file. Reading and writing using the file input and output streams are a sequential process. Using a random access file, you can read or write at any position within the file (hence the name random access). In this article, we'll try to see how to use RandomAccessFile with the help of an example.


An object of the RandomAccessFile class facilitates the random file access. It lets you read/write bytes and all primitive types values to a file. It also lets you work with strings using its readUTF() and writeUTF() methods. The RandomAccessFile class is not in the class hierarchy of the InputStream and OutputStream classes

Access Mode of Random access file
A random access file can be created in four different access modes. In its constructor, you must specify the access mode. The access mode value is a string. They are listed as follows:

  1. "r" : The file is opened in a read-only mode. You will receive an IOException if you attempt to write to the file.
  2. "rw" : The file is opened in a read-write mode. The file is created if it does not exist.
  3. "rws" : Same as the "rw" mode, except that any modifications to the file’s content and its metadata are written to the storage device immediately.
  4. "rwd" : Same as the "rw" mode, except that any modifications to the file’s content are written to the storage device immediately.

You create an instance of the RandomAccessFile class by specifying the file name and the access mode as shown:

RandomAccessFile raf = new RandomAccessFile("randomtest.txt", "rw");


File Pointer
A random access file has a file pointer that is advanced when you read data from it or write data to it. The file pointer is a kind of cursor where your next read or write will start. Its value indicates the distance of the cursor from the beginning of the file in byes. You can get the value of file pointer by using its getFilePointer() method. When you create an object of the RandomAccessFile class, the file pointer is set to zero, which indicates the beginning of the file. You can set the file pointer at a specific location in the file using the seek() method.

Length function
The length() method of a RandomAccessFile returns the current length of the file. You can extend or truncate a file by using its setLength() method. If you extend a file using this method, the contents of the extended portion of the file are not defined.


Reading from and writing to a random access file is performed the same way you have been reading/writing from/to any input and output streams.


Example 
Following example explain the use of a random access file. When you run this program, it writes two things to a file: the file read counter, which keeps track of how many times a file has been read using this program, and a text message of "Hello Java Latte". The program increments the counter value in the file every time it reads the file. The counter value keeps incrementing when you run this program repeatedly. You may get a different output every time you run this program.

I hope this example has given you the brief notion of Random Access File class and how it works.



If you know anyone who has started learning Java, why not help them out! Just share this post with them.Thanks for studying today!...

Saturday, July 25, 2015

Syntax for Lambda expression in Java

In this article, we try to understand the very basic use of Lambda expression with syntax and many examples. If you want to look what is Lambda and the basic idea behind it, I encourage to read this my article on the basic. I will try to cover all the basic use of Lambda expression in the tutorial.


Syntax for Lambda Expressions
A lambda expression describes an anonymous function. The general syntax for using lambda expressions is very similar to declaring a method. The general syntax is
A lambda expression consists of a list of parameters and a body that are separated by an arrow (->).
The list of parameters is declared the same way as the list of parameters for methods. The list of parameters is enclosed in parentheses, as is done for methods.
The body of a lambda expression is a block of code enclosed in braces. Like a method's body, the body of a lambda expression may declare local variables; use statements including break, continue, and return; throw exceptions, etc.

Unlike a method, a lambda expression does not have four parts.

  1. A lambda expression does not have a name
  2. A lambda expression does not have a return type. It is inferred by the compiler from the context of its use and from its body
  3. A lambda expression does not have a throws clause. It is inferred from the context of its use and its body.
  4. A lambda expression cannot declare type parameters. That is, a lambda expression cannot be generic


Basic example to understand Lambda
Lambda expression are invoked via Function Interface.

Function Interface
A functional interface is simply an interface that has exactly one abstract method. The following types of methods in an interface do not count for defining a functional interface:

  • Default methods
  • Static methods
  • Public methods inherited from the Object class

More detail on Function detail.

Runnable is an example of Function interface, (check Runnable Java 8 doc) so we can use Runnable interface in term of Lambda expression.
Runnable interface method does not take any input and does not return anything. So it lambda expression will look like
() -> {  statements;  }


Example
Similarly, Java 8 has many Functional interface for different purpose. Here is the list of commonly used function interface in the package java.util.function


Following table contains some examples of lambda expressions and equivalent methods. I have given a suitable name to methods as you cannot have a method without a name in Java. The compiler infers the return type of lambda expressions.


Example : how to use above lambda expression with function interface.


One of the goals of the lambda expression was to keep its syntax concise and let the compiler infer the details. The following sections discuss the shorthand syntax for declaring lambda expressions.


Omitting Parameter Types
You can omit the declared type of the parameters. The compiler will infer the types of parameters from the context in which the lambda expression is used

// Types of parameters are declared
(int x, int y) -> { return x + y; }

// Types of parameters are omitted
(x, y) -> { return x + y; }

If you omit the types of parameters, you must omit it for all parameters or for none. You cannot omit for some and not for others. The following lambda expression will not compile because it declares the type of one parameter and omits for the other:

// A compile-time error
(int x, y) -> { return x + y; }


Declaring a Single Parameter
Sometimes a lambda expression takes only one parameter. You can omit the parameter type for a single parameter lambda expression as you can do for a lambda expression with multiple parameters. You can also omit the parentheses if you omit the parameter type in a single parameter lambda expression. The following are three ways to declare a lambda expression with a single parameter:

// Declares the parameter type
(String msg) -> { System.out.println(msg); }

// Omits the parameter type
(msg) -> { System.out.println(msg); }

// Omits the parameter type and parentheses
msg -> { System.out.println(msg); }

The parentheses can be omitted only if the single parameter also omits its type. The following lambda expressionwill not compile:

// Omits parentheses, but not the parameter type, which is not allowed.
String msg -> { System.out.println(msg); }



Declaring No Parameters
If a lambda expression does not take any parameters, you need to use empty parentheses.

// Takes no parameters
() -> { System.out.println("Hello"); }

It is not allowed to omit the parentheses when the lambda expression takes no parameter. The following declaration will not compile:

-> { System.out.println("Hello"); }



Parameters with Modifiers
You can use modifiers, such as final, in the parameter declaration for explicit lambda expressions. The following two lambda expressions are valid:

(final int x, final int y) -> { return x + y; }

(int x, final int y) -> { return x + y; }

The following lambda expression will not compile because it uses the final modifier in parameter declarations, but omits the parameter type:
(final x, final y) -> { return x + y; }


Declaring Body of Lambda Expressions
The body of a lambda expression can be a block statement or a single expression. A block statement is enclosed in braces; a single expression is not enclosed in braces
When a block statement is executed the same way as a method’s body. A return statement or the end of the body returns the control to the caller of the lambda expression.

When an expression is used as the body, it is evaluated and returned to the caller. If the expression evaluates to void, nothing is returned to the caller. The following two lambda expressions are the same; one uses a block statement and the other an expression

// Uses a block statement. Takes two int parameters and returns their sum.
(int x, int y) -> { return x + y; }

// Uses an expression. Takes a two int parameters and returns their sum.
(int x, int y) -> x + y

The following two lambda expressions are the same; one uses a block statement as the body and the other an expression that evaluates to void:

// Uses a block statement
(String msg) -> { System.out.println(msg); }

// Uses an expression
(String msg) -> System.out.println(msg)

Check out this link for more examples



If you know anyone who has started learning Java, why not help them out! Just share this post with them.Thanks for studying today!...

How to create thread in Java 8

In this article, we'll see a new way to create thread in Java 8 and also revise the basic two ways to create thread with the help of lambda expression. Main focus of the topic is method reference and how to use thread with lambda expression. If you want to revise the basic concept of thread check this post and this post for lambda concept.


Creating a Thread in Java

The Java API makes it easy to work with threads. It lets you represent a thread as an object. An object of the  java.lang.Thread class represents a thread. Creating and using a thread in Java is as simple as creating an object of the Thread class and using that object in a program. Let’s start with the simplest example of creating a thread in Java. There are at least two steps involved in working with a thread:

  1. Creating an object of the Thread class
  2. Invoking the start() method of the Thread class to start the thread

Creating an object of the Thread class is the same as creating an object of any other classes in Java. In its simplest form, you can use the default constructor of the Thread class to create a Thread object.
// create a new thread
Thread simpleThread  = new Thread();

Creating an object of the Thread class allocates memory for that object on the heap. It does not start or run the thread. After you have created an object of the Thread class, you must call its start() method to start the thread represented by that object.
//start the thread
simpleThread.start();


Example 
The start() method returns after doing some housekeeping work. It puts the thread in the runnable state. In this state, the thread is ready to receive the CPU time. Note that invoking the start() method of a Thread object does not guarantee “when” this thread will start getting the CPU time. That is, it does not guarantee when the thread will start running. It just schedules the thread to receive the CPU time.



Three ways to create thread
There are three ways you can specify your code to be executed by a thread

  1. By inheriting your class from the Thread class
  2. By implementing the Runnable interface in your class
  3. By using the method reference to a method that takes no parameters and returns void


Inheriting Your Class from the Thread Class
When you inherit your class from the Thread class, you should override the run() method and provide the code to be executed by the thread.


Implementing the Runnable Interface
You can create a class that implements the java.lang.Runnable interface. Runnable is a functional interface and it is declared as follows

@FunctionalInterface
public interface Runnable



Using a Method Reference
Check this post for detail on method reference. From Java 8, you can use the method reference of a method of any class that takes no parameters and returns void as the code to be executed by a thread.

The following code declares a ThreadTest class that contains an execute() method. The method contains the code to be executed in a thread.


The thread will execute the code contained in the execute() method of the ThreadTest class

Final example




If you know anyone who has started learning Java, why not help them out! Just share this post with them.Thanks for studying today!...

Tuesday, July 21, 2015

DelayQueue class and Delayed interface example in Java

In this article, we look into one of the implementations in java.util.concurrent support the extended BlockingQueue interface, that defines blocking versions of put and take i.e, DelayQueue.  An unbounded blocking queue of Delayed elements, in which an element can only be taken when its delay has expired. We'll try to understand the concept of DelayQueue class and Delayed interface with the help of example.



What is DelayQueue?
An unbounded blocking queue of Delayed elements, in which an element can only be taken when its delay has expired. In this class, you can store elements with an activation date. The methods that return or extract elements of the queue will ignore those elements whose data is in the future. They are invisible to those methods. Here Delayed elements are those elements who implements Delayed interface.


What is Delayed Interface?
A mix-in style interface for marking objects that should be acted upon after a given delay.

This interface allows you to work with delayed objects, so you will implement the activation date of the objects stored in the DelayedQueue class as the time remaining until the activation date. This interface forces to implement the following two methods

  • compareTo(Delayed o): The Delayed interface extends the Comparable interface. This method will return a value less than zero if the object that is executing the method has a delay smaller than the object passed as a parameter, a value greater than zero if  the object that is executing the method has a delay bigger than the object passed as a parameter, and the zero value if both objects have the same delay.
  •  getDelay(TimeUnit unit): This method has to return the time remaining until the activation date in the units is specified by the unit parameter. The TimeUnit class is an enumeration with the following constants: DAYS, HOURS, MICROSECONDS, MILLISECONDS, MINUTES, NANOSECONDS, and SECONDS.

The head of the queue is that Delayed element whose delay expired furthest in the past. If no delay has expired there is no head and poll will return null. Expiration occurs when an element's getDelay(TimeUnit.NANOSECONDS) method returns a value less than or equal to zero. Even though unexpired elements cannot be removed using take or poll, they are otherwise treated as normal elements. This queue does not permit null elements.


Example
In this example, you will learn how to use the DelayedQueue class storing in it some events with different activation dates

we have implemented the Event class. That class has a unique attribute, the activation date of the events, and implements the Delayed interface, so you can store Event objects in the DelayedQueue class.



The getDelay() method returns the number of nanoseconds between the activation date and the actual date. Both dates are objects of the Date class. You have used the getTime() method that returns a date converted to milliseconds and then, you have converted that value to TimeUnit received as a parameter. The DelayedQueue class works in nanoseconds, but at this point, it's transparent to you.

The compareTo() method returns a value less than zero if the delay of the object executing the method is smaller than the delay of the object passed as a parameter, a value greater than zero if the delay of the object executing the method is bigger than the delay of the object passes as a parameter, and the value 0 if both delays are equal.


This class has an integer attribute named id. When a Task object is executed, it adds a number of seconds equal to the ID of the task to the actual date and that is the activation date of the events stored by this task in the DelayedQueue class. Each Task object stores 10 events in the queue using the add() method.

Finally, in the main() method of the Main class, you have created sixTask objects and executed them in the corresponding threads. When those threads finish their execution, you have written to the console all the events using the poll() method. That method retrieves and removes the first element of the queue. If the queue does not have any active element, the method returns the null value. You called the poll() method and if it returns an Event class, you increment a counter. When the poll() method returns the null value, you write the value of the counter in the console and put the thread to sleep during half a second to wait for more active events. When you have obtained the 500 events stored in the queue, the execution of the program finishes


You can see how the program only gets 10 events when it is activated.

The DelayQueue class has other interesting methods, which are as follows:

  • clear(): This method removes all the elements of the queue.
  • offer(E e): E represents the class used to parameterize the DelayQueue class. This method inserts the element passed as a parameter in the queue.
  • peek(): This method retrieves, but doesn't remove the first element of the queue.
  • take(): This method retrieves and removes the first element of the queue. If there aren't any active elements in the queue, the thread that is executing the method will be blocked until the thread has some active elements.



If you know anyone who has started learning Java, why not help them out! Just share this post with them. Thanks for studying today!...