Skip to content

Commit cd81b8e

Browse files
committed
Implementation of Set
1 parent 354bdff commit cd81b8e

File tree

3 files changed

+190
-0
lines changed

3 files changed

+190
-0
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/**
2+
* Data-Structures-And-Algorithms-in-Java
3+
* ArrayBasedSet.java
4+
*/
5+
package com.deepak.data.structures.Set;
6+
7+
/**
8+
* Class implementing Set based on Array
9+
* @author Deepak
10+
*
11+
* @param <T>
12+
*/
13+
public class ArrayBasedSet<T> {
14+
15+
/**
16+
* Array of object
17+
*/
18+
private Object arrayElements[];
19+
20+
/**
21+
* Variable to keep track of Set size
22+
*/
23+
private int size = 0;
24+
25+
/**
26+
* Default capacity of the Set
27+
*/
28+
private int DEFAULT_CAPACITY = 8;
29+
30+
/**
31+
* Constructor to create a new Set
32+
* based on capacity
33+
* @param capacity
34+
*/
35+
public ArrayBasedSet(int capacity) {
36+
arrayElements = new Object[capacity];
37+
}
38+
39+
/**
40+
* Constructor to create a new Set
41+
* based on elements
42+
* @param elements
43+
*/
44+
public ArrayBasedSet(T[] elements) {
45+
arrayElements = elements;
46+
size = arrayElements.length;
47+
}
48+
49+
/**
50+
* Method to add a element to Set
51+
* @param element
52+
*/
53+
public void addElement(T element) {
54+
if (!contains(element)) {
55+
if (size == arrayElements.length) {
56+
incrementArraySize();
57+
}
58+
arrayElements[size++] = element;
59+
}
60+
}
61+
62+
/**
63+
* Method to check if element exists in Stack
64+
* @param element
65+
* @return {@link boolean}
66+
*/
67+
public boolean contains(T element) {
68+
if (null == element) {
69+
for (int i = 0; i < size; i++) {
70+
if (null == arrayElements[i]) {
71+
return true;
72+
}
73+
}
74+
} else {
75+
for (int i = 0; i < size; i++) {
76+
if (element.equals(arrayElements[i])) {
77+
return true;
78+
}
79+
}
80+
}
81+
return false;
82+
}
83+
84+
/**
85+
* Method to check the size of Set
86+
* @return {@link int}
87+
*/
88+
public int size() {
89+
return size;
90+
}
91+
92+
/**
93+
* Method to check if Set is empty
94+
* @return {@link boolean}
95+
*/
96+
public boolean isEmpty() {
97+
return size() == 0;
98+
}
99+
100+
/**
101+
* Method to clear all the elements in Set
102+
*/
103+
public void clear() {
104+
arrayElements = new Object[DEFAULT_CAPACITY];
105+
size = 0;
106+
}
107+
108+
/**
109+
* toString implementation
110+
*/
111+
public String toString() {
112+
if (null == arrayElements || arrayElements.length == 0) {
113+
return "[EMPTY]";
114+
} else {
115+
String toStr = "[";
116+
for (int i = 0; i < arrayElements.length; i++) {
117+
toStr += arrayElements[i] + "”,";
118+
}
119+
toStr += "”]";
120+
return toStr;
121+
}
122+
}
123+
124+
/**
125+
* Method to increment the size when Set is full
126+
*/
127+
private void incrementArraySize() {
128+
Object[] tempArray = arrayElements;
129+
int tempSize = size * 2;
130+
arrayElements = new Object[tempSize];
131+
System.arraycopy(tempArray, 0, arrayElements, 0, size);
132+
}
133+
134+
}

src/com/deepak/data/structures/Stack/ArrayBasedStack.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
import java.util.NoSuchElementException;
88

9+
/**
10+
* Class implementing Stack based on Array
11+
*/
912
public class ArrayBasedStack {
1013

1114
/**
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* Data-Structures-And-Algorithms-in-Java
3+
* ArrayBasedSetTest.java
4+
*/
5+
package com.deepak.data.structures.Set;
6+
7+
import org.junit.Assert;
8+
import org.junit.Test;
9+
10+
/**
11+
* Test cases for Set
12+
*
13+
* @author Deepak
14+
*/
15+
public class ArrayBasedSetTest {
16+
17+
/**
18+
* Test case to check the insertion
19+
*/
20+
@Test
21+
public void testInsertionInSet() {
22+
ArrayBasedSet<String> set = new ArrayBasedSet<>(5);
23+
Assert.assertTrue(set.size() == 0);
24+
set.addElement("One");
25+
set.addElement("Two");
26+
set.addElement("Three");
27+
Assert.assertTrue(set.size() == 3);
28+
set.addElement("Four");
29+
Assert.assertTrue(set.size() == 4);
30+
/* Trying to insert Three again. Size should not change and operation should fail */
31+
set.addElement("Three");
32+
Assert.assertTrue(set.size() == 4);
33+
}
34+
35+
/**
36+
* Test case to check other operations on Set
37+
*/
38+
@Test
39+
public void testOtherOperationsOnSet() {
40+
ArrayBasedSet<String> set = new ArrayBasedSet<>(5);
41+
Assert.assertTrue(set.isEmpty());
42+
Assert.assertTrue(set.size() == 0);
43+
set.addElement("One");
44+
set.addElement("Two");
45+
set.addElement("Three");
46+
Assert.assertTrue(set.contains("Two"));
47+
Assert.assertTrue(set.size() == 3);
48+
set.clear();
49+
Assert.assertTrue(set.isEmpty());
50+
Assert.assertTrue(set.size() == 0);
51+
}
52+
53+
}

0 commit comments

Comments
 (0)