COLLECTIONS-788 - java8 improvements: (#228)

* Replace Collections.sort() with List.sort()
* Replace lambda with method reference
* Replace Loop with Collection.removeIf()
* Use lambda

Co-authored-by: Gary Gregory <garydgregory@users.noreply.github.com>
This commit is contained in:
Arturo Bernal 2022-03-04 15:07:07 +01:00 committed by GitHub
parent dd69e59fcd
commit 35a6778db8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 16 additions and 35 deletions

View File

@ -213,7 +213,7 @@ public class FluentIterableTest {
@Test
public void forEach() {
final AtomicInteger sum = new AtomicInteger(0);
final Closure<Integer> closure = input -> sum.addAndGet(input);
final Closure<Integer> closure = sum::addAndGet;
FluentIterable.of(iterableA).forEach(closure);
int expectedSum = 0;

View File

@ -167,9 +167,7 @@ public class MapUtilsTest {
@Test
public void testInvertMapNull() {
final Map<String, String> nullMap = null;
final Exception exception = assertThrows(NullPointerException.class, () -> {
MapUtils.invertMap(nullMap);
});
final Exception exception = assertThrows(NullPointerException.class, () -> MapUtils.invertMap(nullMap));
final String actualMessage = exception.getMessage();
assertTrue(actualMessage.contains("map"));
}
@ -1015,30 +1013,22 @@ public class MapUtilsTest {
@Test
public void testUnmodifiableMap() {
final Exception exception = assertThrows(UnsupportedOperationException.class, () -> {
MapUtils.unmodifiableMap(new HashMap<>()).clear();
});
final Exception exception = assertThrows(UnsupportedOperationException.class, () -> MapUtils.unmodifiableMap(new HashMap<>()).clear());
}
@Test
public void testUnmodifiableSortedMap() {
final Exception exception = assertThrows(UnsupportedOperationException.class, () -> {
MapUtils.unmodifiableSortedMap(new TreeMap<>()).clear();
});
final Exception exception = assertThrows(UnsupportedOperationException.class, () -> MapUtils.unmodifiableSortedMap(new TreeMap<>()).clear());
}
@Test
public void testFixedSizeMap() {
final Exception exception = assertThrows(IllegalArgumentException.class, () -> {
MapUtils.fixedSizeMap(new HashMap<>()).put(new Object(), new Object());
});
final Exception exception = assertThrows(IllegalArgumentException.class, () -> MapUtils.fixedSizeMap(new HashMap<>()).put(new Object(), new Object()));
}
@Test
public void testFixedSizeSortedMap() {
final Exception exception = assertThrows(IllegalArgumentException.class, () -> {
MapUtils.fixedSizeSortedMap(new TreeMap<Long, Long>()).put(1L, 1L);
});
final Exception exception = assertThrows(IllegalArgumentException.class, () -> MapUtils.fixedSizeSortedMap(new TreeMap<Long, Long>()).put(1L, 1L));
}
@Test

View File

@ -79,7 +79,7 @@ public class SplitMapUtilsTest {
// check individual operations
int sz = map.size();
attemptPutOperation(() -> map.clear());
attemptPutOperation(map::clear);
assertEquals(sz, map.size());
@ -116,11 +116,11 @@ public class SplitMapUtilsTest {
public void testWritableMap() {
final Map<String, String> map = SplitMapUtils.writableMap(transformedMap);
attemptGetOperation(() -> map.get(null));
attemptGetOperation(() -> map.entrySet());
attemptGetOperation(() -> map.keySet());
attemptGetOperation(() -> map.values());
attemptGetOperation(() -> map.size());
attemptGetOperation(() -> map.isEmpty());
attemptGetOperation(map::entrySet);
attemptGetOperation(map::keySet);
attemptGetOperation(map::values);
attemptGetOperation(map::size);
attemptGetOperation(map::isEmpty);
attemptGetOperation(() -> map.containsKey(null));
attemptGetOperation(() -> map.containsValue(null));
attemptGetOperation(() -> map.remove(null));

View File

@ -963,7 +963,7 @@ public abstract class AbstractCollectionTest<E> extends AbstractObjectTest {
final int size = getCollection().size();
final int targetCount = Collections.frequency(elements, target);
final Predicate<E> filter = e -> target.equals(e);
final Predicate<E> filter = target::equals;
assertTrue("Full collection removeIf should work", getCollection().removeIf(filter));
getConfirmed().removeIf(filter);

View File

@ -700,12 +700,7 @@ public class LRUMapTest<K, V> extends AbstractOrderedMapTest<K, V> {
}
}
synchronized (map) {
for (final Iterator<Map.Entry<Object, Thread>> iter = map.entrySet().iterator(); iter.hasNext();) {
final Map.Entry<Object, Thread> entry = iter.next();
if (entry.getValue() == this) {
iter.remove();
}
}
map.entrySet().removeIf(entry -> entry.getValue() == this);
}
} catch (final InterruptedException e) {
fail("Unexpected InterruptedException");
@ -866,11 +861,7 @@ public class LRUMapTest<K, V> extends AbstractOrderedMapTest<K, V> {
}
}
synchronized (map) {
for (final Iterator<Thread> iter = map.values().iterator(); iter.hasNext();) {
if (iter.next() == this) {
iter.remove();
}
}
map.values().removeIf(thread1 -> thread1 == this);
}
} catch (final InterruptedException e) {
fail("Unexpected InterruptedException");

View File

@ -261,7 +261,7 @@ public class ReferenceMapTest<K, V> extends AbstractIterableMapTest<K, V> {
public void testCustomPurge() {
final List<Integer> expiredValues = new ArrayList<>();
@SuppressWarnings("unchecked")
final Consumer<Integer> consumer = (Consumer<Integer> & Serializable) v -> expiredValues.add(v);
final Consumer<Integer> consumer = (Consumer<Integer> & Serializable) expiredValues::add;
final Map<Integer, Integer> map = new ReferenceMap<Integer, Integer>(ReferenceStrength.WEAK, ReferenceStrength.HARD, false) {
private static final long serialVersionUID = 1L;