Guava常用方法

目录

一、数学和数值操作

二、并发库

三、缓存

 四、集合

 五、I/O 与文件操作

 六、网络

 七、时间处理

 八、事件总线

九、反射

 十、范围和集合操作

 十一、随机数和测试

 十二、注解处理

 十三、比较器和排序

 十四、哈希和散列


  • Guava 是 Google 开源的一个 Java 工具库,它提供了很多 Java 标准库中没有的便利工具和功能。上面的代码示例已经涵盖了 Guava 的一些核心功能,但 Guava 库的用途远不止这些。以下是一些其他的 Guava 特性和组件,可以作为补充:

一、数学和数值操作


  • IntsLongsDoubles 等类提供了对基本数值类型的操作,如比较器、排序、最小最大值等。
// 代码如下
import com.google.common.primitives.Ints;

// 使用 Ints 类获取最小值
int min = Ints.min(1, 3, 2);
System.out.println("Min value: " + min); // 输出: 1

二、并发库


  • AbstractFuture 和 ListenableFuture 等类提供了对异步编程的支持。
  • RateLimiter 类用于控制对资源的访问速率。
// 代码如下
import com.google.common.util.concurrent.RateLimiter;

// 创建一个速率限制器
RateLimiter rateLimiter = RateLimiter.create(1.0); // 每秒一个操作
rateLimiter.acquire(); // 请求一个操作

三、缓存


  • CacheBuilder 不仅可以创建简单的缓存,还可以实现复杂的缓存策略,如自动过期、软引用和弱引用缓存等。
// 代码如下
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;

LoadingCache<String, String> cache = CacheBuilder.newBuilder()
    .maximumSize(100)
    .build(CacheLoader.from(key -> {
        // 模拟数据库查询
        return "Value for " + key;
    }));

String value = cache.get("key1"); // 从缓存获取,或者执行加载逻辑

 四、集合


  • MultisetMultimapBiMap 等类提供了对集合的扩展,支持多值映射和双向映射。
  • Table 类提供了一种存储和操作二维数据的方式。
// 代码如下
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;

// 创建双向映射
BiMap<String, Integer> map = HashBiMap.create();
map.put("one", 1);
map.put("two", 2);

Integer value = map.get("one"); // 获取值
String key = map.inverse().get(2); // 通过值获取键

 五、I/O 与文件操作


  • Files 类提供了文件读写、文件属性获取等操作。
  • ByteSourceCharSource 等类提供了对字节和字符流的操作。
// 代码如下
import com.google.common.io.Resources;

// 读取网络资源
String content = Resources.toString(
    Resources.getResource("http://example.com/file.txt"),
    Charsets.UTF_8
);

 六、网络

  • HttpTransport 和 UrlFetch 类提供了对 HTTP 请求的支持。
// 代码如下
import com.google.common.net.HttpTransportFactory;

// 使用 UrlFetch 进行 HTTP 请求
String url = "http://example.com";
String response = HttpTransportFactory.create().fetch(url).execute().parseAsString();

 七、时间处理


  • Stopwatch 类用于测量代码段的执行时间。
  • TimeLimiter 类可以对操作设置时间限制。
// 代码如下
import com.google.common.base.Stopwatch;

// 使用 Stopwatch 测量时间
Stopwatch stopwatch = Stopwatch.createStarted();
// 执行一些操作...
stopwatch.stop();
System.out.println("Elapsed time: " + stopwatch.elapsed());

 八、事件总线


  • EventBus 类支持发布-订阅模式的事件处理。
// 代码如下
import com.google.common.eventbus.EventBus;

// 创建事件总线并发布事件
EventBus eventBus = new EventBus();
eventBus.post("Event message");

九、反射

  • ClassPath 类用于扫描类路径下的类和资源。
// 代码如下
import com.google.common.reflect.ClassPath;

// 扫描类路径下的类
ClassPath classPath = ClassPath.from(getClassLoader());
ImmutableSet<ClassInfo> allClasses = classPath.getAllClasses();

 十、范围和集合操作

  • Range 类用于表示和操作有序值的范围。
  • Iterables 和 Collections2 类提供了一系列静态方法来增强集合的操作。

// 代码如下
import com.google.common.collect.Range;

// 使用 Range 表示范围
Range<Integer> range = Range.closed(1, 5);
boolean contains = range.contains(3); // 检查是否包含某个值

 十一、随机数和测试

  • Randoms 类提供生成随机数的方法。
  • MoreAsserts 类提供额外的断言方法,用于测试。

// 代码如下
import com.google.common.reflect.TypeToken;
import java.lang.reflect.Type;

// 使用 TypeToken 处理泛型类型
Type type = new TypeToken<List<String>>(){}.getType();

 十二、注解处理

  • Annotations 类提供对注解的操作。

// 代码如下
import com.google.common.reflect.TypeToken;
import java.lang.reflect.Type;

// 使用 TypeToken 处理泛型类型
Type type = new TypeToken<List<String>>(){}.getType();

 十三、比较器和排序

  • Ordering 类提供灵活的比较器和排序功能。

// 代码如下
import com.google.common.collect.Ordering;

// 使用 Ordering 进行排序
List<String> list = Arrays.asList("Alice", "Bob", "Charlie");
Ordering<String> naturalOrder = Ordering.natural();
List<String> sortedList = naturalOrder.sortedCopy(list);

 十四、哈希和散列

  • Hash 类提供哈希相关的操作。

// 代码如下
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;

// 使用 HashFunction 进行哈希计算
HashFunction hashFunction = Hashing.murmur3_32();
String string = "hello";
HashCode hashCode = hashFunction.hashString(string, Charsets.UTF_8);

 

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值