12 Generic collections ▫ From Java 5, all collection interfaces and classes have been redefined as Generics ▫ Use of generics lead to code that is ◇ safer
Previous PDF | Next PDF |
[PDF] Durga Book 1cdr - DURGA SOFTWARE SOLUTIONS
N Durga Prasad 98 java, but after going through the training sessions which were taken by Durga sir regarding SCJP and SCWCD, I just got immense
[PDF] Scjp material download - quigalosiconsheathneter
2 fév 2009 · material pdf Download Scjp study material pdf 05/07/2011 · This Lecture Notes -Free Download,Entrance exam &We offer you the latest 7- SCJP/OCJP Notes Durga Sir 8- Oracle 1- JavaEE / J2EE Naresh Sir Noes ->
[PDF] Core java notes by madhu - Amazon S3
Open source and free software Intial name Generally there is no core java and advanced java in java As per the sun Download the software from internet based on your operating system StringBuffer s2=new StringBuffer("durgasoft");
[PDF] Data Structures and Algorithms in Java
advanced algorithms and data structures course, such as CS210 (T/W/C/S versions) justified, given the importance of efficient data structures in most software systems, including the Web Slides in Powerpoint and PDF (one-per- page) format developed a basic Java tutorial, which ultimately led to Chapter 1 , Java
[PDF] Corejava Mr Ratan - WordPresscom
23 août 2016 · Durgasoft Mr Ratan 1 Page Durgasoft Mr Ratan 7 Page Learning process of java:- Corejava Adv java JDBC java is a open source software we are able to download it free of cost and we are able to see the source
[PDF] Java Collection Framework
12 Generic collections ▫ From Java 5, all collection interfaces and classes have been redefined as Generics ▫ Use of generics lead to code that is ◇ safer
[PDF] Notes on Data Structures and Programming Techniques - Computer
HTML • PDF Code examples can be downloaded from links in the text, or can be found in the GDB is free software, covered by the GNU General Public License, and you are like vim, I can change the default using git config --global core editor, different from the convention in C++ or Java, which encourage variables
[PDF] Python - SeleniumByMahesh
Python is now maintained by a core development team at the institute,although -->Download the required python 3 7 version from "http://python org" website --> NTFS Ex: Jython is customized version of python to work with java applications Dasari''' s="""Durga Soft""" -->We can embeded one string in another string
[PDF] advanced java notes for mca pdf vtu
[PDF] advanced java notes pdf in hindi
[PDF] advanced java programming book
[PDF] advanced java programming course objectives
[PDF] advanced java programming course outcomes
[PDF] advanced java programming course outline
[PDF] advanced java programming course syllabus
[PDF] advanced java programming examples
[PDF] advanced java programming free course
[PDF] advanced java programming lab syllabus
[PDF] advanced java programming lecture notes
[PDF] advanced java programming notes
[PDF] advanced java programming notes for mca
[PDF] advanced java programming notes for msc pdf
Java Collection Framework
Version March 2009
2Framework
Interfaces (ADT, Abstract Data Types)
Implementations (of ADT)
Algorithms (sort)
java.util.*
Java 5 released!
Lots of changes about collections
3Interfaces
Collection
SetList
Map
SortedSet
SortedMap
Group containers
Associative containers
Queue
Iterable
4 Implementations
TreeSet
TreeMapArray
ListLinked
ListHashMap
Linked
HashMap
Linked
HashSet
HashSet
Map
Sorted
Map
Collection
SetList
Sorted
Set
Queue
Priority
Queue 5Internals
LinkedList
Linked list
LinkedHashMapTreeMapHashMapMap
ArrayListList
LinkedHashSet TreeSetHashSetSet
Hash table
Linked list
Balanced
treeResizable
array Hash table interface data structure classes 6Collection
Groupof elements (referencesto objects)
It is not specified whether they are
Ordered / not ordered
Duplicated / not duplicated
Following constructors are common to all
classes implementing CollectionT()
T(Collection c)
7Collection interface
int size()
boolean isEmpty()
boolean contains(Object element)
boolean containsAll(Collection c)
boolean add(Object element)
boolean addAll(Collection c)
boolean remove(Object element)
boolean removeAll(Collection c)
void clear()
Object[] toArray()
Iterator iterator()
8Collection example
Collection persons =
new LinkedListSystem.out.println( persons.size() );
Collection copy =
new TreeSetPerson[] array = copy.toArray();
System.out.println( array[0] );
9 MapAn object that associates keys to values
HBJB 661 ҧ 3HUVRQ
Keys and values must be objects
Keysmust be unique
Only one value per key
Following constructors are common to all
collection implementersT()
T(Map m)
10Map interface
Object put(Object key, Object value)
Object get(Object key)
Object remove(Object key)
boolean containsKey(Object key)
boolean containsValue(Object value)
public Set keySet()
public Collection values()
int size()
boolean isEmpty()
void clear()
11Map example
MapPerson bob = people.get³5%7*51´
if( bob == null)6\VPHPBRXPBSULQPOQ ³1RP IRXQG´
int populationSize = people.size(); 12Generic collections
From Java 5, all collection interfaces
and classes have been redefined asGenerics
Use of generics lead to code that is
safer
more compact
easier to understand
equally performing
13Generic list -excerpt
public interface ListIterator iterator();
public interface IteratorE next();
booleanhasNext(); 14Example
Using a list of Integers
Without generics ( ArrayListlist )
list.add(0, new Integer(42));intn= ((Integer)(list.get(0))).intValue();With generics ( ArrayList list )
list.add(0, new Integer(42));intn= ((Integer)(list.get(0))).intValue(); + autoboxing( ArrayList list )
list.add(0,new Integer(42));inttotal = list.get(0).intValue(); Group containers
(Collections)CollectionSetList
SortedSet
Queue
16 List Can contain duplicateelements
Insertion orderis preserved
User can define insertion point
Elements can be accessed by position
Augments Collection interface
17List additional methods
Object get(int index)
Object set(int index, Object element)
void add(int index, Object element)
Object remove(int index)
boolean addAll(int index, Collection c)
int indexOf(Object o)
int lastIndexOf(Object o)
List subList(int fromIndex, int toIndex)
18List implementations
ArrayList
get(n)
Constant time
Insert (beginning)
and delete while iteratingLinear
LinkedList
get(n)
Linear time
Insert (beginning)
and delete while iteratingConstant
19List implementations
ArrayList
ArrayList()
ArrayList(int initialCapacity)
ArrayList(Collection c)
void ensureCapacity(int minCapacity)
LinkedList
void addFirst(Object o)
void addLast(Object o)
Object getFirst()
Object getLast()
Object removeFirst()
Object removeLast()
20Example I
LinkedListll =
new LinkedListExample II
Car[] garage = new Car[20];
garage[0] = new Car(); garage[1] = new ElectricCar(); garage[2] = new ElectricCar(); garage[3] = new Car(); for(int i=0; iListgarage = new ArrayList(20);
garage.set( 0, new Car() ); garage.set( 1, new ElectricCar() ); garage.set( 2, new ElectricCar() ); garage.set( 3, new Car()); for(int i; iExample III
List l = new ArrayList(2); // 2 refs to null
l.add(new Integer(11)); // 11 in position 0 l.add(0, new Integer(13)); // 11 in position 1 l.set(0, new Integer(20)); // 13 replaced by 20 l.add(9, new Integer(30)); // NO: out of bounds l.add(new Integer(30)); // OK, size extended 23Queue
Collection whose elements have an
order (not and ordered collection though
Defines a headposition where is the
first element that can be accessedpeek()
poll()
24Queue implementations
LinkedList
head is the first element of the list
FIFO: Fist-In-First-Out
PriorityQueue
head is the smallest element
25Queue example
Queue fifo =
new LinkedListQueue pq =
new PriorityQueueSystem.out.println(fifo.peek()); // 3
System.out.println(pq.peek());// 1
26Set
Contains no methods other than those
inherited from Collectionadd()has restriction that no duplicate
elementsare allowede1.equals(e2) == false
e1,e2Iterator
The elements are traversed in no
particular orderThe equals() Contract
It is reflexive: x.equals(x) == true
It is symmetric: x.equals(y) == y.equals(x)
It is transitive: for any reference values x, y, and z, if x.equals(y) == true AND y.equals(z) == true => x.equals(z) == true It is consistent: for any reference values x and y, multiple invocations of x.equals(y) consistently return true (or false), provided that no information used in equals comparisons on the object is modified. x.equals(null) == false hashCodeThe hashCode() contract
The hashCode() method must consistently
return the same int, if no information used in equals() comparisons on the object is modified.If two objects are equal for equals() method,
then calling the hashCode() method on the two objects must produce the sameintegerresult.If two objects are unequal for equals() method,
then calling the hashCode() method on the two objects MAY produce distinct integer results. producing distinct intresults for unequal objects may improve the performance of hashtablesHashCode()
equals() and hashcode()equals() and hashCode() are bound together by
a joint contract that specifies if two objects are considered equal using the equals() method, then they must have identical hashcodevalues.To be truly safe:
If override equals(), override hashCode()
Objects that are equals have to return identical hashcodes. 32SortedSet
No duplicate elements
Iterator
The elements are traversed according to the
natural ordering(ascending)Augments Set interface
Object first()
Object last()
SortedSet headSet(Object toElement)
SortedSet tailSet(Object fromElement)
SortedSet subSet(Object from, Object to)
33Set implementations
HashSetimplements Set
Hash tables as internal data structure
(faster)LinkedHashSetextends HashSet
Elements are traversed by iterator
according to the insertion orderTreeSetimplements SortedSet
R-B trees as internal data structure
(computationally expensive) 34Note on sorted collections
Depending on the constructor used
they require different implementation of the custom orderingTreeSet()
Natural ordering (elements must be
implementations of Comparable)TreeSet(Comparator c)
Ordering is according to the comparator
rules, instead of natural orderingAssociative containers
(Maps)Map
SortedMap
36SortedMap
The elements are traversed according
PR POH NH\V· natural ordering
(ascending)Augments Map interface
SortedMap subMap(K fromKey, K toKey)
SortedMap headMap(K toKey)
SortedMap tailMap(K fromKey)
K firstKey()
K lastKey()
37Map implementations
Analogous of Set
HashMapimplements Map
No order
LinkedHashMapextends HashMap
Insertion order
TreeMapimplements SortedMap
Ascending key order
38HashMap
Get/set takes constant time(in case of
no collisions)Automatic re-allocation when load
factor reachedConstructor optional arguments
load factor(default = .75)
initial capacity(default = 16)
39Using HashMap
Map students =
new HashMapQHR 6PXGHQP³123´³-RH 6PLPO´
Student s = students.get³123´
for(Student si: students.values()){Iterators
41Iterators and iteration
A common operation with collections
is to iterate over their elementsInterface Iterator provides a
transparent means to cycle through all elements of a CollectionKeeps track of last visitedelement of
the related collectionEach time the current element is
queried, it moves on automatically 42Iterator interface
boolean hasNext()
Object next()
void remove()
43Iterator examples
Collection persons =
new LinkedListPerson p = i.next();
System.out.println(p);
Print all objects in a list
44Iterator examples
Collection persons =
new LinkedListSystem.out.println(p);
The for-each syntax avoids
using iterator directly 45Iteration examples
Map people =
new HashMapCollection values = people.values();
for(Person p: values) { System.out.println(p);
Print all values in a map
(variant using while) 46Iteration examples
Map people =
new HashMapCollection keys = people.keySet();
for(String ssn: keys) { Person p = people.get(ssn);
System.out.println(ssn + " -" + p);
Print all keys AND values in a map
47Iterator examples (until Java 1.4)
Collection persons = new LinkedList();"
for(Iteratori= persons.iterator(); i.hasNext();){Person p = (Person)i.next();"}Print all objects in a list
48Iteration examples (until Java 1.4)
Map people = new HashMap();"Collection values = people.values();Iteratori= values.iterator();while(i.hasNext()){Person p = (Person)i.next();"}
Print all values in a map
(variant using while) 49Iteration examples (until Java 1.4)
Map people = new HashMap();"Collection keys = people.keySet();for(Iteratori= keys.iterator(); i.hasNext();){String ssn= (String)i.next();Person p = (Person)people.get(ssn);"}