© Luxoft Training 2013 Java Collections API. © Luxoft Training 2013 Collections hierarchy.

Презентация:



Advertisements
Похожие презентации
© Luxoft Training 2013 Java Collections Framework.
Advertisements

Java Collections Framework (JCF) in Java Tutorial for students of universities Author: Oxana Dudnik.
1/27 Chapter 9: Template Functions And Template Classes.
Unit II Constructor Cont… Destructor Default constructor.
© Luxoft Training 2013 Using Reflection API in Java.
1 © Luxoft Training 2012 Inner and anonymous classes.
© Luxoft Training 2013 Annotations. © Luxoft Training 2013 Java reflection / RTTI // given the name of a class, get a "Class" object that // has all info.
2005 Pearson Education, Inc. All rights reserved. 1 Object-Oriented Programming: Interface.
Michael Marchenko. In mathematics, a sequence is an ordered list of objects (or events). Like a set, it contains members (also called elements, or terms),
Inner Classes. 2 Simple Uses of Inner Classes Inner classes are classes defined within other classes The class that includes the inner class is called.
In mathematics, the notion of permutation is used with several slightly different meanings, all related to the act of permuting (rearranging) objects.
Sequences Sequences are patterns. Each pattern or number in a sequence is called a term. The number at the start is called the first term. The term-to-term.
Combination. In mathematics a combination is a way of selecting several things out of a larger group, where (unlike permutations) order does not matter.
© 2009 Avaya Inc. All rights reserved.1 Chapter Two, Voic Pro Components Module Two – Actions, Variables & Conditions.
1/30 Chapter 8: Dynamic Binding And Abstract classes.
Operator Overloading Customised behaviour of operators Chapter: 08 Lecture: 26 & 27 Date:
HPC Pipelining Parallelism is achieved by starting to execute one instruction before the previous one is finished. The simplest kind overlaps the execution.
Section 2.1: Use Inductive Reasoning Conjecture: A conjecture is an unproven statement that is based on observations; an educated guess. Inductive Reasoning:
Multiples Michael Marchenko. Definition In mathematics, a multiple is the product of any quantity and an integer. in other words, for the quantities a.
© 2005 Cisco Systems, Inc. All rights reserved. BGP v Route Selection Using Policy Controls Applying Route-Maps as BGP Filters.
Транксрипт:

© Luxoft Training 2013 Java Collections API

© Luxoft Training 2013 Collections hierarchy

© Luxoft Training 2013 Collections hierarchy 4-3

© Luxoft Training 2013 Collection methods 4-4

© Luxoft Training 2013 Iterator interface boolean hasNext() Object next() return the next element void remove() removes the element returned by next() remove() is a safe way to remove element 4-5

