Saturday, 10 February 2018

List Interface


List interface::

An ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.
The List interface places additional stipulations, beyond those specified in the Collection interface, on the contracts of the iteratoraddremoveequals, and hashCode methods. Declarations for other inherited methods are also included here for convenience.
The List interface provides four methods for positional (indexed) access to list elements. Lists (like Java arrays) are zero based. Note that these operations may execute in time proportional to the index value for some implementations (the LinkedList class, for example). Thus, iterating over the elements in a list is typically preferable to indexing through it if the caller does not know the implementation.
The List interface provides a special iterator, called a ListIterator, that allows element insertion and replacement, and bidirectional access in addition to the normal operations that the Iterator interface provides. A method is provided to obtain a list iterator that starts at a specified position in the list.

ArrayList::
Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null. In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list. (This class is roughly equivalent to Vector, except that it is unsynchronized.)
The sizeisEmptygetsetiterator, and listIterator operations run in constant time. The add operation runs in amortized constant time, that is, adding n elements requires O(n) time. All of the other operations run in linear time (roughly speaking). The constant factor is low compared to that for the LinkedList implementation.
Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically. The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost.

How to create Arraylist:

public class LstStrm {

public static void main(String args[]) {
         
      //declare both right and left generics in <> in java6
List<String> names=new ArrayList<String>();
names.add("one");
names.add("two");
names.add("three");
names.add("four");
names.add("five");
System.out.println(" display list of elements in java6 ::"+names);

         // in java7you can't declare generics in right side <> ,compiler infers the left hand generic which type it is.
               List<String> names=new ArrayList<>();
names.add("one");
names.add("two");
names.add("three");
names.add("four");
names.add("five");
System.out.println(" display list of elements in java7 ::"+names);
}
}

LinkedList::

Linked list implementation of the List interface. Implements all optional list operations, and permits all elements (including null). In addition to implementing the List interface, the LinkedList class provides uniformly named methods to getremove and insert an element at the beginning and end of the list.By using this you can add elements in both forward and backward,also add elements using while iterating elements.

LinkedList<String> namesList=new LinkedList<>();
namesList.add("one");
namesList.add("two");
namesList.add("three");
namesList.add("four");
namesList.add("five");
namesList.addFirst("first");
namesList.addLast("lastele");

System.out.println(" display list of elements in java6 ::"+namesList);


No comments:

Post a Comment