-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathJavaCollectionFramework.txt
More file actions
54 lines (29 loc) · 1.84 KB
/
Copy pathJavaCollectionFramework.txt
File metadata and controls
54 lines (29 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
Inserting null values in TreeSet and HashSet:
For an Empty TreeSet as first element null value can be inserted but after inserting that first value if we are trying to insert any other objects then we will get NullPointerException.
For an non empty TreeSet if we are trying to inser null value, at run time u will get nullPointerException. This is because when some elements are there in the tree then before inserting any object it compares the new object to the existing ones by the compareTo() method and decides where to put the new object. So when inserting null the compareTo() method internally throws NullPointerException.
For HashSet only one null value insertion is possible. When we try to add more null values the add method returns false and null is not added.
package : java.util
A collection is an object that is composed of elements --> elements can be either primitive or references to objects.
Array:
collection of elements
same type, and both primitive and reference/object type elements
stored contiguous in memory
random access
Array name stores a reference to the beginning of the space allocated.
drawbacks:
fixed size
space must be allocated before any elements stored
insertions/deletions not possible or involve lot of code
Collection classes:
Collection classes can only handle reference types. [This is why we got autoboxing/unboxing concept for primitive types]
Each instance of a collection class is a collection of elements.
Each element is an object.
Storage structures for Collection classes:
Contiguous-collection class: ArrayList
Linked-collection class : LinkedList (Single-linkedlist, double-linkedlist)
Java colleciton framework has few abstract classes:
AbstractCollection
AbstractList
AbstractSet
The Java Collection Framework:
Top of the hierarchy : 2 interfaces -> Collection and Map