Skip to content

Commit 5f39035

Browse files
committed
Collection Utils
1 parent 10b3ae0 commit 5f39035

File tree

2 files changed

+169
-2
lines changed

2 files changed

+169
-2
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,9 @@ Below topics/problems are covered as of now.
153153
- [X] [StringUtils](../master/src/com/deepak/data/structures/Utils/StringUtils.java)
154154
- [X] [NumberUtils](../master/src/com/deepak/data/structures/Utils/NumberUtils.java)
155155
- [X] [ArrayUtils](../master/src/com/deepak/data/structures/Utils/ArrayUtils.java)
156-
- [ ] ListUtils
157156
- [X] [BooleanUtils](../master/src/com/deepak/data/structures/Utils/BooleanUtils.java)
158157
- [ ] CollectionUtils
159158
- [X] [MapUtils](../master/src/com/deepak/data/structures/Utils/MapUtils.java)
160-
- [ ] DateUtils
161159

162160
**18. Iterators**
163161
- [X] [Standard Iterator](../master/src/com/deepak/data/structures/Iterators/StandardIterator.java)
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/**
2+
* Data-Structures-In-Java
3+
* CollectionUtils.java
4+
*/
5+
package com.deepak.data.structures.Utils;
6+
7+
import java.util.ArrayList;
8+
import java.util.Comparator;
9+
import java.util.List;
10+
import java.util.Random;
11+
12+
/**
13+
* Utilities for collections i.e List
14+
*
15+
* @author Deepak
16+
*/
17+
public class CollectionUtils {
18+
19+
/**
20+
* Method to check if list is empty
21+
*
22+
* @param list
23+
* @return {@link boolean}
24+
*/
25+
public static <T> boolean isEmpty(List<T> list) {
26+
return list == null || list.size() == 0;
27+
}
28+
29+
/**
30+
* Method to sort the list with no comparator
31+
*
32+
* @param list
33+
* @return {@link List<T>}
34+
*/
35+
public static <T> List<T> sort(List<T> list) {
36+
list.sort(null);
37+
return list;
38+
}
39+
40+
/**
41+
* Method to sort the list with custom comparator
42+
*
43+
* @param list
44+
* @param comparator
45+
* @return {@link List<T>}
46+
*/
47+
public static <T> List<T> sort(List<T> list, Comparator<T> comparator) {
48+
list.sort(comparator);
49+
return list;
50+
}
51+
52+
/**
53+
* Method to perform a binary search
54+
*
55+
* @param list
56+
* @param element
57+
* @param comparator
58+
* @return {@link T}
59+
*/
60+
public static <T> T binarySearch(List<T> list, T element, Comparator<T> comparator) {
61+
if (list.isEmpty()) {
62+
return null;
63+
}
64+
int low = 0;
65+
int high = list.size() - 1;
66+
while (high > low) {
67+
int mid = (low + high) / 2;
68+
if (list.get(mid) == element) {
69+
return element;
70+
} else if (comparator.compare(list.get(mid), element) > 0) {
71+
high = mid - 1;
72+
} else {
73+
low = mid + 1;
74+
}
75+
}
76+
return null;
77+
}
78+
79+
/**
80+
* Method to reverse the list
81+
*
82+
* @param list
83+
* @return {@link List<T>}
84+
*/
85+
public static <T> List<T> reverse(List<T> list) {
86+
if (list != null && list.size() > 0) {
87+
List<T> updatedList = new ArrayList<>();
88+
for (int i = list.size() - 1; i >= 0; i--) {
89+
updatedList.add(list.get(i));
90+
}
91+
return updatedList;
92+
}
93+
return null;
94+
}
95+
96+
/**
97+
* Method to shuffle the list
98+
*
99+
* @param list
100+
* @return {@link List<T>}
101+
*/
102+
public static <T> List<T> shuffle(List<T> list) {
103+
if (list != null && list.size() > 0) {
104+
List<T> updatedList = new ArrayList<>();
105+
final int[] ints = new Random().ints(0, list.size()).distinct().limit(list.size()).toArray();
106+
for (int i = 0; i < ints.length; i++) {
107+
updatedList.add(list.get(i));
108+
}
109+
return updatedList;
110+
}
111+
return null;
112+
}
113+
114+
/**
115+
* Method to swap elements at two indexes
116+
*
117+
* @param list
118+
* @param minIndex
119+
* @param maxIndex
120+
* @return {@link List<T>}
121+
*/
122+
public static <T> List<T> swap(List<T> list, int minIndex, int maxIndex) {
123+
if (list != null && list.size() > 2 && maxIndex > minIndex) {
124+
T elementAtMinIndex = list.get(minIndex);
125+
T elementAtMaxIndex = list.get(maxIndex);
126+
list.add(minIndex, elementAtMaxIndex);
127+
list.add(maxIndex, elementAtMinIndex);
128+
return list;
129+
}
130+
return null;
131+
}
132+
133+
/**
134+
* Method to fill the list with the given element
135+
*
136+
* @param list
137+
* @param element
138+
* @return {@link List<T>}
139+
*/
140+
public static <T> List<T> fill(List<T> list, T element) {
141+
if (list != null && list.size() > 0) {
142+
for (int i = 0; i < list.size(); i++) {
143+
list.add(i, element);
144+
}
145+
return list;
146+
}
147+
return null;
148+
}
149+
150+
/**
151+
* Method to find the minimum from the list
152+
*
153+
* @param list
154+
* @param comparator
155+
* @return {@link T}
156+
*/
157+
public static <T> T min(List<T> list, Comparator<T> comparator) {
158+
return null;
159+
}
160+
161+
public static <T> T max(List<T> list, Comparator<T> comparator) {
162+
return null;
163+
}
164+
165+
public static <T> List<T> rotate(List<T> list, int n) {
166+
return null;
167+
}
168+
169+
}

0 commit comments

Comments
 (0)