欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 创投人物 > guava里常用功能

guava里常用功能

2024/10/25 19:29:58 来源:https://blog.csdn.net/jveqi/article/details/142554161  浏览:    关键词:guava里常用功能

guava 是 Google 提供的一个 Java 库,提供了很多实用的工具类和方法,可以帮助开发者更高效地编写代码。以下是一些常用的 Guava 工具类及其功能示例:

1. Lists

用于操作列表的工具类。

import com.google.common.collect.Lists;List<Integer> numbers = Lists.newArrayList(1, 2, 3, 4, 5);
List<List<Integer>> partitions = Lists.partition(numbers, 2);
// partitions: [[1, 2], [3, 4], [5]]

2. Maps

用于操作映射的工具类。

import com.google.common.collect.Maps;Map<String, Integer> map = Maps.newHashMap();
map.put("apple", 1);
map.put("banana", 2);
Map<String, Integer> immutableMap = Maps.immutableEnumMap(map);
// 创建不可变的映射

3. Sets

用于操作集合的工具类。

import com.google.common.collect.Sets;Set<String> set1 = Sets.newHashSet("a", "b", "c");
Set<String> set2 = Sets.newHashSet("b", "c", "d");
Set<String> union = Sets.union(set1, set2);
// union: [a, b, c, d]

4. Optional

用于处理可能为空的值。

import com.google.common.base.Optional;Optional<String> optional = Optional.fromNullable(null);
if (optional.isPresent()) {System.out.println(optional.get());
} else {System.out.println("值为空");
}

5. Strings

字符串操作的工具类。

import com.google.common.base.Strings;String str = "Hello, World!";
String padded = Strings.padStart(str, 20, '*');
// padded: "*******Hello, World!"

6. Joiner

用于连接字符串的工具类。

import com.google.common.base.Joiner;List<String> list = Arrays.asList("a", "b", "c");
String joined = Joiner.on(", ").join(list);
// joined: "a, b, c"

7. Splitter

用于分割字符串的工具类。

import com.google.common.base.Splitter;String csv = "apple, banana, cherry";
Iterable<String> fruits = Splitter.on(", ").split(csv);
// fruits: ["apple", "banana", "cherry"]

8. Preconditions

用于参数检查的工具类。

import com.google.common.base.Preconditions;public void setAge(int age) {Preconditions.checkArgument(age > 0, "年龄必须大于0");// 其他逻辑
}

9. Functional Programming

Guava 还支持函数式编程风格。

import com.google.common.base.Function;
import com.google.common.collect.Lists;List<String> names = Lists.newArrayList("Alice", "Bob", "Charlie");
List<Integer> nameLengths = Lists.transform(names, new Function<String, Integer>() {@Overridepublic Integer apply(String name) {return name.length();}
});
// nameLengths: [5, 3, 7]

10.Immutable:不可变对象工具类

import com.google.common.base.ImmutableList;
import com.google.common.base.ImmutableMap;public class ImmutableExample {public static void main(String[] args) {// 创建一个不可变列表ImmutableList<String> immutableList = ImmutableList.of("apple", "banana", "cherry");// 创建一个不可变映射ImmutableMap<String, String> immutableMap = ImmutableMap.of("key1", "value1","key2", "value2");// 打印不可变对象System.out.println(immutableList);System.out.println(immutableMap);}
}

11.Cache:缓存工具类

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;public class CacheExample {public static void main(String[] args) {// 创建一个缓存,设置缓存的大小为 10 个元素,缓存的过期时间为 10 秒LoadingCache<String, String> cache = CacheBuilder.newBuilder().maximumSize(10).expireAfterWrite(10, TimeUnit.SECONDS).build(new CacheLoader<String, String>() {@Overridepublic String load(String key) throws Exception {// 从数据库或其他数据源中加载数据return "Value for " + key;}});// 向缓存中添加元素cache.put("key1", "value1");cache.put("key2", "value2");// 获取缓存中的元素String value1 = cache.get("key1");String value2 = cache.get("key2");// 打印缓存中的元素System.out.println("Value for key1: " + value1);System.out.println("Value for key2: " + value2);}
}

以上只是 Guava 提供的一部分功能,实际上这个库还有很多其他强大的工具类,能帮助你更高效地处理集合、字符串、并发等问题。你可以根据具体需求查阅 Guava 的官方文档 以获取更多信息。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com