🗊Презентация Java Collections Framework

Нажмите для полного просмотра!
Java Collections Framework, слайд №1Java Collections Framework, слайд №2Java Collections Framework, слайд №3Java Collections Framework, слайд №4Java Collections Framework, слайд №5Java Collections Framework, слайд №6Java Collections Framework, слайд №7Java Collections Framework, слайд №8Java Collections Framework, слайд №9Java Collections Framework, слайд №10Java Collections Framework, слайд №11Java Collections Framework, слайд №12Java Collections Framework, слайд №13Java Collections Framework, слайд №14Java Collections Framework, слайд №15Java Collections Framework, слайд №16Java Collections Framework, слайд №17Java Collections Framework, слайд №18Java Collections Framework, слайд №19Java Collections Framework, слайд №20Java Collections Framework, слайд №21Java Collections Framework, слайд №22Java Collections Framework, слайд №23Java Collections Framework, слайд №24Java Collections Framework, слайд №25Java Collections Framework, слайд №26Java Collections Framework, слайд №27Java Collections Framework, слайд №28Java Collections Framework, слайд №29Java Collections Framework, слайд №30Java Collections Framework, слайд №31Java Collections Framework, слайд №32Java Collections Framework, слайд №33Java Collections Framework, слайд №34Java Collections Framework, слайд №35Java Collections Framework, слайд №36Java Collections Framework, слайд №37Java Collections Framework, слайд №38Java Collections Framework, слайд №39Java Collections Framework, слайд №40Java Collections Framework, слайд №41Java Collections Framework, слайд №42Java Collections Framework, слайд №43Java Collections Framework, слайд №44Java Collections Framework, слайд №45Java Collections Framework, слайд №46Java Collections Framework, слайд №47Java Collections Framework, слайд №48Java Collections Framework, слайд №49Java Collections Framework, слайд №50Java Collections Framework, слайд №51Java Collections Framework, слайд №52Java Collections Framework, слайд №53Java Collections Framework, слайд №54Java Collections Framework, слайд №55Java Collections Framework, слайд №56Java Collections Framework, слайд №57Java Collections Framework, слайд №58Java Collections Framework, слайд №59Java Collections Framework, слайд №60Java Collections Framework, слайд №61Java Collections Framework, слайд №62Java Collections Framework, слайд №63

Содержание

Вы можете ознакомиться и скачать презентацию на тему Java Collections Framework. Доклад-сообщение содержит 63 слайдов. Презентации для любого класса можно скачать бесплатно. Если материал и наш сайт презентаций Mypresentation Вам понравились – поделитесь им с друзьями с помощью социальных кнопок и добавьте в закладки в своем браузере.

Слайды и текст этой презентации


Слайд 1







				           Nataliia Romanenko
Описание слайда:
Nataliia Romanenko

Слайд 2





Agenda
Data Structure 
What is JCF?
The Collection Interfaces
Collections Implementations
Ordering and Sorting
The Legacy Collections Type
The Collections Toolbox
Other implementations in the API
Описание слайда:
Agenda Data Structure What is JCF? The Collection Interfaces Collections Implementations Ordering and Sorting The Legacy Collections Type The Collections Toolbox Other implementations in the API

Слайд 3





Lecture Objectives
To understand the concepts of Java collections Framework
To be able to implement Java programs based on Collections
Описание слайда:
Lecture Objectives To understand the concepts of Java collections Framework To be able to implement Java programs based on Collections

Слайд 4





Store of Data Structure
Arrays - a linear data structure and it's mainly used to store similar data. An array is a particular method of storing elements of indexed data.
Описание слайда:
Store of Data Structure Arrays - a linear data structure and it's mainly used to store similar data. An array is a particular method of storing elements of indexed data.

Слайд 5





Store of Data Structure (cont.)
Linked list is a data structure consisting of a group of nodes. Each node is composed of a datum and a reference to the next node in the sequence. This structure allows for efficient insertion or removal of elements from any position in the sequence.
Описание слайда:
Store of Data Structure (cont.) Linked list is a data structure consisting of a group of nodes. Each node is composed of a datum and a reference to the next node in the sequence. This structure allows for efficient insertion or removal of elements from any position in the sequence.

Слайд 6





Store of Data Structure (cont.)
If each node has a reference to the next and previous nodes it’s called Doubly Linked List.
Описание слайда:
Store of Data Structure (cont.) If each node has a reference to the next and previous nodes it’s called Doubly Linked List.

Слайд 7





Store of Data Structure (cont.)
Binary tree is a tree data structure in which each node has at most two child nodes, usually distinguished as "left" and "right".
Описание слайда:
Store of Data Structure (cont.) Binary tree is a tree data structure in which each node has at most two child nodes, usually distinguished as "left" and "right".

Слайд 8





Store of Data Structure (cont.)
A hash table, or a hash map, is a data structure that associates keys with values.
Описание слайда:
Store of Data Structure (cont.) A hash table, or a hash map, is a data structure that associates keys with values.

