Skip to content

Commit 64c2ef1

Browse files
daveRanjanpivovarit
authored andcommitted
Guava 21 (eugenp#1411)
Code for article Guava 21 | common.collect package
1 parent ed70f6b commit 64c2ef1

12 files changed

Lines changed: 516 additions & 0 deletions

‎guava21/README.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=========
2+
3+
## Guava 21
4+
5+
**Important**: Guava 21.0 requires Java 8. If you need Java 6, 7 or Android compatibility, use Guava 20.0 for now. Guava 22.0 and on will introduce a Java 6/Android compatible backport of Guava that includes all of the latest changes that don't require Java 8.
6+
7+
Article 1 : Introduction to Guava21 common.collect package.

‎guava21/pom.xml‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.baeldung.guava</groupId>
8+
<artifactId>tutorial</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<build>
11+
<plugins>
12+
<plugin>
13+
<groupId>org.apache.maven.plugins</groupId>
14+
<artifactId>maven-compiler-plugin</artifactId>
15+
<configuration>
16+
<source>1.8</source>
17+
<target>1.8</target>
18+
</configuration>
19+
</plugin>
20+
</plugins>
21+
</build>
22+
23+
<dependencies>
24+
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
25+
<dependency>
26+
<groupId>com.google.guava</groupId>
27+
<artifactId>guava</artifactId>
28+
<version>21.0</version>
29+
</dependency>
30+
31+
<!-- https://mvnrepository.com/artifact/junit/junit -->
32+
<dependency>
33+
<groupId>junit</groupId>
34+
<artifactId>junit</artifactId>
35+
<version>4.11</version>
36+
</dependency>
37+
38+
</dependencies>
39+
40+
41+
</project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.guava.tutorial;
2+
3+
import com.google.common.collect.Comparators;
4+
5+
import java.util.Arrays;
6+
import java.util.Comparator;
7+
import java.util.List;
8+
9+
public class ComparatorsExamples {
10+
11+
public static void main(String[] args){
12+
13+
List<Integer> integers = Arrays.asList(1,2,3,4,4,6,7,8,9,10);
14+
//This will return true
15+
boolean isInAscendingOrder = Comparators.isInOrder(integers, new AscedingOrderComparator());
16+
17+
System.out.println(isInAscendingOrder);
18+
19+
}
20+
21+
private static class AscedingOrderComparator implements Comparator<Integer> {
22+
@Override
23+
public int compare(Integer o1, Integer o2) {
24+
return o1.compareTo(o2);
25+
}
26+
}
27+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.guava.tutorial;
2+
3+
import com.google.common.collect.Streams;
4+
5+
import java.util.stream.Stream;
6+
7+
public class ConcatStreams {
8+
public static Stream concatStreams(Stream stream1, Stream stream2, Stream stream3){
9+
return Streams.concat(stream1,stream2,stream3);
10+
}
11+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.guava.tutorial;
2+
3+
import com.google.common.collect.Interner;
4+
import com.google.common.collect.Interners;
5+
6+
import static com.google.common.collect.Interners.newBuilder;
7+
8+
public class InternerBuilderExample {
9+
10+
public static void main(String[] args){
11+
Interner<Integer> interners = Interners.<Integer>newBuilder()
12+
.concurrencyLevel(2)
13+
.strong()
14+
.<Integer>build();
15+
16+
17+
}
18+
19+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.guava.tutorial;
2+
3+
import com.google.common.collect.MoreCollectors;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
import java.util.Optional;
8+
9+
public class MoreCollectorsExample {
10+
11+
public static void main(String[] args) {
12+
List<Integer> numbers = Arrays.asList(1);
13+
Optional<Integer> number = numbers.stream()
14+
.map(e -> e * 2)
15+
.collect(MoreCollectors.toOptional());
16+
}
17+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.guava.tutorial;
2+
3+
import com.google.common.collect.Streams;
4+
5+
import java.util.*;
6+
import java.util.stream.DoubleStream;
7+
import java.util.stream.IntStream;
8+
import java.util.stream.LongStream;
9+
import java.util.stream.Stream;
10+
11+
public class StreamsUtility {
12+
13+
public static void main(String[] args){
14+
15+
List<Integer> numbers = Arrays.asList(1,2,3,4,5,6,7,8,9,10,11,11,12,13,14,15,16,17,18,19,20);
16+
//Using Collection
17+
Stream<Integer> streamFromCollection = Streams.stream(numbers);
18+
//Using Iterator
19+
Stream<Integer> streamFromIterator = Streams.stream(numbers.iterator());
20+
//Using Iterable
21+
Stream<Integer> streamFromIterable = Streams.stream((Iterable<Integer>) numbers);
22+
//Using Optional
23+
Stream<Integer> streamFromOptional = Streams.stream(Optional.of(1));
24+
//Using OptionalLong to LongStream
25+
LongStream streamFromOptionalLong = Streams.stream(OptionalLong.of(1));
26+
//Using OptionalInt to IntStream
27+
IntStream streamFromOptionalInt = Streams.stream(OptionalInt.of(1));
28+
//Using OptionalDouble to DoubleStream
29+
DoubleStream streamFromOptionalDouble = Streams.stream(OptionalDouble.of(1.0));
30+
31+
Stream<Integer> concatenatedStreams = Streams.concat(streamFromCollection,streamFromIterable,streamFromIterator);
32+
33+
List<Integer> integers = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
34+
//This will return 10
35+
Optional<Integer> lastItem = Streams.findLast(integers.stream());
36+
37+
Streams.zip(
38+
Stream.of("candy", "chocolate", "bar"),
39+
Stream.of("$1", "$2","$3"),
40+
(arg1, arg2) -> arg1 + ":" + arg2);
41+
}
42+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import com.google.common.collect.Comparators;
2+
import org.junit.Assert;
3+
import org.junit.Test;
4+
5+
import java.util.*;
6+
import java.util.function.Function;
7+
import java.util.function.ToDoubleFunction;
8+
import java.util.function.ToIntFunction;
9+
import java.util.function.ToLongFunction;
10+
11+
public class ComparatorsUnitTests {
12+
13+
@Test
14+
public void isInOrderTest(){
15+
16+
List<Integer> numbers = Arrays.asList(1,2,3,4,4,6,7,8,9,10);
17+
18+
boolean isInAscendingOrder = Comparators.isInOrder(numbers, new AscendingOrderComparator<Number>());
19+
20+
Assert.assertTrue(isInAscendingOrder);
21+
}
22+
23+
@Test
24+
public void isInStrictOrderTest(){
25+
26+
List<Integer> numbers = Arrays.asList(1,2,3,4,3,6,7,8,9,10);
27+
28+
boolean isInAscendingOrder = Comparators.isInOrder(numbers, new AscendingOrderComparator<Number>());
29+
30+
Assert.assertFalse(isInAscendingOrder);
31+
}
32+
33+
34+
private class AscendingOrderComparator<I extends Number> implements Comparator<Integer>{
35+
36+
@Override
37+
public int compare(Integer o1, Integer o2) {
38+
return o1.compareTo(o2);
39+
}
40+
41+
@Override
42+
public Comparator<Integer> reversed() {
43+
return null;
44+
}
45+
46+
@Override
47+
public Comparator<Integer> thenComparing(Comparator<? super Integer> other) {
48+
return null;
49+
}
50+
51+
@Override
52+
public <U> Comparator<Integer> thenComparing(Function<? super Integer, ? extends U> keyExtractor, Comparator<? super U> keyComparator) {
53+
return null;
54+
}
55+
56+
@Override
57+
public <U extends Comparable<? super U>> Comparator<Integer> thenComparing(Function<? super Integer, ? extends U> keyExtractor) {
58+
return null;
59+
}
60+
61+
@Override
62+
public Comparator<Integer> thenComparingInt(ToIntFunction<? super Integer> keyExtractor) {
63+
return null;
64+
}
65+
66+
@Override
67+
public Comparator<Integer> thenComparingLong(ToLongFunction<? super Integer> keyExtractor) {
68+
return null;
69+
}
70+
71+
@Override
72+
public Comparator<Integer> thenComparingDouble(ToDoubleFunction<? super Integer> keyExtractor) {
73+
return null;
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)