MELJUN CORTES JAVA_Collections
-
Upload
meljun-cortes-mbampa -
Category
Documents
-
view
225 -
download
0
Transcript of MELJUN CORTES JAVA_Collections
-
8/10/2019 MELJUN CORTES JAVA_Collections
1/34
The CollectionsFramework
Java Data Structures
MELJUN CORTES MBA MPA BSCS
-
8/10/2019 MELJUN CORTES JAVA_Collections
2/34
What You Should Learn
DefinitionTypes of Collections
Using CollectionsIteratingCopyingCollections in Java 1.5
-
8/10/2019 MELJUN CORTES JAVA_Collections
3/34
Definition
Collectionobject that groups multiple elements into
a single unitan array is a collection
-
8/10/2019 MELJUN CORTES JAVA_Collections
4/34
Definition
Collections Frameworkunified architecture for representing and
manipulating collections
-
8/10/2019 MELJUN CORTES JAVA_Collections
5/34
Definition
Collections FrameworkThree Elements:
Interfaceshow you handle the collectionImplementations
the classes that actually hold the data
Algorithmsuseful routines
-
8/10/2019 MELJUN CORTES JAVA_Collections
6/34
The Java CollectionsFramework
Interfaces
Collection
List Set Queue Map
-
8/10/2019 MELJUN CORTES JAVA_Collections
7/34
The Java CollectionsFramework
Collectionsuper type of List, Set and Queue
Liststores elements in the order they were added, similarto an array only growable
Setstores only unique elements, no duplicates
Queue
stores elements in preparation for processing, usuallyeither by FIFO or by priorityMap
stores key-value pairs, like for a lookup table
-
8/10/2019 MELJUN CORTES JAVA_Collections
8/34
Collection
Defines commonmethods
add
addAllremoveremoveAllclear
containscontainsAll
isEmptysizetoArrayiterator
-
8/10/2019 MELJUN CORTES JAVA_Collections
9/34
Lists
growable array Elements ordered in the sequence theywere added.Allows you to get and set using and index
myList.set(4, Barracuda);
String fish = myList.get(4);
-
8/10/2019 MELJUN CORTES JAVA_Collections
10/34
Lists
Implementations:ArrayList
LinkedListVector
-
8/10/2019 MELJUN CORTES JAVA_Collections
11/34
Lists
ArrayListbacked by an Object arrayfast in getting and setting elementsslow in inserting and removing
-
8/10/2019 MELJUN CORTES JAVA_Collections
12/34
Lists
LinkedListslow in getting and setting elementsfast in inserting and removing
-
8/10/2019 MELJUN CORTES JAVA_Collections
13/34
Lists
VectorJust like an ArrayList only all methods aresynchronized.Was the only list available prior to Java1.1.ArrayList is preferred to Vector because
programmer has control oversynchronization.
-
8/10/2019 MELJUN CORTES JAVA_Collections
14/34
Sets
No duplicatesMaintains its o w n order
Same interface as Collection
-
8/10/2019 MELJUN CORTES JAVA_Collections
15/34
Sets
Implementations:HashSet
TreeSet
-
8/10/2019 MELJUN CORTES JAVA_Collections
16/34
-
8/10/2019 MELJUN CORTES JAVA_Collections
17/34
Sets
TreeSetStores data in a binarytree to search forduplicates.
Sorted naturally Implements SortedSetinterface
-
8/10/2019 MELJUN CORTES JAVA_Collections
18/34
Queues
For holding elements prior to processing.Typically FIFO but may be ordered bypriorityImplementations:
LinkedList (FIFO)PriorityQueue
-
8/10/2019 MELJUN CORTES JAVA_Collections
19/34
Queues
Methodselementoffer
Peekpollremove
-
8/10/2019 MELJUN CORTES JAVA_Collections
20/34
Maps
An object that maps keys to valuesKeys are unique
Key (User ID String) Value (some object)
C234D7
A497X3
B252M5
R567B7
-
8/10/2019 MELJUN CORTES JAVA_Collections
21/34
Maps
Using a map:
myMap.put(T234Y9, someObject);
Getting from a map:
myMap.get(T234Y9);
-
8/10/2019 MELJUN CORTES JAVA_Collections
22/34
Maps
Implementations:HashMap
TreeMapHashTable
-
8/10/2019 MELJUN CORTES JAVA_Collections
23/34
Maps
HashMapKeys are ordered using a hash tableimplementation, similar to HashSet.
Keys are not sorted naturally.
-
8/10/2019 MELJUN CORTES JAVA_Collections
24/34
Maps
TreeMapUses a binary tree to order keys.Keys are sorted naturally.
-
8/10/2019 MELJUN CORTES JAVA_Collections
25/34
Maps
HashTableJust like an HashMap only all methods aresynchronized.
Was the only map available prior to Java 1.1.HashMap is preferred to HashTable becauseprogrammer has control over synchronization.
-
8/10/2019 MELJUN CORTES JAVA_Collections
26/34
Maps
HashMap vs. TreeMapHashMap is usually faster than TreeMap.Use TreeMap if you need keys to be sorted.
-
8/10/2019 MELJUN CORTES JAVA_Collections
27/34
Iterator
Prior to Java 1.5, Iterator was used toprovide generic iteration for Collectionimplementations.
-
8/10/2019 MELJUN CORTES JAVA_Collections
28/34
Iterator
//example: collection of stringsIterator iter = collection.iterator();
while(iter.hasNext) {String s = (String) iter.next();// do stuff with s
}
-
8/10/2019 MELJUN CORTES JAVA_Collections
29/34
Algorithms
java.util.CollectionsUtility class where useful routines can befound.
SortingSearchingFinding Maximum, MinimumReversingShufflingmore...
-
8/10/2019 MELJUN CORTES JAVA_Collections
30/34
Java 1.5 Enhancements
GenericsFor-Each Loop
Autoboxing
-
8/10/2019 MELJUN CORTES JAVA_Collections
31/34
Generics
You can enforce type on the collection.
List list
= new ArrayList();
String s = list.get(5);// no need to cast!
list.add(new Integer(5));// will not compile
-
8/10/2019 MELJUN CORTES JAVA_Collections
32/34
For-Each Loop
Use the same loop construct for arraysand collections:
for(String s : collection) {System.out.println(s);
}
-
8/10/2019 MELJUN CORTES JAVA_Collections
33/34
Autoboxing
You can now use primitives in Collections
list.add(3);
map.put(4857, employee);
-
8/10/2019 MELJUN CORTES JAVA_Collections
34/34
Best Practices
Always use the Interface.Always use the Iterator (1.4) or the For-Each Loop (1.5).