Слайд 9





The Limitation of Arrays
An array is a very useful type in Java but it has its restrictions:
once an array is created it must be sized, and this size is fixed;
it contains no useful pre-defined methods.
Java comes with a group of generic collection classes that grow as more elements are added to them, and these classes provide lots of useful methods. 
This group of collection classes are referred to as the Java Collections Framework.
Описание слайда:
The Limitation of Arrays An array is a very useful type in Java but it has its restrictions: once an array is created it must be sized, and this size is fixed; it contains no useful pre-defined methods. Java comes with a group of generic collection classes that grow as more elements are added to them, and these classes provide lots of useful methods. This group of collection classes are referred to as the Java Collections Framework.

Слайд 10





What is a Collections Framework?
A unified architecture for representing and manipulating collections.  
Includes:  
	– Interfaces: A hierarchy of abstract data types.  
	– Implementations 
	– Algorithms: The methods that perform useful computations, such as searching and sorting, on objects that implement collection interfaces.
Описание слайда:
What is a Collections Framework? A unified architecture for representing and manipulating collections. Includes: – Interfaces: A hierarchy of abstract data types. – Implementations – Algorithms: The methods that perform useful computations, such as searching and sorting, on objects that implement collection interfaces.

Слайд 11





Hierarchy of interfaces
Описание слайда:
Hierarchy of interfaces

Слайд 12





Exception Conventions
UnsupportedOperationException
ClassCastException
IllegalArgumentException
IllegalStateException
NoSuchElementException
NullPointerException
IndexOutOfBoundsException
Описание слайда:
Exception Conventions UnsupportedOperationException ClassCastException IllegalArgumentException IllegalStateException NoSuchElementException NullPointerException IndexOutOfBoundsException

Слайд 13





