Collections Java e Google Collections

Post on 11-Nov-2014

2,793 views 2 download

Tags:

description

Introdução as Collections do JDK e Google Collections

transcript

Java and Google Collections

André Faria Gomes

What is a Collection?

It is a container ?

is simply an object that groups multiple

elements into a single unit

Why?

store, retrieve, manipulate, and

communicate aggregate data

The Java Collections Framework

Generics

The Collection Interface

The Iterator Interface

Lists (Positional access, Search, Iteration, Range-view)

The List Interface

The List Iterator

ArrayListResizable-array implementation of the List interface.

Linked List (Doubly-linked)better performance if elements are frequently inserted or deleted within the list.

VectorSynchronized resizable-array with legacy methods.

Stack (LIFO)

PushPopPeek

The ArrayDeque is a better option

Queue

Queue Offer(element) MethodInserts the specified element into this queue if it is possible to do so immediately without

violating capacity restrictions. When using a capacity-restricted queue, this method is generally preferable to add(E), which can fail to insert an element only by throwing an

exception.

Queue add() MethodInserts the specified element into this queue if it is possible to do so immediately without

violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available.

Queue Pool MethodRetrieves and removes the head of this queue, or returns null if this queue is empty.

Queue Peek MethodRetrieves, but does not remove, the head of this queue, or returns null if this queue is

empty.

Queue element() MethodRetrieves, but does not remove, the head of this queue. This method differs from peek

only in that it throws an exception if this queue is empty.

Linked List (FIFO)Insertion Order (QUEUE - LIST - DEQUE)

PriorityQueueA Queue Ordered By Natural or Non Natural Order

The Deque Interface (Java 6)element insertion and removal at both ends

Deque Interface Methods

Deque Descending Iterator

The ArrayDeque Implementation

Resizable-array implementation of the Deque interface. Array deques have no capacity restrictions; they grow as necessary to support usage. They are not thread-safe; in the absence of external synchronization, they do not support concurrent access by multiple threads. Null elements are prohibited. This class is likely to be faster than Stack when used as a stack, and faster than LinkedList when used as a queue.

BlockingQueue / BlockingDequeadditionally supports operations that wait for the queue to become non-empty when

retrieving an element, and wait for space to become available in the queue when storing an element

Sets

The Set Interface

HashSet (Fast, No Order)

TreeSet (Ordered by Value)

LinkedHashSet (Insertion Order)

Maps

The Map Interface

HashMap, TreeMap, LinkedHashMap

Hashtable (Synchronized HashMap)

Concurrent MapA java.util.Map providing additional atomic putIfAbsent(k,v), remove(k,v), and replace (k,

oldV,newV) methods.

WeakHashMapAn implementation of the Map interface that stores only weak references to its keys.

Storing only weak references allows key-value pairs to be garbage-collected when the key is no longer referenced outside of the WeakHashMap.

Ordering

The Comparable Interface (Natural Order)

The Comparable Interface (Non Natural Order)

The Sorted Set and Sorted Map Interfacesmaintains its elements in ascending order, sorted according to the elements' natural

ordering or according to a Comparator provided at SortedSet creation time. (TreeSet and TreeMap)

The SortedSet Interface

The SortedMap Interface

The NavigableSet Interface

The NavigableSet Interface

A SortedSet extended with navigation methods. Methods lower, floor, ceiling, and higher return elements respectively less than, less than or equal, greater than or equal, and greater than a given element, returning null if there is no such element. Ascending or descending order. This interface additionally defines methods pollFirst and pollLast that return and remove the lowest and highest element, if one exists, else returning null. Methods subSet, headSet, and tailSet. Implemented By TreeMap

The NavigableMap IntefaceImplemented by TreeMao

Enums

EnumSet

EnumMap

Wrapper Implementations

Unmodifiable

Synchronized

Checked Collections

Joshua Bloch

Google Collections

Extending the Java Collections Library

What?

The Google Collections Library 1.0 is a set of new collection types, implementations and related goodness for Java 5 and higher, brought to you by Google. It is a natural extension of the Java Collections Framework you already know and love.

Multimap

MultiMap

Multisets

Multiset

very useful for histograms and counting purposes

BiMap

BiMap

BiMap provides bi-directional map functionality. In a bidirectional map, both keys and values are unique, and looking up a key from a value is possible.

Bi-directional map functionality. Both keys and values are unique, and looking up a key from a value is possible.

MapMaker

MapMaker

Immutable Collections

Immutable Collections

Functional Programming

Predicates

Predicate

Class Predicates

Functions (Transformation)

Functions

Preconditions (Constraints)

Preconditions

Utilities

Joiner

Joiner

Object - Equals and HashCode

Reducing Verbosity

Get Only Element

Reducing Verbosity

Map<String, List<String>> mapOfLists = Maps.newHashMap();List<String> strings = Lists.newArrayList();

You Tubehttp://www.youtube.com/watch?v=ZeO_J2OcHYM

Java Possehttp://www.javaposse.com

Creative Commons Images (1/2)http://www.flickr.com/photos/tonyjcase/2381294958/\http://www.flickr.com/photos/rcsaxon/689732379/ http://www.flickr.com/photos/caveman_92223/3185534518/ http://www.flickr.com/photos/northbaywanderer/121971249/ http://www.flickr.com/photos/photohome_uk/1494590209/ http://www.flickr.com/photos/mworrell/266913194/ http://www.flickr.com/photos/romanlily/2609759239/ http://www.flickr.com/photos/seandreilinger/713631512/ http://www.flickr.com/photos/thatguyfromcchs08/2300190277/ http://www.flickr.com/photos/flyzipper/61475775/ http://www.flickr.com/photos/kasaa/2315571104/ http://www.flickr.com/photos/vermininc/2335148856/ http://www.flickr.com/photos/stewf/2026818238/ http://www.flickr.com/photos/kinghuang/3172003953/ http://www.flickr.com/photos/jgoforth/3111875161/ http://farm3.static.flickr.com/2045/2247502690_d72ec4c683.jpg?v=0 http://www.flickr.com/photos/expressmonorail/3024990539/ http://www.flickr.com/photos/tonivc/403265960/ http://www.flickr.com/photos/kt/146500920/ http://www.flickr.com/photos/marcelgermain/2070007716/ http://www.flickr.com/photos/darwinbell/465459020/ http://www.flickr.com/photos/vgm8383/2482555985/ http://www.flickr.com/photos/darhadubai/2950522110/ http://www.flickr.com/photos/jackhynes/330890500/ http://www.flickr.com/photos/breakfastpirate/167192101/ continue....

Creative Commons Images (2/2)http://www.flickr.com/photos/gadl/89650415/http://www.flickr.com/photos/29714836@N08/2956409782/http://www.flickr.com/photos/phonono/520421532/ http://www.flickr.com/photos/mika/279115724/http://www.flickr.com/photos/smaku/2602374677/ http://www.flickr.com/photos/colm/551068416/http://www.flickr.com/photos/gaetanlee/298680664/http://www.flickr.com/photos/charliegentle/94736480/http://www.flickr.com/photos/theamarand/2616794227/

References

Google Collections JavaDochttp://google-collections.googlecode.com/svn/trunk/javadoc/index.html

The Google Collections Libraryhttp://www.developer.com/open/article.php/3735441