-
Notifications
You must be signed in to change notification settings - Fork 0
/
Helpers.java
43 lines (34 loc) · 1.03 KB
/
Helpers.java
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
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ThreadLocalRandom;
public class Helpers {
// Sets
// Removing duplicates
public static <T> List removeDuplicateStrings(List<T> a) {
return new ArrayList(new LinkedHashSet(a));
}
// Strings
// getStringFromStringIndex("Abcd", 1) -> "b"
public static String getOneLetterFromString(String str1, int index) {
return String.valueOf(str1.charAt(index));
}
// Integers
// getIntegerElement(518, 2) -> 8
private static int getIntegerElement(int intNumber, int index) {
return Character.getNumericValue(Integer.toString(intNumber).charAt(index));
}
// randomNum(1, 10) -> 5
public static int randomNum(int min, int max) {
return ThreadLocalRandom.current().nextInt(min, max + 1);
}
// stringToInt("11").orElse(0)
public static Optional<Integer> stringToInt(String s) {
try {
return Optional.of(Integer.parseInt(s));
} catch (NumberFormatException e) {
return Optional.empty();
}
}
}