© Luxoft Training 2013 Use of iterator 4-6 public void dumpCollection(Collection c) { System.out.println("Collection has" + c.size() + " elements."); Iterator iter = c.iterator(); while (iter.hasNext()) { Object elem = iter.next() System.out.println("Next element is" + elem); if (getClause(elem)) { iter.remove(); }

© Luxoft Training 2013 List interface List keep the order of elements List is indexed from 0 List is alike array, but can change the size List methods: 4-7

© Luxoft Training 2013 ArrayList ArrayList – array-based list implementation Quick search Slow growth, because array have to be copied for the growth Slow removal, because array have to be 4-8

© Luxoft Training 2013 ArrayList: add an element

© Luxoft Training 2013 ArrayList: insert an element

© Luxoft Training 2013 LinkedList

© Luxoft Training 2013 LinkedList: add an element

© Luxoft Training 2013 LinkedList: add to the middle of the list

© Luxoft Training 2013 Example: LinkedOrArrayListTutor List 4-14

© Luxoft Training 2013 Time of work for arrayList add(): 55 1 bln. Time of work for linkedList add(): 2481 bln. Time of work for arrayList get(): 1100 thousands. Time of work for linkedList get(): thousands. Time of work for arrayList remove(): Time of work for linkedList remove(): Time of work for arrayList insert in the middle: Time of work for linkedList insert in the middle: Time of work for arrayList contains(): Time of work for linkedList contains(): Comparision of ArrayList and LinkedList iterations

© Luxoft Training 2013 Set interface Set has no order – just a set of elements Set cannot have duplicate elements On adding the existing element add() returns false and doesnt insert element To compare elements set use equals(), not == 4-16

© Luxoft Training 2013 Collections 4-17 The java.util.Set interface

© Luxoft Training 2013 Collections The java.util.HashSet implementation stores elements in special hash set. You cant predict the order in which the elements will be returned by iterator() 4-18 java.util.HashSet implementation

© Luxoft Training 2013 java.util.HashSet implementation No matter how many elements a HashSet contains, it basic operations will always execute in constant time HashSet stores elements in buckets grouped by hashCode() value 4-19

© Luxoft Training 2013 HashSet The acces to the element in HashSet occupies a constant time HashSet keep elements in the buckets grouped by value of hashCode() The order of elements is not determined. 4-20

© Luxoft Training 2013 HashSet

© Luxoft Training 2013 java.util.TreeSet implementation The java.util.TreeSet implementation guarantees that elements enumerated by iterator() will always be presented in sorted order Amount of time required to access elements is log(n), where n is the number of elements in the set 4-22

© Luxoft Training 2013 java.util.TreeSet implementation The java.util.SortedSet interface extends java.util.Set and presents sorted set of elements Elements added to such a set are resorted; java.util.SortedSet allows to receive elements in a certain order 4-23

© Luxoft Training 2013 java.util.TreeSet implementation At the same time we should define the method for comparison of collection elements (well examine it later) One of the implementations of java.util.SortedSet is the java.util.TreeSet 4-24

© Luxoft Training 2013 Collections 4-25 The java.util.SortedSet methods

© Luxoft Training 2013 Examples: CollectionTutor CollectionRemoveTutor Collections 4-26

© Luxoft Training 2013 Compare elements As far as TreeSet class supports elements sorting it needs the mechanism that would allow to compare two objects The following interfaces can be used for comparison: java.lang.Comparable java.util.Comparator 4-27

© Luxoft Training 2013 Comparable interface java.lang.Comparable defines public int compareTo(Object x) method returns a positive number if the current object is greater than х and a negative number if the current object isless than х, and 0 if the current object is equal to х 4-28

© Luxoft Training 2013 Comparable interface All elements inserted to java.util.TreeSet must implement the Comparable java.util.TreeSet allows for any Object subclass to be inserted. However, if the element doesnt implement Comparable the runtime will throw ClassCastException 4-29

© Luxoft Training 2013 Comparable interface Many Core Java classes implement Comparable (String, Date, Integer) Implementation of Comparable must define so called natural ordering For instance, lexicographical order for strings 4-30

© Luxoft Training 2013 Comparable interface For the Student class the natural order is not so evident, as students can be sorted by names, last names, grades, year of enrollment, etc. When natural order cant be used or some other way of elements comparison is needed use java.util.Comparator 4-31

© Luxoft Training 2013 Comparator interface You can also define a class implementing the java.util. Comparator interface that compares objects of the given type Corresponding Comparator can be passed to TreeSet using special constructor int compare(Object o1, Object o2) compares two objects 4-32

© Luxoft Training 2013 Collections 4-33 java.lang.Comparator

© Luxoft Training 2013 Collections Natural ordering Comparable paradigm and Comparators mechanism are used not only in TreeSet sorting, but also when sorting arrays and other data structures The purpose of these tools is to make it possible to compare objects of given type 4-34 java.lang.Comparator

© Luxoft Training 2013 Example: ComparableTutor java.lang.Comparator 4-35

© Luxoft Training 2013 Collections diagram 4-36

© Luxoft Training 2013 java.util.Map interface java.util.Map combines two collections, called keys and values Map associates exactly one value (it can be null ) with each key Each key is used in Map only once Good example is a dictionary 4-37

© Luxoft Training 2013 The java.util.Map interface 4-38

© Luxoft Training 2013 The java.util.Map implementations 4-39

© Luxoft Training 2013 Collections java.util.HashMap iterates keys in an unpredictable order. Keys are quickly accessed The hashCode() method must be overridden and must return uniformly distinct integers The equals() method is used to check if the elements are equal (not ==) 4-40 java.util.Map implementations

© Luxoft Training 2013 Collections java.util.TreeMap iterates keys in natural order. Implements the java.util.SortedMap interface Keys must implement the java.lang.Comparable interface Or pass the instance of java.util.Comparator to HashMap constructor 4-41 java.util.Map implementations

© Luxoft Training 2013 Collections Interface java.util.SortedMap extends java.util.Map 4-42 Интерфейс java.util.SortedMap

© Luxoft Training 2013 Collections 4-43 Example

© Luxoft Training 2013 Example: MapTutor Collections java.util.Map 4-44

© Luxoft Training 2013 Support classes There are two support classes that provide static methods that operate on collections and arrays: java.util.Collections java.util.Arrays 4-45

© Luxoft Training 2013 Collections 4-46 Some java.util.Collections methods

© Luxoft Training 2013 Collections 4-47 Some java.util.Arrays methods

© Luxoft Training 2013 Example: CollectionUtilitiesTutor Collections Some java.util.Arrays methods 4-48