-
Notifications
You must be signed in to change notification settings - Fork 0
Collection Helper
This page consists of brief explanation and sample usages of the methods in CollectionHelper. You can see more details on each method by clicking on each hyperlinked method name.
addToListfindInCollectionforEachisEmpty(Collection)isEmpty(Map)isInCollectionisInEnumSetisNotEmpty(Collection)isNotEmpty(Map)sanitize(Collection)sanitize(Map)sizeOftoListtoSet
Adds the given element(s) to the given list. Initiates a new ArrayList if the list is null.
List<String> validOrderIds = null;
for (Order validOrder : validOrders) {
validOrderIds.add(validOrder.getOrderId());
}
vs
List<String> validOrderIds = null;
for (Order validOrder : validOrders) {
addToList(validOrderIds, validOrder.getOrderId());
}
Finds the first element in the given collection that satisfies the given predicate.
for (Order order : orders) {
if (someOrderId.equals(order.getOrderId())) {
order.setExpired(true);
}
}
vs
Order order = findInCollection(orders, e - someOrderId.equals(e.getOrderId()));
doIfNotNull(order, e -> e.setExpired(true);
Executes the given function on the given collection.
if (orders != null) {
orders.forEach(order -> {
order.setExpired(true);
order.setTotalAmount(order.getQuantity() * order.getAmount());
});
}
vs
forEach(orders, order -> {
order.setExpired(true);
order.setTotalAmount(order.getQuantity() * order.getAmount());
});
});
isEmpty (Collection)
Checks whether the given collection is empty.
if (order.getOrderItems() == null || order.getOrderItems().isEmpty()) {
...
}
vs
if (isEmpty(order.getOrderItems()) {
...
}
isEmpty (Map)
if (someMap == null || someMap.isEmpty()) {
...
}
vs
if (isEmpty(someMap)) {
...
}
Determines whether the specified element is in the given collection. Comparison is done by invoking .equals() on the element and all the elements inside the collection.
SomeClass targetObj;
for (SomeClass sc : someCollection) {
if (sc.equals(targetObj)) {
return true;
}
}
vs
SomeClass targetObj;
return isInCollection(targetObj, someCollection);
Determines whether the specified enum is in the given EnumSet. Comparison is done by invoking .equals() on the enum and all the elements inside the EnumSet.
EnumSet.of(OrderType.RETAIL, OrderType.FLIGHT).contains(OrderType.FLIGHT);
vs
isInEnumSet(OrderType.FLIGHT, OrderType.RETAIL, OrderType.FLIGHT);
isNotEmpty (Collection)
Negation of isEmpty
isNotEmpty (Map)
Negation of isEmpty
sanitize (Collection)
Removes null elements from the collection. Given a list
List<String> list = new ArrayList<>();
list.add("someString1");
list.add(null);
list.add("someString2");
Removing null elements from the list would look like this:
if (isNotEmpty(list) {
list.removeIf(Objects:isNull);
}
We can use sanitize instead to make it more readable:
sanitize(list);
sanitize (Map)
Removes elements with either null key or null value. Given a map:
Map<String, String> map = new HashMap<>();
map.put(null, "someValue1");
map.put("someKey2", "someValue2");
map.put("someKey3", null);
Doing the said operation would look like this:
if (map != null) {
Iterator<Entry<String, String>> itr = map.entrySet().iterator();
while (itr.hasNext()) {
Entry<String, String> e = itr.next();
if (e.getKey() == null || e.getValue() == null) {
itr.remove();
}
}
}
We can use sanitize instead:
sanitize(map);
Simply returns the size of the collection.
int listSize = 0;
if (list != null) {
listSize = list.size();
}
vs
int listSize = sizeOf(list);
Creates a new ArrayList and populates it with the specified elements.
List<String> validIds = new ArrayList<>();
validIds.add("id1");
validIds.add("id2");
validIds.add(null); // validIds has 3 elements (including 1 null element)
vs
List<String> validIds = toList("id1", "id2", null); // validIds has 2 elements
Creates a new HashSet and populates it with the specified elements.
Set<String> validIds = new HashSet<>();
validIds.add("id1");
validIds.add("id2");
validIds.add(null); // validIds has 3 elements (including 1 null element)
vs
Set<String> validIds = toSet("id1", "id2", null); //validIds now has 2 elements