Java Collection Framework

Java Collection Framework
OliviaCutts Profile Pic
Published Date:01-08-2017
Your Website URL(Optional)
Java Collection Framework Version March 2009Framework  Interfaces (ADT, Abstract Data Types)  Implementations (of ADT)  Algorithms (sort)  java.util.  Java 5 released  Lots of changes about collections 2Interfaces IterableE CollectionE MapK,V QueueE ListE SetE SortedMapK,V SortedSetE Associative containers Group containers 3Implementations CollectionE MapK,V Sorted SetE QueueE ListE MapK,V Sorted SetE HashSet Linked Array TreeMap HashMap List List Linked Linked HashSet HashMap Priority Queue TreeSet 4Internals data structure Hash Resizable Balanced Hash table Linked list table array tree Linked list Set HashSet TreeSet LinkedHashSet List ArrayList LinkedList Map HashMap TreeMap LinkedHashMap classes interface 5Collection  Group of elements (references to objects)  It is not specified whether they are  Ordered / not ordered  Duplicated / not duplicated  Following constructors are common to all classes implementing Collection  T()  T(Collection c) 6Collection interface  int size()  boolean isEmpty()  boolean contains(Object element)  boolean containsAll(Collection c)  boolean add(Object element)  boolean addAll(Collection c)  boolean remove(Object element)  boolean removeAll(Collection c)  void clear()  Object toArray()  Iterator iterator() 7Collection example CollectionPerson persons = new LinkedListPerson(); persons.add( new Person(“Alice”) ); System.out.println( persons.size() ); CollectionPerson copy = new TreeSetPerson(); copy.addAll(persons);//new TreeSet(persons) Person array = copy.toArray(); System.out.println( array0 ); 8Map  An object that associates keys to values (e.g., SSN ⇒ Person)  Keys and values must be objects  Keys must be unique  Only one value per key  Following constructors are common to all collection implementers  T()  T(Map m) 9Map interface  Object put(Object key, Object value)  Object get(Object key)  Object remove(Object key)  boolean containsKey(Object key)  boolean containsValue(Object value)  public Set keySet()  public Collection values()  int size()  boolean isEmpty()  void clear() 10Map example MapString,Person people = new HashMapString,Person(); people.put( “ALCSMT”, //ssn new Person(“Alice”, “Smith”) ); people.put( “RBTGRN”, //ssn new Person(“Robert”, “Green”) ); Person bob = people.get(“RBTGRN”); if( bob == null ) System.out.println( “Not found” ); int populationSize = people.size(); 11Generic collections  From Java 5, all collection interfaces and classes have been redefined as Generics  Use of generics lead to code that is  safer  more compact  easier to understand  equally performing 12Generic list - excerpt public interface ListE void add(E x); IteratorE iterator(); public interface IteratorE E next(); boolean hasNext(); 13Example Using a list of Integers  Without generics ( ArrayList list ) list.add(0, new Integer(42)); int n= ((Integer)(list.get(0))).intValue();  With generics ( ArrayListInteger list ) list.add(0, new Integer(42)); int n= ((Integer)(list.get(0))).intValue();  + autoboxing ( ArrayListInteger list ) list.add(0,new Integer(42)); int total = list.get(0).intValue(); 14Group containers (Collections) CollectionE QueueE ListE SetE SortedSetEList  Can contain duplicate elements  Insertion order is preserved  User can define insertion point  Elements can be accessed by position  Augments Collection interface 16List additional methods  Object get(int index)  Object set(int index, Object element)  void add(int index, Object element)  Object remove(int index)  boolean addAll(int index, Collection c)  int indexOf(Object o)  int lastIndexOf(Object o)  List subList(int fromIndex, int toIndex) 17List implementations ArrayList LinkedList  get(n) get(n)  Constant time Linear time  Insert (beginning)  Insert (beginning) and delete while and delete while iterating iterating  Linear Constant 18List implementations  ArrayList  ArrayList()  ArrayList(int initialCapacity)  ArrayList(Collection c)  void ensureCapacity(int minCapacity)  LinkedList  void addFirst(Object o)  void addLast(Object o)  Object getFirst()  Object getLast()  Object removeFirst()  Object removeLast() 19Example I LinkedListInteger ll = new LinkedListInteger(); ll.add(new Integer(10)); ll.add(new Integer(11)); ll.addLast(new Integer(13)); ll.addFirst(new Integer(20)); //20, 10, 11, 13 20

Advise: Why You Wasting Money in Costly SEO Tools, Use World's Best Free SEO Tool Ubersuggest.