当前位置: 首页 > news >正文

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 的官方文档 以获取更多信息。

http://www.lryc.cn/news/447317.html

相关文章:

  • su 命令:一键切换用户身份、提高su命令安全性的建议
  • 观察者模式(发布-订阅模式)
  • 耦合微带线单元的网络参量和等效电路公式推导
  • elasticsearch的Ingest Attachment插件的使用总结
  • SemiDrive E3 MCAL 开发系列(4) – Gpt 模块的使用
  • 前端导出页面PDF
  • Jenkins的安装
  • 初学51单片机之I2C总线与E2PROM
  • C语言数组探秘:数据操控的艺术【下】
  • Jmeter关联,断言,参数化
  • 嵌入式单片机底层原理详解
  • 重修设计模式-行为型-责任链模式
  • Vercel部署/前端部署
  • 常见的css预处理器
  • mysql—半同步模式
  • You are not allowed to push code to this project
  • Java刷题:最小k个数
  • Redis实战--Redis应用过程中出现的热门问题及其解决方案
  • 实时数字人DH_live使用案例
  • 线上环境排故思路与方法GC优化策略
  • 硬件设计很简单?合宙低功耗4G模组Air780E—开机启动及外围电路设计
  • 初试AngularJS前端框架
  • 【学习笔记】手写 Tomcat 六
  • 打靶记录18——narak
  • LabVIEW编程能力如何能突飞猛进
  • 代码随想录算法训练营第四四天| 1143.最长公共子序列 1035.不相交的线 53. 最大子序和 392.判断子序列
  • 2024.9.26 作业 +思维导图
  • WSL进阶体验:gnome-terminal启动指南与中文显示问题一网打尽
  • recoil和redux之间的选择
  • 无人机的作战指挥中心-地面站!