Java Collections Framework A presentation by Eric Fabricant.

34
Java Collections Framework A presentation by Eric Fabricant

Transcript of Java Collections Framework A presentation by Eric Fabricant.

Page 1: Java Collections Framework A presentation by Eric Fabricant.

Java Collections Framework

A presentation by Eric Fabricant

Page 2: Java Collections Framework A presentation by Eric Fabricant.

What is it?

“The Collections Framework provides a well-designed set of interfaces and classes for storing and manipulating groups of data as a single unit, a collection.” -java.sun.com

Page 3: Java Collections Framework A presentation by Eric Fabricant.

So what?Well, The framework provides a convenient API to many of the abstract data types familiar to computer science: maps, sets, lists, trees, arrays, hashtables and other collections.

Page 4: Java Collections Framework A presentation by Eric Fabricant.

What does it look like?

Page 5: Java Collections Framework A presentation by Eric Fabricant.

The Basics

simple, but not

really.

The Basics

simple, but not

really. • Set

• List

• Map

• Set

• List

• Map

Page 6: Java Collections Framework A presentation by Eric Fabricant.

What makes a Set a Set?

No duplicate elements are allowed, such that e1.equals(e2) is true.

Page 7: Java Collections Framework A presentation by Eric Fabricant.

More on Sets• The Set Interface extends the Collection Interface.

• AbstractSet implements Set and extends AbstractCollection

•AbstractSet is a convenience class that contains basic concrete implementation.

Page 8: Java Collections Framework A presentation by Eric Fabricant.

Types of SetsTypes of Sets

• HashSet

• LinkedHashSet

• TreeSet

• HashSet

• LinkedHashSet

• TreeSet

Page 9: Java Collections Framework A presentation by Eric Fabricant.

HashSet• Implements the Set interface, backed by a hash table (actually a HashMap instance). •Makes no guarantees as to the iteration order of the set.• It does not guarantee that the order will remain constant over time. •Permits the null element.

Page 10: Java Collections Framework A presentation by Eric Fabricant.

LinkedHashSet

• Extends HashSet.

• Implements doubly-linked list.• Can retrieve elements based on insertion-order. • Less efficient than HashSet.

Page 11: Java Collections Framework A presentation by Eric Fabricant.

TreeSet• Extends AbstractSet, implements SortedSet

• Elements can be kept in acscending order according to compareTo() or compare()

• All elements must be comparable.

Page 12: Java Collections Framework A presentation by Eric Fabricant.

Any questions?

Page 13: Java Collections Framework A presentation by Eric Fabricant.

What makes a List a List?

Duplicate elements are allowed and position-oriented operations are permitted.

Page 14: Java Collections Framework A presentation by Eric Fabricant.

More on Lists• The List Interface extends the Collection Interface.

• AbstractList implements List and extends AbstractCollection

•AbstractList is a convenience class that contains basic concrete implementation.

Page 15: Java Collections Framework A presentation by Eric Fabricant.

Types of ListsTypes of Lists

• ArrayList• LinkedList• Vector• Stack

• ArrayList• LinkedList• Vector• Stack

Page 16: Java Collections Framework A presentation by Eric Fabricant.

ArrayList• Extends AbstractList

• Auto-resizeable.

• Based on a simple array.

• Permits the null element.

Page 17: Java Collections Framework A presentation by Eric Fabricant.

LinkedList• Extends AbstractSequentialList, which extends AbstractList

•Similar to ArrayList

• Different implementation based on nodes that contain data and links to other nodes.

Page 18: Java Collections Framework A presentation by Eric Fabricant.

Nodes In Action

Page 19: Java Collections Framework A presentation by Eric Fabricant.

Vector• Similar to ArrayList

• Contains synchronized methods.

Page 20: Java Collections Framework A presentation by Eric Fabricant.

Stack• Extends Vector

• “Last in, First Out” behavior

• Contains methods to allow typical Stack behaviors. (ie: push(), pop(), etc. )

Page 21: Java Collections Framework A presentation by Eric Fabricant.

Any questions?

Page 22: Java Collections Framework A presentation by Eric Fabricant.

What makes a Map a Map?

The collection is kept in key/value pairs. Any object can be a key or value. No duplicate keys allowed.

Page 23: Java Collections Framework A presentation by Eric Fabricant.

More on Maps• The Map Interface does not extend the Collection Interface.

• AbstractMap implements Map and does not extend AbstractCollection

•AbstractMap is a convenience class that contains basic concrete implementation.

Page 24: Java Collections Framework A presentation by Eric Fabricant.

Types of MapsTypes of Maps• TreeMap

• HashMap

• LinkedHashMap

• TreeMap

• HashMap

• LinkedHashMap

Page 25: Java Collections Framework A presentation by Eric Fabricant.

TreeMap• Implements SortedMap, extends AbtractMap

• Keys can be kept in acscending order according to compareTo() or compare()

Page 26: Java Collections Framework A presentation by Eric Fabricant.

HashMap• Implements SortedMap, extends AbstractMap

• Permits the null element.

• Makes no guarantees as to the iteration order of the set.

Page 27: Java Collections Framework A presentation by Eric Fabricant.

More HashMap

•It does not guarantee that the order will remain constant over time.

• Can retrieve elements based on insertion-order or access order.

Page 28: Java Collections Framework A presentation by Eric Fabricant.

LinkedHashMap

• Extends Hashmap

• Implements doubly-linked list.

Page 29: Java Collections Framework A presentation by Eric Fabricant.

Any questions?

Page 30: Java Collections Framework A presentation by Eric Fabricant.

Collections ClassCollections Class

• Contains static methods for operating on collections and maps.

• Quite usefull :-)

• Contains static methods for operating on collections and maps.

• Quite usefull :-)

Page 31: Java Collections Framework A presentation by Eric Fabricant.

Arrays ClassArrays Class

• Contains static methods sorting and searching arrays, comparing arrays, and filling array elements.

• Contains static methods sorting and searching arrays, comparing arrays, and filling array elements.

Page 32: Java Collections Framework A presentation by Eric Fabricant.

Additional InfoAdditional Info

• Synchronized Collection classes can be found in java.util.Collections

• Synchronized Collection classes can be found in java.util.Collections

Page 33: Java Collections Framework A presentation by Eric Fabricant.

( the end )

Page 34: Java Collections Framework A presentation by Eric Fabricant.

Credits

Material Adapted from:Java Programming, 5th ed. by Y.

Daniel Liang&

Java.sun.com