Tags

, ,

https://leanpub.com/u/sanjibsinha

 

How we could manage a collection of data in Java? There are many options, different type of data structures.

You have already seen array. However, array has many limitations, such as it holds fixed number of values of a single type. You cannot change it.
To overcome such limitations, Java introduces core collection interfaces. They encapsulate different type of collections. You have learned the role of an interface, it is actually a contract. So it allows collections to be manipulated independently. The foundation of Java Collections Framework consists of such Core Collection Interfaces.
They are ‘Set’, ‘List’, ‘Queue’, ‘Deque’, and ‘Map’.
In the last problem, we have seen an example of ‘LinkedList’ class, which is a collection based on a Linked List. As the name suggests, it is a collection of container that holds data and each container has a link identifier that links it either to the next one or the previous one. The last link identifier is always ‘null’, because it points to nothing.


Now it goes on like this: A links to B, B links to C, C links to D and so on. If you want to add an object (suppose E) in between C and D, Java sets new link identifiers for everyone and the chain is not broken. It is automatically maintained as before. Only a new data structure emerges.
They are particularly good when you wan to perform many additions and deletions with a collection. You can add anywhere, delete any value anywhere.
The next problem will show you how a LinkedList works. However, I would not go any further. You will find the detail in my ‘Data Structures and Algorithm’ book.

package fun.sanjibsinha;

import java.util.LinkedList;

public class ProblemHundred {

public static void main(String[] args) {
//creating a linked list that contains strings
LinkedList<String> cart = new LinkedList<String>();
cart.add("Book");
cart.add("Grocery");
cart.add("Garments");
cart.add("Mobile");
/*
for(String item : cart){
System.out.print(item + ", ");
}

it will print
Book, Grocery, Garments, Mobile,
*/
//adding last element
cart.addLast("TV");
//adding first element
cart.addFirst("Bottle");
/*
for(String item : cart){
System.out.print(item + ", ");
}

it will now print: Bottle, Book, Grocery, Garments, Mobile, TV,
*/
cart.add(0, "Refrigerator");
cart.set(3, "Medicine");
/*
for(String item : cart){
System.out.print(item + ", ");
}

printing: Refrigerator, Bottle, Book, Medicine, Garments, Mobile, TV,
in the new list Grocery is replaced by Medicine
*/
//we can create a copy or back-up of the above collection
LinkedList<String> backUp = new LinkedList<String>(cart);
/*
System.out.println(backUp);
it gives us exact copy of the cart
[Refrigerator, Bottle, Book, Medicine, Garments, Mobile, TV]
*/
/*
System.out.println(cart.indexOf("Book"));
System.out.println(cart.get(2));
printing:
2
Book
*/
//we can remove items either using index or value
/*
cart.remove(0);
cart.remove("Refrigerator");
cart.removeFirst();
cart.removeLast();

*/
//wec can check whether the cart and the copy contains the same

if(cart.containsAll(backUp)){
System.out.println("It is same.");
} else {
System.out.println("Not same.");
}
//let us remove the last value from the copy
backUp.removeLast();
System.out.println(backUp);
//the last value TV is deleted
//it does not affect the original linked list
System.out.println(cart);
backUp.push("TV");
System.out.println(backUp);
//now the backup has been rearranged
// [TV, Refrigerator, Bottle, Book, Medicine, Garments, Mobile]

}

}

You need to read the comments to understand how the LinkedList works actually. All methods have not been shown here. We have added, deleted, moved objects; the output shows us the result.

//output of code 100
It is same.
[Refrigerator, Bottle, Book, Medicine, Garments, Mobile]
[Refrigerator, Bottle, Book, Medicine, Garments, Mobile, TV]
[TV, Refrigerator, Bottle, Book, Medicine, Garments, Mobile]

I hope you get an idea about data structures; although this is a very basic idea.