When and where to use different Java Collections FrameWorks Part I

This post focus on when are where to use different Java collections interafce and class.
1. ArrayList<E> is a mostly used if we want fast, random access of the elements.

Example . Suppose you have a data of different students grouped by department, you have a task to add new internation students all with their first name started with O and last name ending with A, whereas in the system, student are arranged in alphabetical order with their first name.

public class Student
{
    private List<String> students = new ArrayList<>();

    public List<String> addNewStudent( String fullName )
    {
        students.add(studentInsertionID(fullName),fullName);
        return students;
    }
    public Integer studentInsertionID ( String studentName )
    {
        char firstLetter = studentName.charAt(0);
        return Integer.valueOf(firstLetter);
    }

}

ArrayList offers constant time for the following operations: size, isEmpty, get, set, iterator, and listIterator; amortized constant time for the add operation; and linear time for other operations.
2. LinkeList<E>

we can consider using a LinkedList if fast adding and removing elements at the end of the list is required.
Example : In a shopping software system, you are requested to have a %20 discount on each products price by the arrival of the shopper he gets %20 ( * n ) discount [ where n = totalNumber of user], example if the first user access the system for a product os price $500 , he gets $500 ( 20 * 1 ) % = $100 , the second user wants to purchase $400 , he gets $400 ( 20 * 2 ) % = $160 , for third shopper buying a product worth $1500 = $1500 ( 20 * 3 ) = 900 … till the end

Do you notice addition and manipulation of new data from the end ?

Exactly ! LinkedList<E> is a perfect soltution to such probelm

public class Store
{
    public String discount ( double productPrice )
    {
        double newPrice = 0.01 * productPrice * Math.multiplyExact(20,shoppersArrival());
        if ( newPrice >= productPrice )
            return String.format("Processing #%.2f....\nSorry!!! No discount for you, because you are the number %d shopper , your product price is %.2f . ",productPrice,shoppersArrival(),productPrice);
        else
            return String.format("Processing $%.2f....\nWao!!! Your product discount price is %.2f, because you are the number %d shopper .",productPrice,newPrice,shoppersArrival());

    }
    public Integer shoppersArrival()
    {
        int n = 10;
        return n ;
    }


    public List<String> byDiscount ( String shoppersFullName )
    {
        List<String> addShoppersFromBack = new LinkedList<>();
        addShoppersFromBack.add(shoppersFullName);
        return addShoppersFromBack;
    }
}

The both variables use add method but their run time for manipulation of data varies . Hope you enjoy this ? Why not reading the part II of this post …

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: