Sunday, June 23, 2013

Java Collections Framework Part 1 - Intro

Continuing with another series of articles about java, now I'm going to try to explain the java collections framework, hoping you can get something from it.

A collection in java is an object that represents a data structure which groups data or other objects.
The collection framework is a set of interfaces and classes from the packages java.util and java.util.concurrent, some of the core interfaces are Collection, Set, List, Map, Queue, SortedSet, SortedMap, and so on. Each one of these interfaces has a model or structure for organizing objects.

The next figure shows the interface and class hierarchy of the interfaces and classes in the collection framework.


The basic operations in a collection are add, remove and find an object, also iterate objects through the collection.

The main types of collections are List, Set , Queue and Map. List, Set, Queue extend from the Collection interface, and Map doesn't, a brief description of each type of collection is the following:

List: A list is a collection that groups objects with an order, assigns an index to each element.

Set: A set is a collection that groups object without an order and without duplicates, each element is unique.

Queue: A queue is a collection that groups objects with a certain order, they are arranged by the order in which they are processed.

Map: A map is a collection that associates each object with an identifier, it uses key-value associations to store and retrieve elements, also the identifiers must be unique.

Each of this type is represented by an interface in the collection framework as the previous figure shows, and an implementation of a collection (concrete class) implements one of this interface to define its type, but it can implement other interfaces to add or achieve another behavior.

A collection can be sorted, unsorted, ordered and unordered.

An ordered collection is a collection that can be iterated in a specific order, lists are indexed so they can be iterated through their index, another order would be the LIFO (Last-In, First-Out) or FIFO (First-In, First-Out) collections.

A sorted collection is a collection that its elements were set by a rule, like a natural order, its position in the collection is determined by a condition or rule set by the programmer. To achieve this the collection framework uses the java.lang.Comparable interface and the java.util.Comparator interface.

There are some restrictions or recommendations in the implementation of the elements of some collections such as the ones that no duplicate are allowed or its elements are sorted. So is good to know some of these before getting into these collections.

The equals() method of the java.lang.Object class tests if two objects are equal, if this method is not overridden in an object its behavior is given by its default implementation which only tests if two objects reference are the same like the “==” operator. To provide a better implementation of the equals() it must be overridden and test if the variables of an object are the same to one that is compared to .

Example
 public class Person {  
      private String name;  
      ...  
       public boolean equals(Object obj) {  
           if (this == obj) {  
             return true;       
           }  
           if (!obj instanceof Person) {  
             return false;  
           }  
           .......  
           return this.name.equalsIgnoreCase((((Person)obj).name);  
      }  
 }  

In the example, the equals() test for if the name of the objects is the same then both objects are equal.

The hashCode() method of the java.lang.Object class returns a hashcode for the object and its contract says If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result, if two objects are unequal() it is recommended to return different values.
In order to accomplish this restriction of two equal objects return the same hashcode, it is recommended to use in the hashcode() the same instance variables used in the equals().

Example
 public class Person {  
      private String name;  
      ...  
       public boolean equalsIgnoreCase(Object obj) {  
           if (this == obj) {  
             return true;       
           }  
           if (!obj instanceof Person) {  
             return false;  
           }  
           .......  
           return this.name.equals(((Person)obj).name);  
      }  
      public int hashCode() {   
           return this.name.hashCode();   
      }  
 }  

In the example two equals objects would return the same hashcode and two different objects would return different hashcodes.

You have to know that there is more about these methods or contracts, but for the purpose of the blog with this basic information is alright.

These methods are used in the “hash“ collections that don't allow duplicate elements.

The java.lang,Comparable interface must be implemented by elements of a collection that is sorted, it helps to give a natural order to the collection. It only has the compareTo() method, it compares ans object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

Example:
 public class Person implements Comparable <Person>{  
      private String name;  
      ......       
      public int compareTo(Person p) {   
           return this.name.compareTo(p);   
      }  
 }  

In the expample since the name attribute is a String an Strings implement the comparable interface the is ok to call this method on the attribute.

Go to part 2

3 comments:

  1. It is really a great work and the way in which u r sharing the knowledge is excellent.
    Thanks for helping me to understand basic concepts. As a beginner in java programming your post help me a lot.Thanks for your informative article.java training in chennai

    ReplyDelete
  2. perfect explanation about java programming .its very useful.thanks for your valuable information.java training in chennai | java training in velachery

    ReplyDelete