Results 1 to 4 of 4

Thread: Collections in Java

  1. #1
    Join Date
    Feb 2010
    Posts
    136

    Collections in Java

    Collections in Java


    1. Collection:
    A collection represents a group of objects, known by its elements. Some collections accept duplicates, others not. Some are ordained, some not. Some collections emit some restrictions, such as banning or null. All collections implement JAVA interface collection through sub interfaces like Set, Map or List. Examples include ArrayList, HashSet, LinkedList or HashMap as known collections.

    2. Tables
    A table is a list of items. This is not a collection exactly, because an array does not implement the Collection interface. However, its usefulness is familiar. The size and type of array elements are fixed to the building last. Unlike a real collection, a table does not necessarily an object, but can also accept a primitive type like int or char. A table is not thread safe at the base, i.e it has no special protection in its implementation if two threads simultaneously access the table and at least one of modifies them.
    Here is an example of operations on an array of integers:
    Code:
    int [] marr = new int[20] ; // creation a board of 20 integers
    marr [0]=15 ; // insertion to first Index of the whole 15
    System.out.System.out.println("My integer is : " + marr [0]) ; // view

  2. #2
    Join Date
    Feb 2010
    Posts
    136

    Re: Collections in Java

    3. Collections of type List
    A list is an ordered collection. The user thereof has complete control over the elements it inserts, and can be accessed by their integer index. The first element in the list is always zero.

    3.1. Linked lists
    A linked list is a list where each element is connected to the following by referring to the latter. A linked list can be used as part of a simulated stack or queue, FIFO (First In First Out) or FILO (First In Last Out). The size of a LinkedList is not fixed, we can add and remove items according to our needs. We also note that the LinkedList accept all types of objects. LinkedList is not Thread Safe. Each element contains a reference to the following. Note that the tail has no following. Its. following is actually null.
    Here is an example of operations on a linked list:
    Code:
    List mlst=new LinkedList() ; // we creates our list chained
    mlst.add(new Integer(1)) ; // we adds the whole 1 to the list
    mlst.add(new Float(2.15)) ; // we adds the Floating 2.15 to the list
    /* We remark here that the whole 1 is the head of the list and that the Floating
     * is the tail of the list. */
    Integer exint=(Integer) mlst.getFirst() ; // we forget not of do the cast
    Float exflt=(Float) mlst.getLast(); // we forget not of do the cast
    mlst.remove(0) ; // we withdraws the whole , the Floating becomes the head
    Note that the complexity of operations on a LinkedList (linked list) is proportional to the index on which it performs these operations. Thus, a transaction index 0 will take less time than operation on the index 25, which is logical when conducting an operation on an element n of a linked list, the JVM must go through n-1 preceding the element n. It is also noteworthy that the addition at the end of the list is very inexpensive machine resources.

    A practical application of linked lists can be an execution stack. Indeed, when running a program, an execution stack is created. When calling a procedure, the address of the calling code is stored in the execution stack, only to return when the procedure is completed.

    The main operations on a linked list are:
    - add (Object o): Adds an object o at the end of list
    - addFirst (Object o): Adds an object o top of the list
    - addlast (Object o): Adds an object o at the end of list
    - clear (): Empty list
    - contains (Object o) returns true if the list contains the object o, false otherwise.
    - getFirst () and getLast (): returns the first and last element of the list as Object. Do not forget the cast.
    - removeFirst () and removeLast (): removes the first and last element of the list.
    - size (): returns the size of the list


    3.2. ArrayList
    An ArrayList is an array that resizes automatically. It accepts all types of objects, including null. Each ArrayList instance has a capacity, which defines the number of elements that can be stored. As and when we add elements and that "excess" capacity, the size increases accordingly. ArrayList is not Thread Safe.
    An example of operations on an ArrayList:

    Code:
    List marrlst = new ArrayList(10) ; // we creates a ArrayList of size 10
    for(int i=0, i<30, i++) {
    	marrlst.add(new Integer(i)); // we adds 30 integers
    }
    Operations to access an object (get), recovery of the size (size) of iteration (iterator) and special iteration (ListIterator) are executed in constant time, i.e irrespective the ArrayList, these operations take the same time. The add operation for its time is amortized, meaning that the additions at the end and beginning of the list are faster. However, we must highlight the weakness of ArrayList is that to resize a lot of resources. It is therefore necessary to minimize the scaling, especially in defining a basic size large enough during its construction.

    The main operations on an ArrayList are:


    - add (Object o): adds the object o at the end of the ArrayList
    - clear () empties the ArrayList
    - get (int index): Returns the Object at the specified index. Returns an exception if you exceed the table (IndexOutOfBoundsException)
    - size (): returns the size of ArrayList

  3. #3
    Join Date
    Feb 2010
    Posts
    136

    Re: Collections in Java

    4. The collection of type Map
    A map is a collection that associates a key value. The key is unique, unlike the value that can be associated with multiple keys. The majority of collections of type Map are two constructors: a constructor parameter Map creating a blank, and a constructor taking a Map parameter creates a new map based on the Map passed as parameter.

    4.1. The HashTable
    A Hashtable is an implementation of Map that associates a key value. Any object, except null may be added. Here is an example of using HashTable, where it combines a number of months.
    Code:
    Map exmp = new Hashtable();
    exmp.could(new Integer(1),"January");
    exmp.could(new Integer(2),"February");
    exmp.could(new Integer(3),"March");
    exmp.could(new Integer(4),"April");
    exmp.could(new Integer(5),"May");
    exmp.could(new Integer(6),"June");
    exmp.could(new Integer(7),"July");
    exmp.could(new Integer(8),"August");
    exmp.could(new Integer(9),"September");
    exmp.could(new Integer(10),"October");
    exmp.could(new Integer(11),"November");
    exmp.could(new Integer(12),"December");
    The complexity of access to its key value depends on a function called hashCode defined in the key. This function is already defined in Object, but to optimize the search, it overloads in your own classes (if the key is an instance of a class of your creation) saves considerable time. The principle of this function is simple, it returns an integer. If the two objects are equal, the values returned by their respective function are equal, but the reverse is not true, which means that if two objects are unequal, their hashCode are not necessarily uneven. The HashTable is Thread Safe.

    4.2. The HashMap
    A HashMap is a collection similar to the Hashtable. Only two things are different
    - HashMap accept as key and value as null and
    - HashMap is not Thread Safe.

  4. #4
    Join Date
    Feb 2010
    Posts
    136

    Re: Collections in Java

    5. The collections of type Set
    A Set is a collection that does not duplicate, and at most once null. This means that two elements e1 and e2 of a set will not be equal by e1.equals (e2). Some sets are more restrictive than others, accept null or not a certain type of object. In case an addition of an element already present in the set were to make an exception of type ClassCastException or NullPointerException would be raised and addressed.

    5.1. The HashSet
    HashSet implementation is the most useful set. It can store objects without duplication. As mentioned above, exceptions are thrown if you attempt to insert an unauthorized object (as null). However, if one tries to add an object already present, the function add simply return false and will do nothing. To browse a HashSet, it is necessary to use an Iterator. It should be noted that the Map explained above using the Set to keep the key unique.
    Here is an example of operations on a HashSet:
    Code:
    Set hset=new HashSet(); // we creates our Set
    hset.add(new String("1")); // we adds of string any
    hset.add(new String("2"));
    hset.add(new String("3"));
    hset.add(new String("1")); // oops, itr have already added, the feature manages except clearance, and subject is not added
    Iterator itr=hset.iterator(); // we creates a Iterator for browse our HashSet
    while(itr.hasNext()) // as that has a next
    {
    	System.out.System.out.println(itr.next()); // we displays the next
    }
    /* Note that iteration is made randomly. */

    An HashMap Example which may interest you


    Code:
    import java.awt.image.BufferedImage;
    import java.net.URL;
    import javax.imageio.ImageIO;
    
    Public class ApMp
    {
    	ApMp()
    	{
    	}
    	Public BufferedImage charImage(String pt) // my feature
    	{
    		URL u; // our u to of load
    		try
    		{
    			u=getClass().getClassLoader().getResource(pt); // we load URL by report to pt
    			return ImageIO.read(u); // and we the returns
    		}catch (Exception e) // if ca was ill past (and there it is the drama ! ^^)
    		{
    			System.out.System.out.println("Unable of load the file : "+pt);
    System.out.System.out.println("The error returned : "+e.getMessage());
    System.exit(0);
    			return null;
    		}
    	}
    }
    6. Conclusion
    Java contains a large number of different collections, each tailored to different situations. The whole thing is to choose the collection in accordance with its needs. If you need more information on the methods and classes which are used in the different collection in Java, then you can visit the Sun's official site for Java API.

Similar Threads

  1. FaceBook - Cityville Collections
    By FolloW Me in forum Video Games
    Replies: 6
    Last Post: 14-02-2011, 11:36 AM
  2. Collections available in FrontierVille
    By Meditation in forum Video Games
    Replies: 4
    Last Post: 07-02-2011, 10:16 AM
  3. ConcurrentModificationException in Collections
    By ISAIAH in forum Software Development
    Replies: 5
    Last Post: 23-02-2010, 05:42 AM
  4. How to create type specific collections in java?
    By MAGAR in forum Software Development
    Replies: 5
    Last Post: 01-02-2010, 04:08 PM
  5. Collections in c#
    By Jagdish Gada in forum Software Development
    Replies: 5
    Last Post: 20-01-2010, 10:12 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,915,326.38696 seconds with 17 queries