Skip to content

Commit 930f2c3

Browse files
author
Eugen Paraschiv
committed
further examples
1 parent e491f95 commit 930f2c3

File tree

1 file changed

+71
-2
lines changed

1 file changed

+71
-2
lines changed

guava/src/test/java/org/baeldung/guava/collections/GuavaCollectionsExamplesTest.java

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
package org.baeldung.guava.collections;
22

3+
import static org.junit.Assert.assertNull;
4+
import static org.junit.Assert.assertTrue;
5+
6+
import java.util.Collection;
37
import java.util.List;
8+
import java.util.Set;
49

510
import org.junit.Test;
611

712
import com.google.common.base.Function;
13+
import com.google.common.base.Predicate;
14+
import com.google.common.collect.Iterables;
815
import com.google.common.collect.Lists;
16+
import com.google.common.collect.Sets;
917

1018
public class GuavaCollectionsExamplesTest {
1119

@@ -35,8 +43,69 @@ public final void whenDowncastingGenerifiedCollectionToNewGenerifiedCollection_t
3543
}
3644

3745
@Test
38-
public final void when_then() {
39-
//
46+
public final void whenAddingAnIterableToACollection_thenAddedOK() {
47+
final Iterable<String> iter = Lists.newArrayList();
48+
final Collection<String> collector = Lists.newArrayList();
49+
Iterables.addAll(collector, iter);
50+
}
51+
52+
//
53+
54+
@Test
55+
public final void whenCheckingIfCollectionContainsElementsByCustomMatch1_thenContains() {
56+
final Iterable<String> theCollection = Lists.newArrayList("a", "bc", "def");
57+
final boolean contains = Iterables.any(theCollection, new Predicate<String>() {
58+
@Override
59+
public final boolean apply(final String input) {
60+
return input.length() == 1;
61+
}
62+
});
63+
64+
assertTrue(contains);
65+
}
66+
67+
@Test
68+
public final void whenCheckingIfCollectionContainsElementsByCustomMatch2_thenContains() {
69+
final Set<String> theCollection = Sets.newHashSet("a", "bc", "def");
70+
71+
final boolean contains = !Sets.filter(theCollection, new Predicate<String>() {
72+
@Override
73+
public final boolean apply(final String input) {
74+
return input.length() == 1;
75+
}
76+
}).isEmpty();
77+
78+
assertTrue(contains);
79+
}
80+
81+
@Test
82+
public final void whenCheckingIfCollectionContainsElementsByCustomMatch3_thenContains() {
83+
final Iterable<String> theCollection = Sets.newHashSet("a", "bc", "def");
84+
85+
final boolean contains = Iterables.find(theCollection, new Predicate<String>() {
86+
@Override
87+
public final boolean apply(final String input) {
88+
return input.length() == 1;
89+
}
90+
}) != null;
91+
92+
assertTrue(contains);
93+
}
94+
95+
//
96+
97+
@Test
98+
public final void givenNoSearchResult_whenFindingElementInIterable_thenNoException() {
99+
final Iterable<String> theCollection = Sets.newHashSet("abcd", "efgh", "ijkl");
100+
101+
final String found = Iterables.find(theCollection, new Predicate<String>() {
102+
@Override
103+
public final boolean apply(final String input) {
104+
return input.length() == 1;
105+
}
106+
});
107+
108+
assertNull(found);
40109
}
41110

42111
}

0 commit comments

Comments
 (0)