Comprehensive Java library providing essential utilities, immutable collections, caching, and concurrency tools for modern Java development.
npx @tessl/cli install tessl/maven-com-google-guava--guava@33.4.0Google Guava is a comprehensive Java library that provides essential utilities and data structures for modern Java development. It includes immutable collections, caching, concurrency utilities, I/O helpers, string manipulation, and much more.
Name: com.google.guava:guava
Type: Library
Language: Java
Version: 33.4.8
Add this Maven dependency:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>33.4.8-jre</version>
</dependency>// Basic utilities
import com.google.common.base.*;
import com.google.common.collect.*;
// Specialized functionality
import com.google.common.cache.*;
import com.google.common.util.concurrent.*;
import com.google.common.io.*;
import com.google.common.hash.*;
import com.google.common.math.*;import com.google.common.base.Strings;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
// Null-safe string operations
String safe = Strings.nullToEmpty(null); // ""
boolean empty = Strings.isNullOrEmpty(""); // true
// Joining strings
String result = Joiner.on(", ").skipNulls().join("a", null, "b"); // "a, b"
// Splitting strings
List<String> parts = Splitter.on(',').trimResults().splitToList("a, b , c");import com.google.common.collect.Lists;
import com.google.common.collect.ImmutableList;
// Creating lists
List<String> list = Lists.newArrayList("a", "b", "c");
// Immutable collections
ImmutableList<String> immutable = ImmutableList.of("a", "b", "c");
ImmutableList<String> copy = ImmutableList.copyOf(existingList);import com.google.common.base.Preconditions;
// Method precondition checking
public void setAge(int age) {
Preconditions.checkArgument(age >= 0, "Age must be non-negative: %s", age);
Preconditions.checkNotNull(name, "Name cannot be null");
this.age = age;
}Guava is organized into several key capability areas:
Essential utilities for common programming tasks including annotations, string manipulation, preconditions, and object helpers.
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.base.Objects;
// Precondition checking
Preconditions.checkArgument(count >= 0);
Preconditions.checkNotNull(value);
// String utilities
String padded = Strings.padStart("42", 5, '0'); // "00042"
String common = Strings.commonPrefix("foobar", "foobaz"); // "fooba"
// Object utilities
boolean equal = Objects.equal(a, b); // null-safe
int hash = Objects.hashCode(field1, field2, field3);→ Basic Utilities - Complete utilities for annotations, strings, validation, objects, and functional programming
Enhanced collection types and utilities including multimaps, multisets, and powerful collection transformations.
import com.google.common.collect.*;
// Multimap - key to multiple values
ListMultimap<String, String> multimap = ArrayListMultimap.create();
multimap.put("fruits", "apple");
multimap.put("fruits", "banana");
// Multiset - collection with duplicates and counts
Multiset<String> counts = HashMultiset.create();
counts.add("apple", 3);
int appleCount = counts.count("apple"); // 3
// BiMap - bidirectional map
BiMap<String, Integer> bimap = HashBiMap.create();
bimap.put("one", 1);
Integer value = bimap.get("one"); // 1
String key = bimap.inverse().get(1); // "one"→ Collections - Comprehensive collection utilities, multimaps, multisets, and advanced data structures
Thread-safe, high-performance immutable data structures that can be safely shared and cached.
import com.google.common.collect.*;
// Immutable list
ImmutableList<String> list = ImmutableList.of("a", "b", "c");
ImmutableList<String> built = ImmutableList.<String>builder()
.add("x")
.addAll(otherList)
.build();
// Immutable map
ImmutableMap<String, Integer> map = ImmutableMap.of(
"one", 1,
"two", 2,
"three", 3
);
// Immutable set with natural ordering
ImmutableSortedSet<String> sorted = ImmutableSortedSet.of("c", "a", "b");→ Immutable Collections - Complete guide to immutable lists, sets, maps, and sorted collections
Flexible in-memory caching with automatic loading, expiration, and comprehensive statistics.
import com.google.common.cache.*;
import java.util.concurrent.TimeUnit;
// Cache with automatic loading
LoadingCache<String, String> cache = CacheBuilder.newBuilder()
.maximumSize(1000)
.expireAfterWrite(10, TimeUnit.MINUTES)
.build(new CacheLoader<String, String>() {
public String load(String key) {
return loadFromDatabase(key);
}
});
String value = cache.get("key"); // Loads automatically if absent
cache.invalidate("key"); // Remove specific entry→ Caching - Complete caching solution with loading, expiration, and eviction strategies
Enhanced concurrency utilities including listenable futures, rate limiting, and improved executors.
import com.google.common.util.concurrent.*;
import java.util.concurrent.Executors;
// Listenable futures
ListeningExecutorService executor = MoreExecutors.listeningDecorator(
Executors.newFixedThreadPool(10));
ListenableFuture<String> future = executor.submit(callable);
Futures.addCallback(future, new FutureCallback<String>() {
public void onSuccess(String result) { /* handle success */ }
public void onFailure(Throwable t) { /* handle failure */ }
}, MoreExecutors.directExecutor());
// Rate limiting
RateLimiter rateLimiter = RateLimiter.create(5.0); // 5 permits per second
rateLimiter.acquire(); // Blocks until permit available→ Concurrency - Advanced concurrency with ListenableFuture, rate limiting, and services
Comprehensive I/O utilities for streams, files, and resource management with proper exception handling.
import com.google.common.io.*;
import java.io.File;
import java.nio.charset.StandardCharsets;
// File operations
List<String> lines = Files.readLines(file, StandardCharsets.UTF_8);
Files.write("Hello World".getBytes(), file);
// Resource operations
URL resource = Resources.getResource("config.properties");
String content = Resources.toString(resource, StandardCharsets.UTF_8);
// Stream copying
ByteStreams.copy(inputStream, outputStream);
CharStreams.copy(reader, writer);→ I/O Utilities - File operations, resource handling, and stream processing utilities
Powerful graph data structures and algorithms for modeling complex relationships.
import com.google.common.graph.*;
// Mutable graph
MutableGraph<String> graph = GraphBuilder.undirected().build();
graph.addNode("A");
graph.addNode("B");
graph.putEdge("A", "B");
// Graph queries
Set<String> neighbors = graph.adjacentNodes("A");
boolean connected = graph.hasEdgeConnecting("A", "B");
int degree = graph.degree("A");→ Graph API - Graph data structures, algorithms, and network modeling
Hash functions and mathematical operations with overflow checking and precision handling.
import com.google.common.hash.*;
import com.google.common.math.IntMath;
import java.math.RoundingMode;
// Hashing
HashFunction hasher = Hashing.sha256();
HashCode hash = hasher.hashString("input", StandardCharsets.UTF_8);
// Safe math operations
int sum = IntMath.checkedAdd(a, b); // Throws on overflow
int sqrt = IntMath.sqrt(x, RoundingMode.HALF_UP);
int gcd = IntMath.gcd(a, b);→ Hash and Math - Hash functions, mathematical utilities, and statistical operations
Additional utilities including event bus, escaping, networking, reflection, and primitive operations.
import com.google.common.eventbus.EventBus;
import com.google.common.net.HostAndPort;
import com.google.common.primitives.Ints;
// Event bus
EventBus eventBus = new EventBus();
eventBus.register(listenerObject);
eventBus.post(new SomeEvent());
// Network utilities
HostAndPort hostPort = HostAndPort.fromString("example.com:8080");
String host = hostPort.getHost(); // "example.com"
int port = hostPort.getPort(); // 8080
// Primitive utilities
List<Integer> intList = Ints.asList(1, 2, 3, 4, 5);
int[] array = Ints.toArray(intList);→ Other Utilities - Event bus, networking, escaping, reflection, and primitive utilities