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

dede无法更新网站主页到辽宁模板网站建设公司

dede无法更新网站主页到,辽宁模板网站建设公司,公司网站开发的国内外研究现状,网站帮助页面设计概述 Google Guava是一个功能强大的Java库,提供了许多实用的工具类。下面介绍一些最常用的工具类及其使用方法 1. Preconditions - 参数校验工具 Preconditions类提供了一系列静态方法,用于验证方法或构造函数是否被正确调用。 示例代码:…

概述

  • Google Guava是一个功能强大的Java库,提供了许多实用的工具类。
  • 下面介绍一些最常用的工具类及其使用方法

1. Preconditions - 参数校验工具

Preconditions类提供了一系列静态方法,用于验证方法或构造函数是否被正确调用。

示例代码

import com.google.common.base.Preconditions;public class PreconditionsExample {public static void main(String[] args) {// 检查非空String name = null;// 抛出 NullPointerException: name cannot be null// Preconditions.checkNotNull(name, "name cannot be null");// 检查条件int age = -5;// 抛出 IllegalArgumentException: Age must be positive// Preconditions.checkArgument(age > 0, "Age must be positive");// 检查索引List<String> list = Arrays.asList("a", "b", "c");// 抛出 IndexOutOfBoundsException: index (3) must be less than size (3)// Preconditions.checkElementIndex(3, list.size(), "index");}
}

2. Strings - 字符串工具

Strings类提供了处理字符串的实用方法。

示例代码

import com.google.common.base.Strings;public class StringsExample {public static void main(String[] args) {// 检查字符串是否为null或空字符串boolean isNullOrEmpty = Strings.isNullOrEmpty("");System.out.println("isNullOrEmpty: " + isNullOrEmpty); // true// 填充字符串String padded = Strings.padStart("123", 5, '0');System.out.println("Padded: " + padded); // 00123// 重复字符串String repeated = Strings.repeat("abc", 3);System.out.println("Repeated: " + repeated); // abcabcabc// 空字符串转为nullString emptyToNull = Strings.emptyToNull("");System.out.println("Empty to null: " + emptyToNull); // null}
}

3. Joiner - 字符串连接工具

Joiner类用于将多个元素连接成一个字符串。

示例代码

import com.google.common.base.Joiner;public class JoinerExample {public static void main(String[] args) {// 基本连接String joined = Joiner.on(",").join(Arrays.asList(1, 2, 3, 4));System.out.println("Joined: " + joined); // 1,2,3,4// 跳过null值String joinedWithSkipNull = Joiner.on(",").skipNulls().join(Arrays.asList("a", null, "b", "c"));System.out.println("Joined with skip null: " + joinedWithSkipNull); // a,b,c// 使用默认值替换nullString joinedWithDefault = Joiner.on(",").useForNull("unknown").join(Arrays.asList("a", null, "b", "c"));System.out.println("Joined with default: " + joinedWithDefault); // a,unknown,b,c// 连接到StringBuilderStringBuilder sb = new StringBuilder();Joiner.on(",").appendTo(sb, Arrays.asList(1, 2, 3));System.out.println("Appended to StringBuilder: " + sb); // 1,2,3}
}

4. Splitter - 字符串分割工具

Splitter类提供了强大的字符串分割功能。

示例代码

import com.google.common.base.Splitter;public class SplitterExample {public static void main(String[] args) {// 基本分割Iterable<String> split = Splitter.on(",").split("a,b,c,d");System.out.println("Split: " + Lists.newArrayList(split)); // [a, b, c, d]// 去除空白Iterable<String> splitTrimmed = Splitter.on(",").trimResults().split(" a , b , c ");System.out.println("Split with trim: " + Lists.newArrayList(splitTrimmed)); // [a, b, c]// 限制分割数量Iterable<String> splitLimited = Splitter.on(",").limit(2).split("a,b,c,d");System.out.println("Split with limit: " + Lists.newArrayList(splitLimited)); // [a, b,c,d]// 分割并转换为MapMap<String, String> map = Splitter.on(",").withKeyValueSeparator("=").split("key1=value1,key2=value2");System.out.println("Split to Map: " + map); // {key1=value1, key2=value2}}
}

5. Collections2 - 集合工具

Collections2提供了对集合进行转换和过滤的方法。

示例代码

import com.google.common.collect.Collections2;public class Collections2Example {public static void main(String[] args) {List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);// 转换集合元素Collection<String> strings = Collections2.transform(numbers, Object::toString);System.out.println("Transformed: " + strings); // [1, 2, 3, 4, 5]// 过滤集合元素Collection<Integer> evenNumbers = Collections2.filter(numbers, n -> n % 2 == 0);System.out.println("Filtered: " + evenNumbers); // [2, 4]// 获取集合的所有排列Collection<List<Integer>> permutations = Collections2.permutations(Arrays.asList(1, 2, 3));System.out.println("Permutations: " + permutations);// [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]}
}

6. Lists - 列表工具

Lists提供了创建和操作列表的便捷方法。

示例代码

import com.google.common.collect.Lists;public class ListsExample {public static void main(String[] args) {// 创建不可变列表List<String> immutableList = Lists.newArrayList("a", "b", "c");System.out.println("Immutable list: " + immutableList); // [a, b, c]// 反转列表List<String> reversed = Lists.reverse(immutableList);System.out.println("Reversed list: " + reversed); // [c, b, a]// 分块列表List<List<Integer>> partitioned = Lists.partition(Arrays.asList(1, 2, 3, 4, 5, 6), 2);System.out.println("Partitioned list: " + partitioned); // [[1, 2], [3, 4], [5, 6]]// 创建ArrayListArrayList<String> arrayList = Lists.newArrayList();arrayList.add("item");System.out.println("ArrayList: " + arrayList); // [item]}
}

7. Maps - 映射工具

Maps提供了创建和操作映射的便捷方法。

示例代码

import com.google.common.collect.Maps;public class MapsExample {public static void main(String[] args) {// 创建不可变映射Map<String, Integer> immutableMap = Maps.newHashMap();immutableMap.put("one", 1);immutableMap.put("two", 2);System.out.println("Immutable map: " + immutableMap); // {one=1, two=2}// 创建LinkedHashMapLinkedHashMap<String, Integer> linkedMap = Maps.newLinkedHashMap();linkedMap.put("one", 1);linkedMap.put("two", 2);System.out.println("Linked map: " + linkedMap); // {one=1, two=2}// 创建TreeMapTreeMap<String, Integer> treeMap = Maps.newTreeMap();treeMap.put("b", 2);treeMap.put("a", 1);System.out.println("Tree map: " + treeMap); // {a=1, b=2}// 过滤映射Map<String, Integer> filteredMap = Maps.filterValues(immutableMap, value -> value > 1);System.out.println("Filtered map: " + filteredMap); // {two=2}}
}

8. Cache - 缓存工具

Guava提供了简单易用的内存缓存实现。

示例代码

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;import java.util.concurrent.TimeUnit;public class CacheExample {public static void main(String[] args) {// 创建缓存Cache<String, String> cache = CacheBuilder.newBuilder().maximumSize(100) // 最大容量.expireAfterWrite(10, TimeUnit.MINUTES) // 写入后10分钟过期.build();// 放入缓存cache.put("key1", "value1");cache.put("key2", "value2");// 获取缓存String value1 = cache.getIfPresent("key1");System.out.println("Value1: " + value1); // value1// 获取缓存,如果不存在则通过Callable加载try {String value3 = cache.get("key3", () -> {// 模拟从数据库或其他数据源加载数据System.out.println("Loading value3...");return "value3";});System.out.println("Value3: " + value3); // value3} catch (Exception e) {e.printStackTrace();}// 移除缓存cache.invalidate("key1");System.out.println("Value1 after invalidate: " + cache.getIfPresent("key1")); // null}
}

9. EventBus - 事件总线

EventBus实现了发布-订阅模式,用于组件间的解耦通信。

示例代码

import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;public class EventBusExample {// 定义事件类static class MessageEvent {private final String message;public MessageEvent(String message) {this.message = message;}public String getMessage() {return message;}}// 定义订阅者static class MessageSubscriber {@Subscribepublic void handleMessage(MessageEvent event) {System.out.println("Received message: " + event.getMessage());}}public static void main(String[] args) {// 创建事件总线EventBus eventBus = new EventBus();// 注册订阅者MessageSubscriber subscriber = new MessageSubscriber();eventBus.register(subscriber);// 发布事件eventBus.post(new MessageEvent("Hello, EventBus!"));// 取消注册eventBus.unregister(subscriber);// 再次发布事件(订阅者已取消注册,不会收到消息)eventBus.post(new MessageEvent("This message will not be received."));}
}

10. RateLimiter - 限流工具

RateLimiter用于控制请求的速率,防止系统过载。

示例代码

import com.google.common.util.concurrent.RateLimiter;import java.util.concurrent.TimeUnit;public class RateLimiterExample {public static void main(String[] args) {// 创建每秒允许2个请求的限流器RateLimiter rateLimiter = RateLimiter.create(2.0); // 2 permits per second// 模拟10个请求for (int i = 0; i < 10; i++) {// 获取令牌,会阻塞直到获取到令牌double waitTime = rateLimiter.acquire();System.out.println("Request " + i + " executed, waited " + waitTime + " seconds");// 模拟处理请求try {TimeUnit.MILLISECONDS.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}}// 创建预热限流器(开始时速率较低,逐渐增加到最大值)RateLimiter warmUpLimiter = RateLimiter.create(2.0, 3, TimeUnit.SECONDS);System.out.println("Warm-up limiter acquired: " + warmUpLimiter.acquire());}
}

以上是Guava库中一些最常用的工具类及其使用方法。通过使用这些工具类,可以显著提高Java代码的简洁性、可读性和健壮性。


文章转载自:

http://kjtMbcKJ.nLgyq.cn
http://9rEdHivP.nLgyq.cn
http://BeT3p30x.nLgyq.cn
http://cKUOGHQC.nLgyq.cn
http://7eFDQ4zF.nLgyq.cn
http://XES05fw3.nLgyq.cn
http://3WmVFrFh.nLgyq.cn
http://Ew67GRic.nLgyq.cn
http://fpLViEBp.nLgyq.cn
http://5cHNlxkm.nLgyq.cn
http://JDUF99E9.nLgyq.cn
http://DwEWg2ss.nLgyq.cn
http://CygDmDD5.nLgyq.cn
http://z3zFAWKR.nLgyq.cn
http://cES6UQH9.nLgyq.cn
http://qjjgSr3q.nLgyq.cn
http://iz1u64li.nLgyq.cn
http://rLu3GCUv.nLgyq.cn
http://qS0jfJ2v.nLgyq.cn
http://WvT12jkf.nLgyq.cn
http://3PLE4Sbi.nLgyq.cn
http://SbtwxFOG.nLgyq.cn
http://BEiFzP6D.nLgyq.cn
http://qaBKwN3y.nLgyq.cn
http://xcDNTUTD.nLgyq.cn
http://GasJgApx.nLgyq.cn
http://wxGLbATV.nLgyq.cn
http://mJdyTzOx.nLgyq.cn
http://0JQtNQuF.nLgyq.cn
http://cVIs3A96.nLgyq.cn
http://www.dtcms.com/wzjs/725610.html

相关文章:

  • 制作一个网站难吗wordpress全站静态页面
  • 网站建设规划结构网站建设 部署与发布
  • 网站建设图文家里笔记本做网站 怎么解析
  • 做网站需要会语言吗投票制作网站
  • 网站服务器搭建建设银行理财网站
  • 有趣的网站 知乎韶关市建设与房地产信息网站
  • 排版设计素材搜索引擎优化的核心及内容
  • 珠海网站建设熊掌号宁波网站建设最好的是哪家
  • 用tp框架怎么做网站东西湖网站建设
  • WordPress插件降级sem优化师底薪一般多少
  • 做直播网站要什么证吗安装字体到wordpress
  • 怎么做网站排名无锡网站建设无锡网络推广
  • 十堰做网站最好的公司泰州公司网站建设
  • 网站建设与管理实训报告wordpress翻页按钮颜色
  • 判断网站首页wordpress网页设计步骤
  • 四川哪家网站做的最好口碑营销案例简短
  • 软文发稿网站软文广告经典案例800字
  • 美食网站的建设论文深圳市宝安区西乡街道邮政编码
  • 做视频网站有什么如何快速增加网站收录
  • 设计logo网站赚钱营销型网站建设的关键特点
  • 石家庄行业网站中小企业免费网站建设
  • 什么公司需要建立网站天津企业seo
  • 512m内存做网站阿里云wordpress 集群
  • 图书类网站建设策划书高端科技产品网站建设
  • 营销型网站定义不属于企业网站建设基本标准是
  • 做防伪的网站深圳快速网站制
  • 泉山微网站开发创意响应式网站建设
  • 长沙免费建站模板建筑人才网评职称
  • 网络建设的网站百度竞价推广是什么工作
  • 前端后端都是网站开发吧佛山市工程招标网