Java Collection API : Built-in Data Structures for Java .

Uploaded on:
Category: Medical / Health
Java Accumulation Programming interface : Inherent Information Structures for Java. in view of unique presentation by C.- C. Chen. The Java Accumulation Programming interface. Interfaces: Gathering ? Set ? SortedSet, List Map ? SortedMap Iterator ? ListIterator Comparator. Rundown of all interfaces in the java Gathering Programming interface.
Slide 1

Java Collection API : Built-in Data Structures for Java in light of unique introduction by C.- C. Chen

Slide 2

The Java Collection API Interfaces: Collection  Set  SortedSet, List Map  SortedMap Iterator  ListIterator Comparator

Slide 3

Summary of all interfaces in the java Collection API Collection Interfaces : The essential means by which accumulations are controlled. Accumulation  Set, List A gathering of items. Might be requested; May or may not contain copies. Set  SortedSet The well known set reflection. No copies; May or may not be requested. SortedSet components consequently sorted, either in their normal requesting (see the Comparable interface), or by a Comparator protest gave when a SortedSet case is made. List Ordered accumulation, otherwise called a succession . Copies allowed; Allows positional get to.

Slide 4

Map SortedMap A mapping from keys to values. Each key can guide to at most one esteem (work). SortedMap A guide whose mappings are consequently sorted by key, either in the keys\' regular requesting or by a comparator gave when a SortedMap example is made.

Slide 5

classes of the java gathering API AbstractCollection ( Collection )  AbstractSet ( Set )  HashSet , TreeSet ( SortedSet ) AbstractList ( List )  ArrayList , AbstractSequentialList  LinkedList AbstractMap (Map)  HashMap TreeMap (SortedMap) WeakHashMap Arrays Collections

Slide 6

General-Purpose Implementation classes The essential usage of the accumulation interfaces. HashSet : Hash table execution of the Set interface. TreeSet : Red-dark tree execution of the SortedSet interface. ArrayList : Resizable-exhibit usage of the List interface. (Basically an unsynchronized Vector.) The best all-around usage of the List interface. LinkedList : Doubly-connected rundown usage of the List interface. May give preferred execution over the ArrayList usage if components are much of the time embedded or erased inside the rundown. Helpful for lines and twofold finished lines (deques). HashMap : Hash table usage of the Map interface. (Basically an unsynchronized Hashtable that backings invalid keys and values.) The best all-around usage of the Map interface. TreeMap : Red-dark tree execution of the SortedMap interface.

Slide 7

The java.util.Collection interface speaks to a gathering of articles, known as its components. no immediate execution The essential utilize : go around accumulations of items where greatest simplification is wanted. Ex: List l = new ArrayList( c ) ;/c is a Collection protest

Slide 8

