Skip to content

Commit f5ec12d

Browse files
Fixed Multiple If-else statements in BloomFilter.java using reflection api + moved ComplexNumberMultiplication.java to maths package along with its tests
1 parent c495f3d commit f5ec12d

File tree

3 files changed

+19
-21
lines changed

3 files changed

+19
-21
lines changed

src/main/java/com/thealgorithms/datastructures/bloomfilter/BloomFilter.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.thealgorithms.datastructures.bloomfilter;
22

3+
import java.lang.reflect.Array;
34
import java.util.Arrays;
45
import java.util.BitSet;
56

@@ -117,24 +118,22 @@ private static class Hash<T> {
117118
*/
118119
public int compute(T key) {
119120
String keyString;
120-
if (key instanceof byte[]) {
121-
keyString = Arrays.toString((byte[]) key);
122-
} else if (key instanceof short[]) {
123-
keyString = Arrays.toString((short[]) key);
124-
} else if (key instanceof int[]) {
125-
keyString = Arrays.toString((int[]) key);
126-
} else if (key instanceof long[]) {
127-
keyString = Arrays.toString((long[]) key);
128-
} else if (key instanceof char[]) {
129-
keyString = Arrays.toString((char[]) key);
130-
} else if (key instanceof float[]) {
131-
keyString = Arrays.toString((float[]) key);
132-
} else if (key instanceof double[]) {
133-
keyString = Arrays.toString((double[]) key);
134-
} else if (key instanceof boolean[]) {
135-
keyString = Arrays.toString((boolean[]) key);
136-
} else if (key instanceof Object[]) {
137-
keyString = Arrays.deepToString((Object[]) key);
121+
if (key != null && key.getClass().isArray()) {
122+
if (key instanceof Object[] objects) {
123+
keyString = Arrays.deepToString(objects);
124+
}
125+
else {
126+
int length = Array.getLength(key);
127+
StringBuilder sb = new StringBuilder("[");
128+
for (int i = 0; i < length; i++) {
129+
sb.append(Array.get(key, i));
130+
if (i < length - 1) {
131+
sb.append(", ");
132+
}
133+
}
134+
sb.append("]");
135+
keyString = sb.toString();
136+
}
138137
} else {
139138
keyString = String.valueOf(key);
140139
}

src/main/java/com/thealgorithms/strings/ComplexNumberMultiplication.java renamed to src/main/java/com/thealgorithms/maths/ComplexNumberMultiplication.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.thealgorithms.strings;
1+
package com.thealgorithms.maths;
22
/**
33
* @author Swarit Srivastava (https://github.com/SwarritSrivastava)
44
*/

src/test/java/com/thealgorithms/strings/ComplexNumberMultiplicationTest.java renamed to src/test/java/com/thealgorithms/maths/ComplexNumberMultiplicationTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
package com.thealgorithms.strings;
1+
package com.thealgorithms.maths;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4-
54
import org.junit.jupiter.api.Test;
65

76
class ComplexNumberMultiplicationTest {

0 commit comments

Comments
 (0)