collection classes

15
Collection classes

Upload: shanthinisampath

Post on 17-Feb-2016

214 views

Category:

Documents


1 download

DESCRIPTION

java collection classes

TRANSCRIPT

Page 1: Collection Classes

Collection classes

Page 2: Collection Classes
Page 3: Collection Classes

• The Java Collections Framework standardizes the way in which groups of objects are handled by your programs.

Page 4: Collection Classes
Page 5: Collection Classes

• The ArrayList Class• The ArrayList class extends AbstractList and implements

the List interface. ArrayList is a• generic class that has this declaration:

• class ArrayList<E>

• ArrayList has the constructors shown here:• ArrayList( )• ArrayList(Collection<? extends E> c)• ArrayList(int capacity)

Page 6: Collection Classes

1. // Demonstrate ArrayList.2. import java.util.*;3. class ArrayListDemo {4. public static void main(String args[]) {5. // Create an array list.6. ArrayList<String> al = new ArrayList<String>();7. System.out.println("Initial size of al: " + al.size());8. // Add elements to the array list.9. al.add("C");10. al.add("A");11. al.add("E");12. al.add("B");13. al.add("D");14. al.add("F");15. al.add(1, "A2");16. System.out.println("Size of al after additions: " + al.size());17. // Display the array list.18. System.out.println("Contents of al: " + al);19. // Remove elements from the array list.20. al.remove("F");21. al.remove(2);22. System.out.println("Size of al after deletions: " + al.size()); }System.out.println("Contents of al: " + al);}

The output from this program is shown here:Initial size of al: 0Size of al after additions: 7Contents of al: [C, A2, A, E, B, D, F]Size of al after deletions: 5Contents of al: [C, A2, E, B, D]

Page 7: Collection Classes
Page 8: Collection Classes

Linked LIST• public class LinkedList<E> extends AbstractSequentialList<E>

implements List<E>, Deque<E>, Cloneable, Serializable

Constructors • LinkedList()This constructs constructs an empty list.• LinkedList(Collection<? extends E> c) This constructs a list containing the elements of the specified

collection, in the order they are returned by the collection's iterat

Page 9: Collection Classes

• boolean add(E e)• void add(I• boolean addAll(Collection<? extends E> c)nt

index, E element)• boolean addAll(int

index, Collection<? extends E> c• void addFirst(E e)• void addLast(E e)

Page 10: Collection Classes

1. import java.util.*; 2. public class LinkedListDemo3. { public static void main(String[] args)4. { 5. // create a LinkedList 6. LinkedList list = new LinkedList();7. // add some elements 8. list.add("Hello");9. list.add(2); 10. list.add("Chocolate"); 11. list.add("10"); 12. // print the list 13. System.out.println("LinkedList:" + list); 14. // clear the list15. List.remove();

16. list.clear(); 17. // print the list 18. System.out.println("LinkedList:" + list); 19. } }

Page 11: Collection Classes

VectorVector implements a dynamic array. It is similar to ArrayList, but with two differences: Vectoris synchronized, and it contains many legacy methods that are not part of the CollectionsVector was reengineered to extend AbstractList andto implement the List interface

class Vector<E>Here, E specifies the type of element that will be stored.Here are the Vector constructors:Vector( )Vector(int size)Vector(int size, int incr)Vector(Collection<? extends E> c)

Page 12: Collection Classes
Page 13: Collection Classes

1. import java.util.*;2. class VectorDemo {3. public static void main(String args[]) {4. // initial size is 3, increment is 25. Vector<Integer> v = new Vector<Integer>(3, 2);6. System.out.println("Initial size: " + v.size());7. System.out.println("Initial capacity: " + v.capacity());8. v.addElement(1);9. v.addElement(2);10. v.addElement(3);11. v.addElement(4);12. System.out.println("Capacity after four additions: " + v.capacity());13. v.addElement(5);14. System.out.println("Current capacity: " + v.capacity());15. v.addElement(6);16. v.addElement(7);17. System.out.println("Current capacity: " + v.capacity());

The output from this program is shown here:Initial size: 0Initial capacity: 3Capacity after four additions: 5Current capacity: 5Current capacity: 7Current capacity: 9First element: 1Last element: 12Vector contains 3.Elements in vector:1 2 3 4 5 6 7 9 10 11 12

Page 14: Collection Classes

18. v.addElement(9);19. v.addElement(10);20. System.out.println("Current capacity: " +21. v.capacity());22. v.addElement(11);23. v.addElement(12);24. System.out.println("First element: " + v.firstElement());25. System.out.println("Last element: " + v.lastElement());26. if(v.contains(3))27. System.out.println("Vector contains 3.");28. // Enumerate the elements in the vector.29. Enumeration vEnum = v.elements();30. System.out.println("\nElements in vector:");31. while(vEnum.hasMoreElements())32. System.out.print(vEnum.nextElement() + " ");33. System.out.println();

Page 15: Collection Classes

• Iterator<Integer> vItr = v.iterator();• System.out.println("\nElements in vector:");• while(vItr.hasNext())• System.out.print(vItr.next() + " ");• System.out.println();