MELJUN CORTES Java Lecture Collections

363 views

Published on

MELJUN CORTES Java Lecture Collections

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
363
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
19
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • *not sure how small “small” is… hundreds?
  • MELJUN CORTES Java Lecture Collections

    1. 1. The Collections Framework MELJUN CORTES Java Data Structures MELJUN CORTES
    2. 2. What You Should Learn  Definition  Types of Collections  Using Collections Iterating  Copying  Collections in Java 1.5 
    3. 3. Definition Collection  object that groups multiple elements into a single unit  an array is a collection
    4. 4. Definition Collections Framework  unified architecture for representing and manipulating collections
    5. 5. Definition Collections Framework  Three Elements:  Interfaces   Implementations   how you handle the collection the classes that actually hold the data Algorithms  useful routines
    6. 6. The Java Collections Framework  Interfaces Collection List Set Queue Map
    7. 7. 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
    8. 8. Collection  Defines common methods      add addAll remove removeAll clear       contains containsAll isEmpty size toArray iterator
    9. 9. 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);
    10. 10. Lists Implementations:  ArrayList  LinkedList  Vector
    11. 11. Lists ArrayList  backed by an Object array  fast in getting and setting elements  slow in inserting and removing
    12. 12. Lists LinkedList  slow in getting and setting elements  fast in inserting and removing
    13. 13. 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.
    14. 14. Sets  No duplicates  Maintains its own order  Same interface as Collection
    15. 15. Sets Implementations:  HashSet  TreeSet
    16. 16. Sets  HashSet  Stores data in a hash table implementation.  Not sorted “naturally”.
    17. 17. Sets  TreeSet  Stores data in a binary tree to search for duplicates.  Sorted “naturally”  Implements SortedSet interface
    18. 18. Queues  For holding elements prior to processing.  Typically FIFO but may be ordered by priority  Implementations: LinkedList (FIFO)  PriorityQueue 
    19. 19. Queues  Methods element  offer  Peek  poll  remove 
    20. 20. Maps  An object that maps keys to values  Keys are unique Key (User ID – String) “C234D7” “A497X3” “B252M5” “R567B7” Value (some object)
    21. 21. Maps  Using a map: myMap.put(“T234Y9”, someObject);  Getting from a map: myMap.get(“T234Y9”);
    22. 22. Maps Implementations:  HashMap  TreeMap  HashTable
    23. 23. Maps  HashMap Keys are ordered using a hash table implementation, similar to HashSet.  Keys are not sorted “naturally”. 
    24. 24. Maps  TreeMap Uses a binary tree to order keys.  Keys are sorted “naturally”. 
    25. 25. 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. 
    26. 26. Maps  HashMap vs. TreeMap HashMap is usually faster than TreeMap.  Use TreeMap if you need keys to be sorted. 
    27. 27. Iterator  Prior to Java 1.5, Iterator was used to provide generic iteration for Collection implementations.
    28. 28. Iterator //example: collection of strings Iterator iter = collection.iterator(); while(iter.hasNext) { String s = (String) iter.next(); // do stuff with s }
    29. 29. Algorithms  java.util.Collections  Utility class where useful routines can be found.       Sorting Searching Finding Maximum, Minimum Reversing Shuffling more...
    30. 30. Java 1.5 Enhancements  Generics  For-Each Loop  Autoboxing
    31. 31. 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
    32. 32. For-Each Loop  Use the same loop construct for arrays and collections: for(String s : collection) { System.out.println(s); }
    33. 33. Autoboxing  You can now use primitives in Collections list.add(3); map.put(4857, employee);
    34. 34. Best Practices  Always use the Interface.  Always use the Iterator (1.4) or the For- Each Loop (1.5).

    ×