Thursday 10 September 2015

Collections Frameworks


List

Package: java.util.*;

  • An ordered collection (also known as a sequence).
  • The user can access elements by their integer index (position in the list), and search for elements in the list.
  • Unlike sets, lists typically allow duplicate elements.
  • They typically allow multiple null elements if they allow null elements at all.
  • Lists (like Java arrays) are zero based.
  • 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.
  • The List interface provides two methods to search for a specified object.
  • The List interface provides two methods to efficiently insert and remove multiple elements at an arbitrary point in the list.

Some methods those demand attention –


  • size()Returns the number of elements in this list. If this list contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE. <br>
  • toArray(T[] a)Returns an array containing all of the elements in this list in proper sequence (from first to last element). The runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list. If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the list is set to null.
                          List<Integer> list = new ArrayList();
  list.add(1);

  list.add(2);

  list.add(3);

  list.add(4);


Integer i[] = new Integer[10]; // size > list.size()
Arrays.fill(i, 0);
list.toArray(i); // works on same array
System.out.println(Arrays.toString(i));
// output is [1, 2, 3, 4, null, 0, 0, 0, 0, 0]
Integer i[] = new Integer[2]; // size < list.size()
Arrays.fill(i, 0);
Integer j[] = list.toArray(i); // new array is returned
System.out.println(Arrays.toString(j));
// output is [1, 2, 3, 4]

  • hashCode() Returns the hash code value for this list. The hash code of a list is defined to be the result of the following calculation:                      

                                int hashCode = 1;                                
                                for (E e : list)
                                hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());

         This ensures that list1.equals(list2) implies that list1.hashCode()==list2.hashCode() for any two lists, list1 and list2, as required by the general contract of Object.hashCode().

                                List<Integer> al1 = new ArrayList<>();
                                al.add(1);
                                al.add(2);
                                List<Integer> al2 = new ArrayList<>(al1);
                                System.out.println(al1.equals(al2)); // output is true
                                System.out.println(al1.hashCode()==al2.hashCode()); // output is true
  • subList(int fromIndex, int toIndex) Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned list is empty). The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa. The returned list supports all of the optional list operations supported by this list. This method eliminates the need for explicit range operations. Any operation that expects a list can be used as a range operation by passing a subList view instead of a whole list.
                                List<Integer> list = new ArrayList();
                                list.add(1);
                                list.add(2);
                                list.add(3);
                                list.add(4);
                                list.subList(1, 3).add(5); // 5 is added at position 3
                                System.out.println(list); // output is [1, 2, 3, 5, 4]
                                list.subList(1, 3).clear(); // elements from position 1 to 2 are removed
                                System.out.println(list); // output is [1, 5, 4]



         The semantics of the list returned by this method become undefined if the backing list (i.e., this list) is structurally modified in any way other than via the returned list. (Structural modifications are those that change the size of this list, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results).

            List<Integer> temp = list.subList(0, 4);
                      list.remove(2);
                             System.out.println(temp); // error : throws ConcurrentModificationException

No comments:

Post a Comment