The definition open interface Collection {/Basic properties int measure(); boolean isEmpty(); boolean contains(Object component);/utilize rises to() for correlation boolean equal(Object); int hashCode();/new equivalents() requires new hashCode()/fundamental operations boolean add(Object);/Optional; return genuine if this changed boolean remove(Object);/Optional; utilize breaks even with() (not ==)

Slide 9

Why utilizing Iterators rather than Enumerations Iterator permits the guest to expel components from the hidden accumulation amid the emphasis with all around characterized semantics. Technique names have been made strides. open interface Iterator { boolean hasNext();/cf: hasMoreElements() Object next();/cf: nextElement() void expel();/Optional }/a basic Collection channel utilizing iterator that Enumeration couldn\'t help static void filter(Collection c) { for (Iterator i = c.iterator(); i.hasNext(); ) { if ( no-great ( ) i.remove() ;/ is expelled from c i.remove();/special case raised, can\'t evacuate more than once ! }

Slide 10

The Set Interface is a Collection that can\'t contain copy components. models the scientific set deliberation. contains no techniques other than those acquired from Collection. same marks yet extraordinary semantics ( meaning ) Collection c = new LinkedList(); Set s = new HashSet(); String o = "a"; c.add(o); c.add(o) ;/both return genuine; c.size() == 2 s.add(o); s.add(o) ;/second include() returns false; s.size() == 1 It includes the confinement that copy components are denied. Gathering noDups = new HashSet(c);/a basic approach to take out copy from c Two Set items are equivalent in the event that they contain similar components. Two direct executions: HashSet TreeSet

Slide 11

Basic Operations A basic program to distinguish copies utilizing set: import java.util.*; open class FindDups { open static void main(String args[]) { Set s = new HashSet();/or new TreeSet() , another usage of Set/taking after code utilizes Set techniques just for (int i=0; i<args.length; i++) if ( !s.add(args[i]) ) System.out.println("Duplicate recognized: " + args[i]); System.out.println(s.size()+" particular words identified: "+s); } % java FindDups i came i saw i exited Duplicate identified: i Duplicate identified: i 4 unmistakable words recognized: [came, left, saw, i]

Slide 12

The Map Interface A Map is a question that maps keys to values. A guide can\'t contain copy keys: Each key can guide to at most one esteem. Three usage: HashMap , which stores its entrances in a hash table, is the best-performing execution. TreeMap , which stores its entrances in a red-dark tree, ensures the request of cycle. Hashtable has additionally been retrofitted to actualize Map. All usage must give two constructors: (like Collections) Assume M is your execution M()/purge outline m)/a duplicate of guide from m

Slide 13

The Map interface open interface Map {/Map does not develop Collection/Basic Operations/put or supplant, return supplant protest //NOTE: not quite the same as Weiss\' insert(Hashable x) Object put(Object key, Object esteem);/discretionary Object get(Object key); Object remove(Object key); boolean containsKey(Object key); boolean containsValue(Object esteem); int measure(); boolean isEmpty();

Slide 14

The Map interface/Bulk Operations void putAll(Map t);/discretionary void clear();/discretionary/Collection Views;/upheld by the Map, change on either will be pondered the other. open Set keySet();/can\'t copy by definition!! open Collection values();/can copy open Set entrySet();/no proportionate in Dictionary/settled Interface for entrySet components open interface Entry { Object getKey(); Object getValue(); Object setValue(Object esteem); }

Slide 15

Basic Operations a basic program to create a recurrence table import java.util.*; open class Freq { private static last Integer ONE = new Integer(1) ; open static void main(String args[]) { Map m = new HashMap();/Initialize recurrence table from summon line for (int i=0; i<args.length; i++) { Integer freq = (Integer) m.get(args[i]);/key is a string m.put(args[i], (freq==null ? ONE : new Integer( freq.intValue() + 1))); }/esteem is Integer System.out.println( m.size() +" particular words detected:"); System.out.println(m); } > java Freq on the off chance that it is to be it is dependent upon me to delegate 8 unmistakable words recognized: {to=3, me=1, delegate=1, it=2, is=2, if=1, be=1, up=1}

Slide 16

Collection Views techniques permit a Map to be seen as a Collection in three ways: keySet : the Set of keys contained in the Map. values: The Collection of qualities contained in the Map. This Collection is not a Set, as numerous keys can guide to a similar esteem. entrySet: The Set of key-esteem sets contained in the Map. The Map interface gives a little settled interface called Map.Entry that is the kind of the components in this Set. the standard expression for emphasizing over the keys in a Map: for (Iterator i = m. keySet ().iterator(); i.hasNext(); ) { System.out.println(; if(no-great(… )) i.remove() ; }/bolster expulsion from the back Map Iterating over key-esteem sets for (Iterator i=m. entrySet ().iterator(); i.hasNext(); ) { Map.Entry e = (Map.Entry); System.out.println(e. getKey () + ": " + e. getValue ()); }

Slide 17

Actual Collection and Map Implementations are the genuine information objects used to store accumulations (and Maps). Three sorts of usage: General-reason Implementations the general population classes that give the essential executions of the center interfaces. Wrapper Implementations utilized as a part of mix with different usage (regularly the broadly useful executions) to give included usefulness . Comfort Implementations Convenience usage are smaller than expected executions , regularly made accessible by means of static manufacturing plant techniques that give advantageous, productive contrasting options to the broadly useful executions for extraordinary accumulations (like singleton sets).

Slide 18

General Purpose Implementations

Slide 19

Properties of the executions steady names and additionally reliable conduct. full usage [of all the discretionary operations]. All allow invalid components, keys and values. unsynchronized . cure the insufficiency of Hashtable and Vector can get to be distinctly synchronized through the synchronization wrappers All have bomb quick iterators, which recognize illicit simultaneous adjustment amid emphasis and flop rapidly and neatly. All are Serializable , all bolster an open clone() strategy. ought to consider the interfaces as opposed to the usage. The decision of

View more...