-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPractice19.java
More file actions
103 lines (85 loc) · 3.48 KB
/
Practice19.java
File metadata and controls
103 lines (85 loc) · 3.48 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
Collection in java is the base interface. It represent the set of classes and interfaces.
The collection in Java is a framework that provides an architecture to store and manipulate the group of objects.
- Types are:
1) List : ArrayList, LinkedList, Vector->Stack(Legacy Class) (Mentain the order using index)
2) Set : HashSet->LinkedHashSet, SortedSet->NavigableSet->TreeSet (Set can't mentain the order)
3) Queue : Priority Queue, Blocking Queue->Priority Blocking Queue and Linked Blocking Queue
- Java has "collections class", which is very used for different operations as it has pre-defined methods.
- Every child class(list, set, vector and many) hold the features of parent class as well.
- Methods in collection interface:
boolean add(obj) int size()
boolean addAll(collection) boolean conatins(obj)
boolean remove(obj) void clear()
boolean removeAll(collection) boolane isEmpty()
boolean retaiinAll(collection) Iterator iterator()
- Methods in list:
void add(int index, E element) datatype remove(int index)
datatype get(int index) List<datatype> subList(int from index, int to index)
int indexOF(obj)
int lastIndexOF(obj)
- Methods in ArrayList:
defined using dynamic arrays order of elements are preserved
resizable null insertion possible
duplicates are allowes Heterogenous obj are allowed
- Methods in LinkedList:
void addFirst(obj) obj removeFirst()
void addLast(obj) obj removeLast()
obj.getFirst()
obj.getLast()
- Array is faster in terms of searching but linded list faster in terms of insertions and deletion.
- Vector is thread safe and its methods are synchronized, low performance as compare to ArrayList.
- Mehods in Vector:
void addElement(obj) copyInto(obj[] array)
clear() elements()
clone() int capacity()
contain(obj) boolean equals(obj)
containAll(obj)
- Stack works on LIFO approach.
- Methods in stack:
obj.push() obj.empty()
obj.pop() obj.search()
obj.peek()
Note: ArrayList size increases automaitcally(one by one) after mentioning the size and any type of date can be store.
In Vetor, the size increases exponentially(double the original size).
*/
import java.util.*;
class Practice19
{
public static void main(String[] args)
{
// ArrayList
ArrayList arr = new ArrayList(5);
arr.add("A");
arr.add("B");
arr.add("C");
arr.add(2, "G");
Iterator i1 = arr.iterator();
while(i1.hasNext())
{
String s = (String)i1.next();
if(s != "B")
{
System.out.println(s);
}
}
System.out.println(arr);
// LinkedList
LinkedList l1 = new LinkedList();
l1.addFirst("Hello");;
l1.addLast("World");
l1.add(1, "Welcome");
System.out.println(l1.getFirst());
System.out.println(l1.get(1));
System.out.println(l1.getLast());
// Vector
Vector v = new Vector(); // During size increase, Double size increases of the previous size.
// Stack
Stack st = new Stack();
st.push(10);
st.push(30);
st.push(40);
st.push(20);
System.out.println(st.search(40));
}
}