-
Notifications
You must be signed in to change notification settings - Fork 65
/
CollectionUtils.java
160 lines (149 loc) · 3.44 KB
/
CollectionUtils.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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* Data-Structures-In-Java
* CollectionUtils.java
*/
package com.deepak.data.structures.Utils;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Random;
/**
* Utilities for collections i.e List
* Note : Finding min, max and rotate can also be added here
*
* @author Deepak
*/
public class CollectionUtils {
/**
* Method to check if list is empty
*
* @param list
* @return {@link boolean}
*/
public static <T> boolean isEmpty(List<T> list) {
return list == null || list.size() == 0;
}
/**
* Method to sort the list with no comparator
*
* @param list
* @return {@link List<T>}
*/
public static <T> List<T> sort(List<T> list) {
list.sort(null);
return list;
}
/**
* Method to sort the list with custom comparator
*
* @param list
* @param comparator
* @return {@link List<T>}
*/
public static <T> List<T> sort(List<T> list, Comparator<T> comparator) {
list.sort(comparator);
return list;
}
/**
* Method to perform a binary search
*
* @param list
* @param element
* @param comparator
* @return {@link T}
*/
public static <T> T binarySearch(List<T> list, T element, Comparator<T> comparator) {
if (list.isEmpty()) {
return null;
}
int low = 0;
int high = list.size() - 1;
while (high > low) {
int mid = (low + high) / 2;
if (list.get(mid) == element) {
return element;
} else if (comparator.compare(list.get(mid), element) > 0) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return null;
}
/**
* Method to reverse the list
*
* @param list
* @return {@link List<T>}
*/
public static <T> List<T> reverse(List<T> list) {
if (list != null && list.size() > 0) {
List<T> updatedList = new ArrayList<>();
for (int i = list.size() - 1; i >= 0; i--) {
updatedList.add(list.get(i));
}
return updatedList;
}
return null;
}
/**
* Method to shuffle the list
*
* @param list
* @return {@link List<T>}
*/
public static <T> List<T> shuffle(List<T> list) {
if (list != null && list.size() > 0) {
List<T> updatedList = new ArrayList<>();
final int[] ints = new Random().ints(0, list.size()).distinct().limit(list.size()).toArray();
for (int i = 0; i < ints.length; i++) {
updatedList.add(list.get(i));
}
return updatedList;
}
return null;
}
/**
* Method to swap elements at two indexes
*
* @param list
* @param minIndex
* @param maxIndex
* @return {@link List<T>}
*/
public static <T> List<T> swap(List<T> list, int minIndex, int maxIndex) {
if (list != null && list.size() > 2 && maxIndex > minIndex) {
List<T> updatedList = new ArrayList<>();
T elementAtMinIndex = list.get(minIndex);
T elementAtMaxIndex = list.get(maxIndex);
for (int i = 0; i < list.size(); i++) {
if (i != minIndex && i != maxIndex) {
updatedList.add(i, list.get(i));
} else if (i == minIndex) {
updatedList.add(i, elementAtMaxIndex);
} else if (i == maxIndex) {
updatedList.add(i, elementAtMinIndex);
}
}
return updatedList;
}
return null;
}
/**
* Method to fill the list with the given element
*
* @param list
* @param element
* @return {@link List<T>}
*/
public static <T> List<T> fill(List<T> list, T element) {
if (list != null && list.size() > 0) {
List<T> updatedList = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
updatedList.add(element);
}
return updatedList;
}
return null;
}
}