Skip to content

Commit 07a3bb2

Browse files
committed
initial commit
1 parent 16cb763 commit 07a3bb2

File tree

8 files changed

+603
-0
lines changed

8 files changed

+603
-0
lines changed

JavaCollections/.classpath

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>

JavaCollections/.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>JavaCollections</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.8
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.8
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/ArrayListExample.class
2+
/LinkedListExample.class
3+
/StackExample.class
4+
/VectorExample.class
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
package listExamples;
2+
import java.util.ArrayList;
3+
import java.util.Iterator;
4+
import java.util.List;
5+
import java.util.ListIterator;
6+
import java.util.concurrent.CopyOnWriteArrayList;
7+
public class ArrayListExample {
8+
/** List(I) is the Child of Collection(I).
9+
* ArrayList (C) is one of the classes provides implementation for the List(I).
10+
* In list duplicate values are allowed and the insertion order is maintained.
11+
* The underlying DS is resizeable Array or Growable Array. We can insert Heterogeneous objects as well.
12+
* NOTE: All the collections can store Heterogeneous objects can be stored except TREE SET and TREE MAP.
13+
* ArraList and vector implements RandomAccess, Serializable and Cloneable Interfaces
14+
* Synchronized-> No
15+
* Thread safe-> NO
16+
*/
17+
public void arrayListExample(){
18+
19+
//add() used to add the specified element at the end of the List
20+
List<String> arrayList= new ArrayList<String>();
21+
arrayList.add("Audi");
22+
arrayList.add("Benz");
23+
arrayList.add("Bugatti");
24+
arrayList.add("Aston martin");
25+
26+
System.out.println(arrayList);
27+
28+
//add(int index, Object element) method will add at specified index position
29+
arrayList.add(0,"Mustang");
30+
System.out.println(arrayList);
31+
32+
//to check the indexof any element
33+
System.out.println("Index position of Audi is :"+arrayList.indexOf("Audi"));
34+
//-1 is returned if the element is not there
35+
System.out.println("Index position of Ambassador is :"+arrayList.indexOf("Ambassador"));
36+
37+
//List allows duplicate elements
38+
arrayList.add("Mustang");
39+
System.out.println(arrayList);
40+
41+
//indexof always returns the first occurrence
42+
System.out.println("Index position of Mustang is :"+arrayList.indexOf("Mustang"));
43+
//last index of is used to check the last occurence position
44+
System.out.println("Last Index position of Mustang is :"+arrayList.lastIndexOf("Mustang"));
45+
46+
// add a list to another list
47+
List<String> anotherList=new ArrayList<String>();
48+
anotherList.addAll(arrayList);
49+
System.out.println("New List copied :"+ anotherList);
50+
51+
//clear to delete all the elements
52+
anotherList.clear();
53+
System.out.println("List after clearing "+anotherList);
54+
55+
//we can even insert null
56+
anotherList.add(null);
57+
System.out.println("After adding null "+anotherList);
58+
anotherList.add("mango");
59+
anotherList.add("Banana");
60+
//adding list to a list at specified position.
61+
anotherList.addAll(0,arrayList);
62+
System.out.println("New list after adding the old list at 0th position :"+anotherList);
63+
64+
//set() is used to update the element based on index
65+
anotherList.set(6, "Tata");
66+
anotherList.set(7,"Civic");
67+
System.out.println("List after updating the last two elements : "+anotherList);
68+
69+
//remove(int position) removes the value at the specified position
70+
anotherList.remove(6);
71+
System.out.println("After removing :"+anotherList);
72+
73+
//remove using object value
74+
anotherList.remove("Civic");
75+
System.out.println("After removing Civic: "+ anotherList);
76+
77+
/*get an element based on index position.
78+
If the index is not there we will get index out of bound exception*/
79+
System.out.println("Element at 5th Position "+anotherList.get(5));
80+
81+
//isEmpty() method to check the list is empty or not
82+
System.out.println("This list is empty. True or False? "+ anotherList.isEmpty());
83+
System.out.println(anotherList);
84+
85+
//get all the elements in the list using for loop
86+
for(int size=0; size<anotherList.size();size++){
87+
System.out.println("element at "+size+"th position " +anotherList.get(size));
88+
}
89+
90+
//fetch using advanced for loop
91+
for (String string : anotherList) {
92+
System.out.println("List elements "+string);
93+
}
94+
95+
//forward traversing using ListIterator
96+
ListIterator<String> listIterator= anotherList.listIterator();
97+
while(listIterator.hasNext()){
98+
System.out.println("Forward Iteration : "+listIterator.next());
99+
}
100+
101+
// reverse Iteration using ListIterator
102+
while(listIterator.hasPrevious()){
103+
System.out.println("Reverse Iteration : "+listIterator.previous());
104+
}
105+
106+
/*Iteration with Iterator (NOTE: Not ListIterator)
107+
Iterator can only traverse forward but not on reverse.
108+
Hence we are using ListIterator for better usages.*/
109+
110+
Iterator< String> iterator=anotherList.iterator();
111+
while(iterator.hasNext()){
112+
System.out.println("Forward Only:"+iterator.next());
113+
}
114+
115+
/* ArrayList is non-synchronized.It should not be used in multi-threaded
116+
* environment without explicit synchronization.Hence,
117+
* adding an element to the list when traversing through it
118+
* will give concurrent modification exception.
119+
* This is happening because when a thread is trying to read the elements
120+
* from the list another thread is trying to add or remove an element from the same.
121+
*/
122+
try{
123+
for (String string : anotherList) {
124+
System.out.println("Reading the list values"+ string);
125+
anotherList.add("Mustang");
126+
}
127+
}
128+
catch(Exception e){
129+
130+
System.out.println("Dude! You are trying to modify a list while a read operation is happening");
131+
}
132+
}
133+
134+
/* CopyOnWriteArrayList allows us to modify
135+
* the list while reading it
136+
*/
137+
public void syncArrayListExample(){
138+
139+
CopyOnWriteArrayList<String> syncal = new CopyOnWriteArrayList<String>();
140+
141+
//Adding elements to synchronized ArrayList
142+
syncal.add("Pen");
143+
syncal.add("NoteBook");
144+
syncal.add("Ink");
145+
System.out.println("Iterating synchronized ArrayList:");
146+
147+
Iterator<String> syncIterator = syncal.iterator();
148+
while (syncIterator.hasNext()){
149+
System.out.println(syncIterator.next());
150+
syncal.add("Eraser");
151+
syncal.remove("Eraser");
152+
153+
154+
}
155+
System.out.println("Final List: "+syncal);
156+
}
157+
158+
public static void main(String[] args) {
159+
// TODO Auto-generated method stub
160+
ArrayListExample example= new ArrayListExample();
161+
example.arrayListExample();
162+
//example.syncArrayListExample();
163+
164+
}
165+
166+
}
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
package listExamples;
2+
import java.util.Iterator;
3+
import java.util.LinkedList;
4+
public class LinkedListExample {
5+
/**
6+
* LinkedList is implemented using Doubly linked list. This is best suited for insertion and deletion operations.
7+
* Unlike ArrayList, this is not the best for retrieving the data.
8+
* All the collections implements Serializable and cloneable Interfaces.
9+
* LinkedList also implements those Interfaces but not RandomAccess Interface.
10+
*/
11+
public void linkedListOperationsExample(){
12+
13+
//Create a simple Linked list
14+
LinkedList< Integer> linkedList= new LinkedList<Integer>();
15+
linkedList.add(2);
16+
linkedList.add(3);
17+
linkedList.add(4);
18+
19+
System.out.println("Linked List :"+ linkedList);
20+
21+
//Add an element to the first position
22+
linkedList.addFirst(1);
23+
System.out.println("Linked List after adding first:"+ linkedList);
24+
25+
//Add an element at last
26+
linkedList.addLast(5);
27+
System.out.println("Linked List after adding last:"+ linkedList);
28+
29+
/*
30+
* LinkedList performs the worst if the operation is retrieval of data.
31+
* Hence if our requirement is to fetch data, we should use ArrayList.
32+
* But LinkedList is the best choice if we have requirements of adding and
33+
* removing data very often. In this case we should avoid ArrayList
34+
*/
35+
36+
//Get the first value
37+
System.out.println("First Value :" +linkedList.getFirst());
38+
39+
//Get the first value using index position
40+
System.out.println("First Value using index :"+linkedList.get(0));
41+
42+
/*
43+
* Now get the third value of the list.
44+
* Since LinkedList has the data structure of doubly linked list,
45+
* the value of the third index is known only to the link present in
46+
* the Second index. Internally, the program will traverse to index number 2 and then only
47+
* it can get the value of 3. So Linked list is not suited for search operations.
48+
*/
49+
System.out.println("Third value of the list : "+ linkedList.get(3));
50+
51+
//update the values using set().
52+
//Integer newFirst=linkedList.get(0);
53+
//linkedList.set(0, newFirst);
54+
//System.err.println("After 0th position updated :"+linkedList);
55+
56+
//removeFirst and removeLast
57+
System.out.println("Remove first :"+linkedList.removeFirst());
58+
System.out.println("Remove last :"+linkedList.removeLast());
59+
60+
//poll method and pollfirst() deletes the first element in the list
61+
System.out.println(linkedList.poll());
62+
System.out.println(linkedList);
63+
64+
//pollLast deletes the last
65+
linkedList.pollLast();
66+
System.out.println(linkedList);
67+
68+
//remove deletes the first element
69+
linkedList.remove();
70+
System.out.println(linkedList);
71+
72+
//Adding
73+
linkedList.addFirst(1);
74+
linkedList.add(2);
75+
linkedList.add(3);
76+
linkedList.add(4);
77+
linkedList.add(5);
78+
linkedList.add(6);
79+
80+
//removeFirstOccurence
81+
linkedList.removeFirstOccurrence(2);
82+
System.out.println("After removing the first occurence of 2 "+ linkedList );
83+
84+
//removeLastOccurrence
85+
linkedList.removeLastOccurrence(6);
86+
System.out.println("After removing the last occurence of 6 "+ linkedList );
87+
88+
System.out.println("-----------------------------------------------------");
89+
}
90+
91+
/*
92+
* Iteration of Linked List with simple for loop
93+
*/
94+
public void iterateLinkedListWithSimpleFor(){
95+
96+
LinkedList< String> linkedList= new LinkedList<String>();
97+
linkedList.add("a");
98+
linkedList.add("b");
99+
linkedList.add("c");
100+
linkedList.add("d");
101+
System.out.println("Simple For loop");
102+
for (int index=0;index<linkedList.size();index++){
103+
System.out.println("Elements in the LL are "+ linkedList.get(index));
104+
}
105+
System.out.println("-----------------------------------------------------");
106+
}
107+
108+
109+
/*
110+
* Iteration of Linked List with Advanced For loop (For each)
111+
*/
112+
113+
public void iterationWithAdvancedFor(){
114+
LinkedList< String> linkedList= new LinkedList<String>();
115+
linkedList.add("a");
116+
linkedList.add("b");
117+
linkedList.add("c");
118+
linkedList.add("d");
119+
System.out.println("For Each");
120+
for (String string : linkedList) {
121+
System.out.println("Elements in the LL are "+ string);
122+
}
123+
System.out.println("-----------------------------------------------------");
124+
}
125+
126+
127+
/*
128+
* Iterate using While
129+
*/
130+
public void iterateUsingWhile(){
131+
LinkedList< String> linkedList= new LinkedList<String>();
132+
linkedList.add("a");
133+
linkedList.add("b");
134+
linkedList.add("c");
135+
linkedList.add("d");
136+
int number=0;
137+
System.out.println("While");
138+
while(linkedList.size()>number){
139+
System.out.println("Elements in the LL are "+ linkedList.get(number));
140+
number++;
141+
}
142+
System.out.println("-----------------------------------------------------");
143+
}
144+
145+
/*
146+
* Iterate using Iterator
147+
*/
148+
public void iterateUsingIterator(){
149+
LinkedList< String> linkedList= new LinkedList<String>();
150+
linkedList.add("a");
151+
linkedList.add("b");
152+
linkedList.add("c");
153+
linkedList.add("d");
154+
Iterator<String> iterator=linkedList.iterator();
155+
System.out.println("Iterator");
156+
while(iterator.hasNext()){
157+
System.out.println("Elements in the LL are "+ iterator.next());
158+
}
159+
System.out.println("-----------------------------------------------------");
160+
}
161+
162+
163+
164+
public static void main(String[] args) {
165+
// TODO Auto-generated method stub
166+
LinkedListExample linkedListExample = new LinkedListExample();
167+
linkedListExample.linkedListOperationsExample();
168+
linkedListExample.iterateLinkedListWithSimpleFor();
169+
linkedListExample.iterationWithAdvancedFor();
170+
linkedListExample.iterateUsingWhile();
171+
linkedListExample.iterateUsingIterator();
172+
}
173+
174+
}

0 commit comments

Comments
 (0)