Boxing & AutoBoxing Java Collection Framework Collection List Map Set Generics vs Inheritance 2
Primitive types (int, float, double, boolean etc.) do not inherit Object We can use wrapper classes encapsulate primitive types within objects: Double - double, Integer - int, etc Many data structures operate on objects and then primitives must often be wrapped before they can be used (this is called boxing) Example : Integer iobject = new Integer(10); Integer intobj = 100; //works well (autobox an int) Unboxing (the reverse process to boxing) extracts integer value Excample : int i = iobject.intvalue(); int ii = intobj; 3
4
A collection is a group of data manipulate as a single object Collections are primarily defined through a set of interfaces. 5
Solid boxes represent concrete classes Dashed boxes indicate interfaces 6
The Collection interface is a group of objects, with duplicates allowed The Set interface extends Collection but forbids duplicates The List interface extends Collection, allows duplicates, and introduces positional indexing. A Map is an object that maps keys to values 7
Interface Implementation Historical Set HashSet TreeSet List ArrayList Vector LinkedList Stack Map HashMap HashTable TreeMap Properties 8
We can use the interface Iterator to enumerate or count through a collection of objects Methods: hasnext() next() remove() Collection c; Iterator it = c.iterator(); //produces iterator while(it.hasnext()){ if(!contains(it.next())) return false; } return true; 9
boolean add (Object o) boolean contains (Object o) boolean remove (Object c) boolean removeall (Collection <?> c) Iterator <E> iterator() Object[] toarray() int size() etc 10
The Set interface extends the Collection interface and forbids duplicates within the collection. HashSet For set where fast search/access is important Based on a hash table TreeSet An ordered set 11
s1.containsall(s2) returns true if s2 is a subset of s1 s1.addall(s2) transforms s1 into the union of s1 and s2 s1.removeall(s2) transforms s1 into the (asymmetric) set difference of s1 and s2 12
13
The List interface extends the Collection interface to define an ordered collection, permitting duplicates. ArrayList fast random element access inserting/removing operations are slow (use LinkedList instead) LinkedList provides optimal sequential access rather slow for random element access (use ArrayList instead) 14
15
16
Arrays have fixed size, which is defined when they are initialized int[] arr = new int[20]; //size fixed to 20 arr[4]; //returns fifth element from the array Collections structures (those implementing Collection interface) are extendible - no fixed size ArrayList, similarly to array, allows to select from a sequence of objects using a number - index (element position within the list) ArrayList arr; arr.get(4); //returns fifth element from the list But ArrayList is also extendible ArrayList<String> = new ArrayList<String>();// no size specification 17
18
A Map is an object that maps keys to values. A map cannot contain duplicate keys Each key can map to at most one value. 19
The keys are like database primary keys in that a value (row) is uniquely identified by its key Allows an object to be stored: put(object key, Object value) Retrieved the value get(object key) 20
TreeMap If you need to traverse the keys in a sorted order HashMap For inserting, deleting, and locating elements in a Map 21
22
23
24
Search and sort: binarysearch(), sort() Reorganization: reverse(), shuffle() Wrappings: unmodifiablecollection, synchonizedcollection 25
Advantages Can hold different types of objects Resizable Disadvantages Must cast to correct type Cannot do compile-time type checking 26
27
Kelas generic: kelas yang masih umum, belum spesifik ketika didefinisikan Pada saat deklarasi objek, hal yang umum harus dibuat spesifik Setelah dibuat spesifik, baru bisa dipakai Biasanya yang umum adalah type -nya,dipakai untuk membungkus operasi yang sama Dalam bahasa C++ menjadi template 28
Kelas generic pada Java diimplementasikan sebagai parameter tipe Hasil kompilasi kode kelas generic tetap hanya satu kelas, dengan parameter type yang diganti dengan type yang seharusnya pada saat runtime Keuntungan generic: Meningkatkan expressiveness power Meningkatkan type safety Mengeksplisitkan parameter type dan mengimplisitkan type casting 29
Mendefinisikan kelas dan interface: class NamaKelas <TypeGeneric> {... } interface NamaInterface <TypeGeneric> {... } class NamaKelas <TypeGeneric1, TypeGeneric2> {... } interface NamaInterface <TypeGeneric1, TypeGeneric2> {... } Nama untuk type generic sebaiknya menggunakan karakter huruf besar, misalnya E dan T Kita dapat mendefinisikan type generic lebih dari satu 30
Kelas dapat digunakan untuk membuat kelas baru dengan menggunakan template kelas generic public class MyList<E> { MyList() {... } public void Add (E o) {... } public E get(int idx) {... } } MyList<String> ls = new MyList<String>(); 31
32
E Element (digunakan pada Java Collections Framework) K Key N Number T Type V Value S, U, V, etc. 2nd, 3rd, 4th types 33