📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
Utility methods perform common, often reused functions and they don't require to have object-level state, that is they tend to be global functions.
1. isEmpty(Collection<?> collection)
public static boolean isEmpty(Collection<?> collection) {
return (collection == null || collection.isEmpty());
}
2. isNotEmpty(Collection<?> collection)
public static boolean isNotEmpty(Collection<?> collection) {
return !isEmpty(collection);
}
3. isEmptyMap(Map, ?> map)
public static boolean isEmptyMap(Map<?, ?> map) {
return (map == null || map.isEmpty());
}
4. isNotEmptyMap(Map, ?> map)
public static boolean isNotEmptyMap(Map<?, ?> map) {
return !isEmptyMap(map);
}
5. toObjectArray(Object source)
public static Object[] toObjectArray(Object source) {
if (source instanceof Object[]) {
return (Object[]) source;
}
if (source == null) {
return new Object[0];
}
if (!source.getClass().isArray()) {
throw new IllegalArgumentException("Source is not an array: " + source);
}
int length = Array.getLength(source);
if (length == 0) {
return new Object[0];
}
Class<?> wrapperType = Array.get(source, 0).getClass();
Object[] newArray = (Object[]) Array.newInstance(wrapperType, length);
for (int i = 0; i < length; i++) {
newArray[i] = Array.get(source, i);
}
return newArray;
}
6. mergeArrayIntoCollection(Object array, Collection collection)
@SuppressWarnings("unchecked")
public static <E> void mergeArrayIntoCollection(Object array, Collection<E> collection) {
Object[] arr = toObjectArray(array);
for (Object elem : arr) {
collection.add((E) elem);
}
}
7. mergePropertiesIntoMap(Properties props, Map<K, V> map)
public static <K, V> void mergePropertiesIntoMap(Properties props, Map<K, V> map) {
if (props != null) {
for (Enumeration<?> en = props.propertyNames(); en.hasMoreElements();) {
String key = (String) en.nextElement();
Object value = props.get(key);
if (value == null) {
// Allow for defaults fallback or potentially overridden
// accessor...
value = props.getProperty(key);
}
map.put((K) key, (V) value);
}
}
}
8. contains(Iterator<?> iterator, Object element)
public static boolean contains(Iterator<?> iterator, Object element) {
if (iterator != null) {
while (iterator.hasNext()) {
Object candidate = iterator.next();
if (nullSafeEquals(candidate, element)) {
return true;
}
}
}
return false;
}
9. containsInstance(Collection<?> collection, Object element)
public static boolean containsInstance(Collection<?> collection, Object element) {
if (collection != null) {
for (Object candidate : collection) {
if (candidate == element) {
return true;
}
}
}
return false;
}
10. containsAny(Collection> source, Collection> candidates)
public static boolean containsAny(Collection<?> source, Collection<?> candidates) {
if (isEmpty(source) || isEmpty(candidates)) {
return false;
}
for (Object candidate : candidates) {
if (source.contains(candidate)) {
return true;
}
}
return false;
}
11. hasUniqueObject(Collection<?> collection)
public static boolean hasUniqueObject(Collection<?> collection) {
if (isEmpty(collection)) {
return false;
}
boolean hasCandidate = false;
Object candidate = null;
for (Object elem : collection) {
if (!hasCandidate) {
hasCandidate = true;
candidate = elem;
} else if (candidate != elem) {
return false;
}
}
return true;
}
12. lastElement(List list)
public static <T> T lastElement(List<T> list) {
if (isEmpty(list)) {
return null;
}
return list.get(list.size() - 1);
}
13. nullSafeEquals(Object o1, Object o2)
public static boolean nullSafeEquals(Object o1, Object o2) {
if (o1 == o2) {
return true;
}
if (o1 == null || o2 == null) {
return false;
}
if (o1.equals(o2)) {
return true;
}
if (o1.getClass().isArray() && o2.getClass().isArray()) {
return arrayEquals(o1, o2);
}
return false;
}
14. reverseArray(Object[] array)
public static void reverseArray(final Object[] array) {
int i = 0;
int j = array.length - 1;
Object tmp;
while (j > i) {
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
j--;
i++;
}
}
15. size(Collection c) and size(Map m)
public static int size(Collection c) {
return c != null ? c.size() : 0;
}
public static int size(Map m) {
return m != null ? m.size() : 0;
}
16. isSubList(List l1, List<? super T> l)
public static <T> boolean isSubList(List<T> l1, List<? super T> l) {
Iterator<? super T> it = l.iterator();
for (T o1 : l1) {
if (!it.hasNext()) {
return false;
}
Object o = it.next();
while ((o == null && !(o1 == null)) || (o != null && !o.equals(o1))) {
if (!it.hasNext()) {
return false;
}
o = it.next();
}
}
return true;
}
17. diff(Collection list1, Collection list2)
public static <T> Collection<T> diff(Collection<T> list1, Collection<T> list2) {
Collection<T> diff = new ArrayList<>();
for (T t : list1) {
if (!list2.contains(t)) {
diff.add(t);
}
}
return diff;
}
18. getFirstItem(Collection collection, T defaultValue)
public static <T> T getFirstItem(final Collection<T> collection, final T defaultValue) {
if (collection == null) {
return defaultValue;
}
final Iterator<T> iterator = collection.iterator();
return iterator.hasNext() ? iterator.next() : defaultValue;
}
If this article really helps you then let me know in the comment section so that I will prepare similar articles for you.
Check out the top complete collections developers guide on Collections Framework Developer Guide
Please write comments, if you want to give any suggestions or feedback about this article would be appreciated.
Happy Learning and Keep Coding !!!!.
Very productive and useful utility methods. Thanks Ramesh
ReplyDeletefor a long time I am waiting for such a insightful blog. keep up the good work. Thanks Ramesh
ReplyDelete