• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
MELJUN CORTES Java Lecture Collections
 

MELJUN CORTES Java Lecture Collections

on

  • 220 views

MELJUN CORTES Java Lecture Collections

MELJUN CORTES Java Lecture Collections

Statistics

Views

Total Views
220
Views on SlideShare
220
Embed Views
0

Actions

Likes
0
Downloads
8
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • *not sure how small “small” is… hundreds? <br />

MELJUN CORTES Java Lecture Collections MELJUN CORTES Java Lecture Collections Presentation Transcript

  • The Collections Framework MELJUN CORTES Java Data Structures MELJUN CORTES
  • What You Should Learn  Definition  Types of Collections  Using Collections Iterating  Copying  Collections in Java 1.5 
  • Definition Collection  object that groups multiple elements into a single unit  an array is a collection
  • Definition Collections Framework  unified architecture for representing and manipulating collections
  • Definition Collections Framework  Three Elements:  Interfaces   Implementations   how you handle the collection the classes that actually hold the data Algorithms  useful routines
  • The Java Collections Framework  Interfaces Collection List Set Queue Map
  • The Java Collections Framework  Collection  super type of List, Set and Queue  List  stores elements in the order they were added, similar to an array only growable  Set  stores only unique elements, no duplicates  Queue  stores elements in preparation for processing, usually either by FIFO or by priority  Map  stores key-value pairs, like for a lookup table
  • Collection  Defines common methods      add addAll remove removeAll clear       contains containsAll isEmpty size toArray iterator
  • Lists  “growable array”  Elements ordered in the sequence they were added.  Allows you to get and set using and index myList.set(4, “Barracuda”); String fish = myList.get(4);
  • Lists Implementations:  ArrayList  LinkedList  Vector
  • Lists ArrayList  backed by an Object array  fast in getting and setting elements  slow in inserting and removing
  • Lists LinkedList  slow in getting and setting elements  fast in inserting and removing
  • Lists Vector  Just like an ArrayList only all methods are synchronized.  Was the only list available prior to Java 1.1.  ArrayList is preferred to Vector because programmer has control over synchronization.
  • Sets  No duplicates  Maintains its own order  Same interface as Collection
  • Sets Implementations:  HashSet  TreeSet
  • Sets  HashSet  Stores data in a hash table implementation.  Not sorted “naturally”.
  • Sets  TreeSet  Stores data in a binary tree to search for duplicates.  Sorted “naturally”  Implements SortedSet interface
  • Queues  For holding elements prior to processing.  Typically FIFO but may be ordered by priority  Implementations: LinkedList (FIFO)  PriorityQueue 
  • Queues  Methods element  offer  Peek  poll  remove 
  • Maps  An object that maps keys to values  Keys are unique Key (User ID – String) “C234D7” “A497X3” “B252M5” “R567B7” Value (some object)
  • Maps  Using a map: myMap.put(“T234Y9”, someObject);  Getting from a map: myMap.get(“T234Y9”);
  • Maps Implementations:  HashMap  TreeMap  HashTable
  • Maps  HashMap Keys are ordered using a hash table implementation, similar to HashSet.  Keys are not sorted “naturally”. 
  • Maps  TreeMap Uses a binary tree to order keys.  Keys are sorted “naturally”. 
  • Maps  HashTable Just like an HashMap only all methods are synchronized.  Was the only map available prior to Java 1.1.  HashMap is preferred to HashTable because programmer has control over synchronization. 
  • Maps  HashMap vs. TreeMap HashMap is usually faster than TreeMap.  Use TreeMap if you need keys to be sorted. 
  • Iterator  Prior to Java 1.5, Iterator was used to provide generic iteration for Collection implementations.
  • Iterator //example: collection of strings Iterator iter = collection.iterator(); while(iter.hasNext) { String s = (String) iter.next(); // do stuff with s }
  • Algorithms  java.util.Collections  Utility class where useful routines can be found.       Sorting Searching Finding Maximum, Minimum Reversing Shuffling more...
  • Java 1.5 Enhancements  Generics  For-Each Loop  Autoboxing
  • Generics  You can enforce type on the collection. List<String> list = new ArrayList<String>(); String s = list.get(5); // no need to cast! list.add(new Integer(5)); XXX // will not compile
  • For-Each Loop  Use the same loop construct for arrays and collections: for(String s : collection) { System.out.println(s); }
  • Autoboxing  You can now use primitives in Collections list.add(3); map.put(4857, employee);
  • Best Practices  Always use the Interface.  Always use the Iterator (1.4) or the For- Each Loop (1.5).