Iterators
Iterator is an object that enables a programmer to traverse a container, particularly lists. 
public interface Iterator<E> {
    boolean hasNext();
    E next();
    void remove(); //optional
}
Описание слайда:
Iterators Iterator is an object that enables a programmer to traverse a container, particularly lists. public interface Iterator<E> { boolean hasNext(); E next(); void remove(); //optional }

Слайд 14





Collection<E>
 public interface Collection<E> extends Iterable<E> {
    // Basic operations
    int size();
    boolean isEmpty();
    boolean contains(Object element);
    boolean add(E element);         //optional
    boolean remove(Object element); //optional
    Iterator<E> iterator();
    // Bulk operations
    boolean containsAll(Collection<?> c);
    boolean addAll(Collection<? extends E> c); //optional
    boolean removeAll(Collection<?> c);        //optional
    boolean retainAll(Collection<?> c);        //optional
    void clear();                              //optional
    // Array operations
    Object[] toArray();
    <T> T[] toArray(T[] a);
}
Описание слайда:
Collection<E>  public interface Collection<E> extends Iterable<E> { // Basic operations int size(); boolean isEmpty(); boolean contains(Object element); boolean add(E element); //optional boolean remove(Object element); //optional Iterator<E> iterator(); // Bulk operations boolean containsAll(Collection<?> c); boolean addAll(Collection<? extends E> c); //optional boolean removeAll(Collection<?> c); //optional boolean retainAll(Collection<?> c); //optional void clear(); //optional // Array operations Object[] toArray(); <T> T[] toArray(T[] a); }

Слайд 15





Set
Set — a collection that cannot contain duplicate elements 
This interface models the mathematical set abstraction and is used to represent sets, such as the cards comprising a poker hand, the courses making up a student's schedule, or the processes running on a machine
Описание слайда:
Set Set — a collection that cannot contain duplicate elements This interface models the mathematical set abstraction and is used to represent sets, such as the cards comprising a poker hand, the courses making up a student's schedule, or the processes running on a machine

Слайд 16





Set<E> (methods)
public interface Set<E> extends Collection<E> {
    // Basic operations
    int size();
    boolean isEmpty();
    boolean contains(Object element);
    boolean add(E element);         //optional
    boolean remove(Object element); //optional
    Iterator<E> iterator();
    // Bulk operations
    boolean containsAll(Collection<?> c);
    boolean addAll(Collection<? extends E> c); //optional
    boolean removeAll(Collection<?> c);        //optional
    boolean retainAll(Collection<?> c);        //optional
    void clear();                              //optional
    // Array Operations
    Object[] toArray();
    <T> T[] toArray(T[] a);
}
Описание слайда:
Set<E> (methods) public interface Set<E> extends Collection<E> { // Basic operations int size(); boolean isEmpty(); boolean contains(Object element); boolean add(E element); //optional boolean remove(Object element); //optional Iterator<E> iterator(); // Bulk operations boolean containsAll(Collection<?> c); boolean addAll(Collection<? extends E> c); //optional boolean removeAll(Collection<?> c); //optional boolean retainAll(Collection<?> c); //optional void clear(); //optional // Array Operations Object[] toArray(); <T> T[] toArray(T[] a); }

Слайд 17





List
List — an ordered collection (sometimes called a sequence) 
Lists can contain duplicate elements
Описание слайда:
List List — an ordered collection (sometimes called a sequence) Lists can contain duplicate elements

Слайд 18





List<E> (methods)
public interface List<E> extends Collection<E> {
    // Positional access
    E get(int index);
    E set(int index, E element);    //optional
    boolean add(E element);         //optional
    void add(int index, E element); //optional
    E remove(int index);            //optional
    boolean addAll(int index,
        Collection<? extends E> c); //optional
    // Search
    int indexOf(Object o);
    int lastIndexOf(Object o);
    // Iteration
    ListIterator<E> listIterator();
    ListIterator<E> listIterator(int index);
    // Range-view
    List<E> subList(int from, int to);
}
Описание слайда:
List<E> (methods) public interface List<E> extends Collection<E> { // Positional access E get(int index); E set(int index, E element); //optional boolean add(E element); //optional void add(int index, E element); //optional E remove(int index); //optional boolean addAll(int index, Collection<? extends E> c); //optional // Search int indexOf(Object o); int lastIndexOf(Object o); // Iteration ListIterator<E> listIterator(); ListIterator<E> listIterator(int index); // Range-view List<E> subList(int from, int to); }

Слайд 19





ListIterators
A ListIterator extends Iterator to treat the collection as a list, allowing  
	– access to the integer position (index) of elements 
	– forward and backward traversal 
	– modification and insertion of elements
Описание слайда:
ListIterators A ListIterator extends Iterator to treat the collection as a list, allowing – access to the integer position (index) of elements – forward and backward traversal – modification and insertion of elements

Слайд 20





ListIterator<E> (methods)
public interface ListIterator<E> extends Iterator<E> {
    	boolean hasNext();
    	E next();
	boolean hasPrevious();
    	E previous();
    	int nextIndex();
    	int previousIndex();
    	void remove(); //optional
    	void set(E e); //optional
    	void add(E e); //optional
}
Описание слайда:
ListIterator<E> (methods) public interface ListIterator<E> extends Iterator<E> { boolean hasNext(); E next(); boolean hasPrevious(); E previous(); int nextIndex(); int previousIndex(); void remove(); //optional void set(E e); //optional void add(E e); //optional }

Слайд 21





ListIterator
ListIterator<String> it = list.listIterator(list.size());
while (it.hasPrevious()) {
   String obj = it.previous();
      // … use obj  ….
 }
Описание слайда:
ListIterator ListIterator<String> it = list.listIterator(list.size()); while (it.hasPrevious()) { String obj = it.previous(); // … use obj …. }

Слайд 22





Queue
Queue — a collection used to hold multiple elements prior to processing. Besides basic Collection operations, a Queue provides additional insertion, extraction, and inspection operations
Queues typically, but do not necessarily, order elements in a FIFO (first-in-first-out) manner
Описание слайда:
Queue Queue — a collection used to hold multiple elements prior to processing. Besides basic Collection operations, a Queue provides additional insertion, extraction, and inspection operations Queues typically, but do not necessarily, order elements in a FIFO (first-in-first-out) manner

Слайд 23





Queue<E> (methods)
public interface Queue<E> extends Collection<E> {
    E element();		//throws
    E peek();			//null
	boolean add(E e); 	//throws
	boolean offer(E e);	//null
    E remove();		//throws
    E poll();			//null
}
Описание слайда:
Queue<E> (methods) public interface Queue<E> extends Collection<E> { E element(); //throws E peek(); //null boolean add(E e); //throws boolean offer(E e); //null E remove(); //throws E poll(); //null }

Слайд 24





Queue
In addition to the inherited core services offered by Collection, queue offers following methods in two flavors:
Описание слайда:
Queue In addition to the inherited core services offered by Collection, queue offers following methods in two flavors:

Слайд 25





Deque
A linear collection that supports element insertion and removal at both ends 
When a Deque is used as a queue, FIFO (First-In-First-Out) behavior results
Deques can also be used as LIFO (Last-In-First-Out) stacks
Описание слайда:
Deque A linear collection that supports element insertion and removal at both ends When a Deque is used as a queue, FIFO (First-In-First-Out) behavior results Deques can also be used as LIFO (Last-In-First-Out) stacks

Слайд 26





Deque<E> (methods)
public interface Deque<E> extends Queue<E> {
	element()
	add(E e) (addLast(E e)) 
	offer(E e) (offerLast(E e)), offerFirst(E e) 
	peek()* (peekFirst()), peekLast()
	getFirst(), getLast()
	remove() (removeFirst()), removeFirstOccurrence(Object o),
  		  removeLast(), removeLastOccurrence(Object o) 
	poll() (pollFirst()), pollLast() 
	contains(Object o)
	iterator() 
	descendingIterator()
	size() 
	pop() (removeFirst())
	push(E e) (addFirst(E e))
}
from Queue, from Stack
Описание слайда:
Deque<E> (methods) public interface Deque<E> extends Queue<E> { element() add(E e) (addLast(E e)) offer(E e) (offerLast(E e)), offerFirst(E e) peek()* (peekFirst()), peekLast() getFirst(), getLast() remove() (removeFirst()), removeFirstOccurrence(Object o), removeLast(), removeLastOccurrence(Object o) poll() (pollFirst()), pollLast() contains(Object o) iterator() descendingIterator() size() pop() (removeFirst()) push(E e) (addFirst(E e)) } from Queue, from Stack

Слайд 27





SortedSet
SortedSet — a Set that maintains its elements in ascending order. Several additional operations are provided to take advantage of the ordering. Sorted sets are used for naturally ordered sets, such as word lists and membership rolls
Описание слайда:
SortedSet SortedSet — a Set that maintains its elements in ascending order. Several additional operations are provided to take advantage of the ordering. Sorted sets are used for naturally ordered sets, such as word lists and membership rolls

Слайд 28





SortedSet<E> (methods)
public interface SortedSet<E> extends Set<E> {
    // Range-view
    SortedSet<E> subSet(E fromElement, E toElement);
    SortedSet<E> headSet(E toElement);
    SortedSet<E> tailSet(E fromElement);
    // Endpoints
    E first();
    E last();
    // Comparator access
    Comparator<? super E> comparator();
}
Описание слайда:
SortedSet<E> (methods) public interface SortedSet<E> extends Set<E> { // Range-view SortedSet<E> subSet(E fromElement, E toElement); SortedSet<E> headSet(E toElement); SortedSet<E> tailSet(E fromElement); // Endpoints E first(); E last(); // Comparator access Comparator<? super E> comparator(); }

Слайд 29





Map
An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value 
The Map interface provides three collection views, which allow a map's contents to be viewed as a set of keys, collection of values, or set of key-value mappings
Описание слайда:
Map An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value The Map interface provides three collection views, which allow a map's contents to be viewed as a set of keys, collection of values, or set of key-value mappings

Слайд 30





Map<K,V> (methods)
public interface Map<K,V> {
    // Basic operations
    V put(K key, V value);
    V get(Object key);
    V remove(Object key);
    boolean containsKey(Object key);
    boolean containsValue(Object value);
    int size();
    boolean isEmpty();
    // Bulk operations
    void putAll(Map<? extends K, ? extends V> m);
Описание слайда:
Map<K,V> (methods) public interface Map<K,V> { // Basic operations V put(K key, V value); V get(Object key); V remove(Object key); boolean containsKey(Object key); boolean containsValue(Object value); int size(); boolean isEmpty(); // Bulk operations void putAll(Map<? extends K, ? extends V> m);

Слайд 31





Map<K,V> (methods)
    void clear();
    // Collection Views
    public Set<K> keySet();
    public Collection<V> values();
    public Set<Map.Entry<K,V>> entrySet();
    // Interface for entrySet elements
    public interface Entry {
        K getKey();
        V getValue();
        V setValue(V value);
    }
}
Описание слайда:
Map<K,V> (methods) void clear(); // Collection Views public Set<K> keySet(); public Collection<V> values(); public Set<Map.Entry<K,V>> entrySet(); // Interface for entrySet elements public interface Entry { K getKey(); V getValue(); V setValue(V value); } }

Слайд 32





Map.Entry
public interface Entry {
        K getKey();
        V getValue();
        V setValue(V value);
    }
Map<String, String> map = new HashMap<String, String>(); 
map.put("1", "a"); 
map.put("2", "b"); 
map.put("3", "c"); 
for( Entry<String, String> entry : map.entrySet() ) {
	if( "2".equals( entry.getKey() ) ) 
		entry.setValue( "x" ); 
}
Описание слайда:
Map.Entry public interface Entry { K getKey(); V getValue(); V setValue(V value); } Map<String, String> map = new HashMap<String, String>(); map.put("1", "a"); map.put("2", "b"); map.put("3", "c"); for( Entry<String, String> entry : map.entrySet() ) { if( "2".equals( entry.getKey() ) ) entry.setValue( "x" ); }

Слайд 33





SortedMap
SortedMap — a Map that maintains its mappings in ascending key order. This is the Map analog of SortedSet. Sorted maps are used for naturally ordered collections of key/value pairs, such as dictionaries and telephone directories
Описание слайда:
SortedMap SortedMap — a Map that maintains its mappings in ascending key order. This is the Map analog of SortedSet. Sorted maps are used for naturally ordered collections of key/value pairs, such as dictionaries and telephone directories

Слайд 34





SortedMap<K,V> (methods)
public interface SortedMap<K, V> extends Map<K, V>{
    SortedMap<K, V> subMap(K fromKey, K toKey);
    SortedMap<K, V> headMap(K toKey);
    SortedMap<K, V> tailMap(K fromKey);
    K firstKey();
    K lastKey();
    Comparator<? super K> comparator();
}
Описание слайда:
SortedMap<K,V> (methods) public interface SortedMap<K, V> extends Map<K, V>{ SortedMap<K, V> subMap(K fromKey, K toKey); SortedMap<K, V> headMap(K toKey); SortedMap<K, V> tailMap(K fromKey); K firstKey(); K lastKey(); Comparator<? super K> comparator(); }

Слайд 35





Implementations
JDK  provides implementations of each interface. 
All implementations permit null elements, keys and values
All are Serializable, and all support a public clone method
Each one is unsynchronized 
If you need a synchronized collection, the synchronization wrappers allow any collection to be transformed into a synchronized collection
Описание слайда:
Implementations JDK provides implementations of each interface. All implementations permit null elements, keys and values All are Serializable, and all support a public clone method Each one is unsynchronized If you need a synchronized collection, the synchronization wrappers allow any collection to be transformed into a synchronized collection

Слайд 36





HashSet, TreeSet, LinkedHashSet
The two general purpose Set implementations are HashSet and TreeSet (and LinkedHashSet which is between them)
HashSet is much faster but offers no ordering guarantees. 
If  in-order iteration is important use TreeSet. 
Iteration in HashSet is linear in the sum of the number of entries and the capacity. It's important to choose an appropriate initial capacity if iteration performance is important.  The default initial capacity is 101. The initial capacity may be specified using the int constructor. To allocate a HashSet whose initial capacity is 17: 
	Set s= new HashSet(17);
Описание слайда:
HashSet, TreeSet, LinkedHashSet The two general purpose Set implementations are HashSet and TreeSet (and LinkedHashSet which is between them) HashSet is much faster but offers no ordering guarantees. If in-order iteration is important use TreeSet. Iteration in HashSet is linear in the sum of the number of entries and the capacity. It's important to choose an appropriate initial capacity if iteration performance is important. The default initial capacity is 101. The initial capacity may be specified using the int constructor. To allocate a HashSet whose initial capacity is 17: Set s= new HashSet(17);

Слайд 37





Set Implementation Comparisons
Описание слайда:
Set Implementation Comparisons

Слайд 38





ArrayList and LinkedList
The two general purpose List implementations are ArrayList and LinkedList . ArrayList offers constant time positional access, and it's just plain fast, because it does not have to allocate a node object for each element in the List, and it can take advantage of the native method System.arraycopy when it has to move multiple elements at once
If you frequently add elements to the beginning of the List, or iterate over the List deleting elements from its interior, you might want to consider LinkedList. These operations are constant time in a LinkedList but linear time in an ArrayList.  Positional access is linear time in a LinkedList and constant time in an ArrayList
Описание слайда:
ArrayList and LinkedList The two general purpose List implementations are ArrayList and LinkedList . ArrayList offers constant time positional access, and it's just plain fast, because it does not have to allocate a node object for each element in the List, and it can take advantage of the native method System.arraycopy when it has to move multiple elements at once If you frequently add elements to the beginning of the List, or iterate over the List deleting elements from its interior, you might want to consider LinkedList. These operations are constant time in a LinkedList but linear time in an ArrayList. Positional access is linear time in a LinkedList and constant time in an ArrayList

Слайд 39





HashMap, TreeMap, LinkedHashMap
The two general purpose Map implementations are HashMap and TreeMap. 
And LinkedHashMap (similar to LinkedHashSet) 
The situation for Map is exactly analogous to Set 
If you need SortedMap operations you should use TreeMap; otherwise, use HashMap
Описание слайда:
HashMap, TreeMap, LinkedHashMap The two general purpose Map implementations are HashMap and TreeMap. And LinkedHashMap (similar to LinkedHashSet) The situation for Map is exactly analogous to Set If you need SortedMap operations you should use TreeMap; otherwise, use HashMap

Слайд 40





The Legacy Collection Types
Enumeration
Analogous to Iterator.
Vector
Analogous to ArrayList, maintains an ordered list of elements that are stored in an underlying array.
Stack
Analogous of Vector that adds methods to push and pop elements.
Dictionary
Analogous to the Map interface, although Dictionary is an abstract class, not an interface.
Hashtable
Analogous HashMap.
Properties
A subclass of Hashtable. Maintains a map of key/value pairs where the keys and values are strings. If a key is not found in a properties object a “default” properties object can be searched.
Описание слайда:
The Legacy Collection Types Enumeration Analogous to Iterator. Vector Analogous to ArrayList, maintains an ordered list of elements that are stored in an underlying array. Stack Analogous of Vector that adds methods to push and pop elements. Dictionary Analogous to the Map interface, although Dictionary is an abstract class, not an interface. Hashtable Analogous HashMap. Properties A subclass of Hashtable. Maintains a map of key/value pairs where the keys and values are strings. If a key is not found in a properties object a “default” properties object can be searched.

Слайд 41


Java Collections Framework, слайд №41
Описание слайда:

Слайд 42





Implementations
Each of the implementations offers the strengths and weaknesses of the underlying data structure.
What does that mean for:
Hashtable
Resizable array
Tree
LinkedList
Hashtable plus LinkedList
Think about these tradeoffs when selecting the implementation!
Описание слайда:
Implementations Each of the implementations offers the strengths and weaknesses of the underlying data structure. What does that mean for: Hashtable Resizable array Tree LinkedList Hashtable plus LinkedList Think about these tradeoffs when selecting the implementation!

Слайд 43





Ordering and Sorting
There are two ways to define orders on objects. 	
Each class can define a natural order among its instances by implementing the Comparable interface. 
Arbitrary orders among different objects can be defined by comparators, classes that implement the Comparator interface.
Описание слайда:
Ordering and Sorting There are two ways to define orders on objects. Each class can define a natural order among its instances by implementing the Comparable interface. Arbitrary orders among different objects can be defined by comparators, classes that implement the Comparator interface.

Слайд 44





The Comparable Interface
The Comparable interface consists of a single method: 
	public interface Comparable<T> {
    	public int compareTo(T o);
    }
	The compareTo method compares the receiving object with the specified object, and returns a negative integer, zero, or a positive integer as the receiving object is less than, equal to, or greater than the specified Object.
Описание слайда:
The Comparable Interface The Comparable interface consists of a single method: public interface Comparable<T> { public int compareTo(T o); } The compareTo method compares the receiving object with the specified object, and returns a negative integer, zero, or a positive integer as the receiving object is less than, equal to, or greater than the specified Object.

Слайд 45





Comparator
Comparator is another interface (in addition to Comparable) provided by the Java API which can be used to order objects.
You can use this interface to define an order that is different from the Comparable (natural) order.
Описание слайда:
Comparator Comparator is another interface (in addition to Comparable) provided by the Java API which can be used to order objects. You can use this interface to define an order that is different from the Comparable (natural) order.

Слайд 46





Comparator
A Comparator is an object that encapsulates an ordering. Like the Comparable interface, the Comparator interface consists of a single method: 
	public interface Comparator<T> {
    int compare(T o1, T o2);
 }
	The compare method compares its two arguments, returning a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
Описание слайда:
Comparator A Comparator is an object that encapsulates an ordering. Like the Comparable interface, the Comparator interface consists of a single method: public interface Comparator<T> { int compare(T o1, T o2); } The compare method compares its two arguments, returning a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

Слайд 47





Live Code . Comparable
class HDTV implements Comparable<HDTV> {
	private int size;
	private String brand;
	 public HDTV(int size, String brand) {
		this.size = size;
		this.brand = brand;
	}
 	public int getSize() { return size;}
 	public void setSize(int size) {this.size = size; }
 	public String getBrand() { return brand;}
 	public void setBrand(String brand) {this.brand = brand; }
	
	@Override
	public int compareTo(HDTV tv) {
		if (this.getSize() > tv.getSize()) return 1;
		else if (this.getSize() < tv.getSize()) return -1;
		else return 0;
	}
}
Описание слайда:
Live Code . Comparable class HDTV implements Comparable<HDTV> { private int size; private String brand; public HDTV(int size, String brand) { this.size = size; this.brand = brand; } public int getSize() { return size;} public void setSize(int size) {this.size = size; } public String getBrand() { return brand;} public void setBrand(String brand) {this.brand = brand; } @Override public int compareTo(HDTV tv) { if (this.getSize() > tv.getSize()) return 1; else if (this.getSize() < tv.getSize()) return -1; else return 0; } }

Слайд 48





Live Code . Comparable
public class Main {
	public static void main(String[] args) {
		HDTV tv1 = new HDTV(55, "Samsung"); 
		HDTV tv2 = new HDTV(60, "Sony"); 
		HDTV tv3 = new HDTV(42, "Panasonic");   
		ArrayList<HDTV> al = new ArrayList<HDTV>(); 
		al.add(tv1); 
		al.add(tv2); 
		al.add(tv3);  
		Collections.sort(al); 
		for (HDTV a : al) { 
			System.out.println(a.getBrand()); 
		}	
	}
}
Описание слайда:
Live Code . Comparable public class Main { public static void main(String[] args) { HDTV tv1 = new HDTV(55, "Samsung"); HDTV tv2 = new HDTV(60, "Sony"); HDTV tv3 = new HDTV(42, "Panasonic");   ArrayList<HDTV> al = new ArrayList<HDTV>(); al.add(tv1); al.add(tv2); al.add(tv3);   Collections.sort(al); for (HDTV a : al) { System.out.println(a.getBrand()); } } }

Слайд 49





Live Code . Comparator
class HDTV {
	private int size;
	private String brand;
	 public HDTV(int size, String brand) {
		this.size = size;
		this.brand = brand;
	}
 	public int getSize() { return size;}
 	public void setSize(int size) {this.size = size; }
 	public String getBrand() { return brand;}
 	public void setBrand(String brand) {this.brand = brand; }
}
Описание слайда:
Live Code . Comparator class HDTV { private int size; private String brand; public HDTV(int size, String brand) { this.size = size; this.brand = brand; } public int getSize() { return size;} public void setSize(int size) {this.size = size; } public String getBrand() { return brand;} public void setBrand(String brand) {this.brand = brand; } }

Слайд 50





Live Code . Comparator
class SizeComparator implements Comparator<HDTV> {
	
@Override
	public int compare(HDTV tv1, HDTV tv2) {
		int tv1Size = tv1.getSize();
		int tv2Size = tv2.getSize();
 		if (tv1Size > tv2Size) {
			return 1;
		} else if (tv1Size < tv2Size) {
			return -1;
		} else {
			return 0;
		}
	}
}
class BrandComparatorDescOrder implements Comparator<HDTV> {
	
	@Override
	public int compare(HDTV tv1, HDTV tv2) {
	return tv2.getBrand().compareTo(tv1.getBrand())	
	}
}
Описание слайда:
Live Code . Comparator class SizeComparator implements Comparator<HDTV> { @Override public int compare(HDTV tv1, HDTV tv2) { int tv1Size = tv1.getSize(); int tv2Size = tv2.getSize(); if (tv1Size > tv2Size) { return 1; } else if (tv1Size < tv2Size) { return -1; } else { return 0; } } } class BrandComparatorDescOrder implements Comparator<HDTV> { @Override public int compare(HDTV tv1, HDTV tv2) { return tv2.getBrand().compareTo(tv1.getBrand()) } }

Слайд 51





Live Code . Comparator
public class Main {
	public static void main(String[] args) {
		HDTV tv1 = new HDTV(55, "Samsung");
		HDTV tv2 = new HDTV(60, "Sony");
		HDTV tv3 = new HDTV(42, "Panasonic");
 
		ArrayList<HDTV> al = new ArrayList<HDTV>();
		al.add(tv1);
		al.add(tv2);
		al.add(tv3);
 
		Collections.sort(al, new SizeComparator());
		for (HDTV a : al) {
			System.out.println(a.getBrand());
		}
		Collections.sort(al, new BrandComparatorDescOrder());
		for (HDTV a : al) {
			System.out.println(a.getBrand());
		}
	}
}
Описание слайда:
Live Code . Comparator public class Main { public static void main(String[] args) { HDTV tv1 = new HDTV(55, "Samsung"); HDTV tv2 = new HDTV(60, "Sony"); HDTV tv3 = new HDTV(42, "Panasonic"); ArrayList<HDTV> al = new ArrayList<HDTV>(); al.add(tv1); al.add(tv2); al.add(tv3); Collections.sort(al, new SizeComparator()); for (HDTV a : al) { System.out.println(a.getBrand()); } Collections.sort(al, new BrandComparatorDescOrder()); for (HDTV a : al) { System.out.println(a.getBrand()); } } }

Слайд 52





Other implementations in the API
Wrapper implementations delegate all their real work to a specified collection but add (or remove) extra functionality on top of what the collection offers. 
Synchronization Wrappers 
Unmodifiable Wrappers 
Convenience implementations are mini-implementations that can be more convenient and more efficient than general-purpose implementations when you don't need their full power 
List View of an Array
Immutable Multiple-Copy List
Immutable Singleton Set
Empty Set, List, and Map Constants
Описание слайда:
Other implementations in the API Wrapper implementations delegate all their real work to a specified collection but add (or remove) extra functionality on top of what the collection offers. Synchronization Wrappers Unmodifiable Wrappers Convenience implementations are mini-implementations that can be more convenient and more efficient than general-purpose implementations when you don't need their full power List View of an Array Immutable Multiple-Copy List Immutable Singleton Set Empty Set, List, and Map Constants

Слайд 53





Synchronization wrappers
The synchronization wrappers add automatic synchronization (thread-safety) to an arbitrary collection. There is one static factory method for each of the six core collection interfaces:
public static Collection synchronizedCollection(Collection c);
public static Set synchronizedSet(Set s);
public static List synchronizedList(List list); 
public static Map synchronizedMap(Map m); 
public static SortedSet synchronizedSortedSet(SortedSet s);
public static SortedMap synchronizedSortedMap(SortedMap m); 
Each of these methods returns a synchronized (thread-safe) Collection backed by the specified collection.
Описание слайда:
Synchronization wrappers The synchronization wrappers add automatic synchronization (thread-safety) to an arbitrary collection. There is one static factory method for each of the six core collection interfaces: public static Collection synchronizedCollection(Collection c); public static Set synchronizedSet(Set s); public static List synchronizedList(List list); public static Map synchronizedMap(Map m); public static SortedSet synchronizedSortedSet(SortedSet s); public static SortedMap synchronizedSortedMap(SortedMap m); Each of these methods returns a synchronized (thread-safe) Collection backed by the specified collection.

Слайд 54





Unmodifiable wrappers
Unmodifiable wrappers take away the ability to modify the collection, by intercepting all of the operations that would modify the collection, and throwing an UnsupportedOperationException. The unmodifiable wrappers have two main uses: 
To make a collection immutable once it has been built. 
To allow "second-class citizens" read-only access to your data structures. You keep a reference to the backing collection, but hand out a reference to the wrapper. In this way, the second-class citizens can look but not touch, while you maintain full access.
Описание слайда:
Unmodifiable wrappers Unmodifiable wrappers take away the ability to modify the collection, by intercepting all of the operations that would modify the collection, and throwing an UnsupportedOperationException. The unmodifiable wrappers have two main uses: To make a collection immutable once it has been built. To allow "second-class citizens" read-only access to your data structures. You keep a reference to the backing collection, but hand out a reference to the wrapper. In this way, the second-class citizens can look but not touch, while you maintain full access.

Слайд 55





Unmodifiable wrappers(cont.)
There is one static factory method for each of the six core collection interfaces: 
public static Collection unmodifiableCollection(Collection c); 
public static Set unmodifiableSet(Set s); 
public static List unmodifiableList(List list); 
public static Map unmodifiableMap(Map m);
public static SortedSet unmodifiableSortedSet(SortedSet s);
public static SortedMap unmodifiableSortedMap(SortedMap m);
Описание слайда:
Unmodifiable wrappers(cont.) There is one static factory method for each of the six core collection interfaces: public static Collection unmodifiableCollection(Collection c); public static Set unmodifiableSet(Set s); public static List unmodifiableList(List list); public static Map unmodifiableMap(Map m); public static SortedSet unmodifiableSortedSet(SortedSet s); public static SortedMap unmodifiableSortedMap(SortedMap m);

Слайд 56





Singleton
static <T> Set<T>Collections.singleton(T e) returns an immutable set containing only the element e
This is handy when you have a single element but you would like to use a Set operation
c.removeAll(Collections.singleton(e)); 
	will remove all occurrences of e from the Collection c
Описание слайда:
Singleton static <T> Set<T>Collections.singleton(T e) returns an immutable set containing only the element e This is handy when you have a single element but you would like to use a Set operation c.removeAll(Collections.singleton(e)); will remove all occurrences of e from the Collection c

Слайд 57





The Collections Toolbox
The collections framework also provides polymorphic versions of algorithms you can run on collections.
Sorting
Shuffling
Routine Data Manipulation
Reverse
Fill copy
etc.
Searching
Binary Search
Composition
Frequency
Disjoint
Finding extreme values
Min
Max
Описание слайда:
The Collections Toolbox The collections framework also provides polymorphic versions of algorithms you can run on collections. Sorting Shuffling Routine Data Manipulation Reverse Fill copy etc. Searching Binary Search Composition Frequency Disjoint Finding extreme values Min Max

Слайд 58





Concurrent Collections
ConcurrentReaderHashMap An analog of java.util.Hashtable that allows retrievals during updates. 
ConcurrentHashMap An analog of java.util.Hashtable that allows both concurrent retrievals and concurrent updates. 
CopyOnWriteArrayList A copy-on-write analog of java.util.ArrayList 
CopyOnWriteArraySet A java.util.Set based on CopyOnWriteArrayList. 
SyncCollection A wrapper class placing either Syncs or ReadWriteLocks around java.util.Collection 
SyncSet A wrapper around java.util.Set 
SyncSortedSet A wrapper around java.util.SortedSet 
SyncList A wrapper around java.util.List 
SyncMap A wrapper around java.util.Map 
SyncSortedMap A wrapper around java.util.SortedMap
Описание слайда:
Concurrent Collections ConcurrentReaderHashMap An analog of java.util.Hashtable that allows retrievals during updates. ConcurrentHashMap An analog of java.util.Hashtable that allows both concurrent retrievals and concurrent updates. CopyOnWriteArrayList A copy-on-write analog of java.util.ArrayList CopyOnWriteArraySet A java.util.Set based on CopyOnWriteArrayList. SyncCollection A wrapper class placing either Syncs or ReadWriteLocks around java.util.Collection SyncSet A wrapper around java.util.Set SyncSortedSet A wrapper around java.util.SortedSet SyncList A wrapper around java.util.List SyncMap A wrapper around java.util.Map SyncSortedMap A wrapper around java.util.SortedMap

Слайд 59





How to choose which Java 
collection class to use?
Описание слайда:
How to choose which Java collection class to use?

Слайд 60





How to choose which Java 
collection class to use?
Описание слайда:
How to choose which Java collection class to use?

Слайд 61





How to choose which Java 
collection class to use?
Описание слайда:
How to choose which Java collection class to use?

Слайд 62





Open Source Collections Libraries in Java
Apache Commons Collections Package
Guava-libraries (Google Collections Library)
Trove high performance collections for Java
The Mango Library
Описание слайда:
Open Source Collections Libraries in Java Apache Commons Collections Package Guava-libraries (Google Collections Library) Trove high performance collections for Java The Mango Library

Слайд 63





Questions
?
Описание слайда:
Questions ?



Похожие презентации
Mypresentation.ru
Загрузить презентацию