Java 中节省 90% 时间的常用的工具类

System.out.println("--------------");

Map<String, ImagesTypeEnum> imagesTypeEnumMap = EnumUtils.getEnumMap(ImagesTypeEnum.class);

imagesTypeEnumMap.forEach((k, v) -> System.out.println(“key:” + k + “,value:” + v));

System.out.println("-------------");

boolean result = EnumUtils.isValidEnum(ImagesTypeEnum.class, “JPG”);

System.out.println("result = " + result);

boolean result1 = EnumUtils.isValidEnum(ImagesTypeEnum.class, null);

System.out.println("result1 = " + result1);

}

输出结果:

imagesTypeEnum = JPG


imagesTypeEnum1 = JPG

imagesTypeEnum1 = JPEG

imagesTypeEnum1 = PNG

imagesTypeEnum1 = GIF


key:JPG,value:JPG

key:JPEG,value:JPEG

key:PNG,value:PNG

key:GIF,value:GIF


result = true

result1 = false

Process finished with exit code 0

[](

)collections4 集合操作

commons-collections4 增强了 Java 集合框架,提供了一系列简单的 API 方便操作集合。

maven 依赖

org.apache.commons

commons-collections4

4.4

[](

)CollectionUtils 工具类

这是一个工具类,可以检查 null 元素不被加入集合,合并列表,过滤列表,两个列表的并集、差集、合集。有部分功能在 Java 8 中可以被 Stream API 替换。

public static void main(String[] args) {

//null 元素不能加进去

List arrayList1 = new ArrayList<>();

arrayList1.add(“a”);

CollectionUtils.addIgnoreNull(arrayList1, null);

System.out.println(arrayList1.size());

//排好序的集合,合并后还是排序的

List arrayList2 = new ArrayList<>();

arrayList2.add(“a”);

arrayList2.add(“b”);

List arrayList3 = new ArrayList<>();

arrayList3.add(“c”);

arrayList3.add(“d”);

System.out.println(“arrayList3:” + array

【一线大厂Java面试题解析+核心总结学习笔记+最新架构讲解视频+实战项目源码讲义】

浏览器打开:qq.cn.hn/FTf 免费领取

List3);

List arrayList4 = CollectionUtils.collate(arrayList2, arrayList3);

System.out.println(“arrayList4:” + arrayList4);

//交集

Collection strings = CollectionUtils.retainAll(arrayList4, arrayList3);

System.out.println(“arrayList3和arrayList4的交集:” + strings);

//并集

Collection union = CollectionUtils.union(arrayList4, arrayList3);

System.out.println(“arrayList3和arrayList4的并集:” + union);

//差集

Collection subtract = CollectionUtils.subtract(arrayList4, arrayList3);

System.out.println(“arrayList3和arrayList4的差集:” + subtract);

// 过滤,只保留 b

CollectionUtils.filter(arrayList4, s -> s.equals(“b”));

System.out.println(arrayList4);

}

输出结果:

1

arrayList3:[c, d]

arrayList4:[a, b, c, d]

arrayList3和arrayList4的交集:[c, d]

arrayList3和arrayList4的并集:[a, b, c, d]

arrayList3和arrayList4的差集:[a, b]

[b]

Process finished with exit code 0

[](

)Bag 统计次数

用于统计值在集合中出现的次数。

public static void main(String[] args) {

Bag bag = new HashBag();

bag.add(“a”);

bag.add(“b”);

bag.add(“a”);

bag.add(“c”, 3);

System.out.println(bag);

System.out.println(bag.getCount(“c”));

}

输出结果:

[2:a,1:b,3:c]

3

Process finished with exit code 0

[](

)beanutils Bean 操作

beanutils 是通过反射机制对 JavaBean 进行操作的。比如对 Bean 进行复制、map 转对象、对象转 Map。

maven 依赖

commons-beanutils

commons-beanutils

1.9.4

public class User {

private String name;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

public static void main(String[] args) throws Exception {

User user1 = new User();

user1.setName(“李四”);

User user2 = (User) BeanUtils.cloneBean(user1);

System.out.println(user2.getName());

//User 转 map

Map<String, String> describe = BeanUtils.describe(user1);

System.out.println(describe);

//Map 转 User

Map<String, String> beanMap = new HashMap();

beanMap.put(“name”, “张三”);

User user3 = new User();

BeanUtils.populate(user3, beanMap);

System.out.println(user3.getName());

}

输出结果:

李四

{name=李四}

张三

Process finished with exit code 0

[](

)Guava


Google 开源的一个基于 Java 扩展项目,包含了一些基本工具、集合扩展、缓存、并发工具包、字符串处理等。

maven 依赖

com.google.guava

guava

30.1.1-jre

[](

)Map<String, List> 类型

在java 代码中经常会遇到需要写 Map<String, List> map 的局部变量的时候。有时候业务情况还会更复杂一点。

public static void main(String[] args) {

//以前

Map<String, List> map = new HashMap<>();

List list = new ArrayList<>();

list.add(“张三”);

list.add(“李四”);

map.put(“名称”, list);

System.out.println(map.get(“名称”));

//现在

Multimap<String, String> multimap = ArrayListMultimap.create();

multimap.put(“名称”, “张三”);

multimap.put(“名称”, “李四”);

System.out.println(multimap.get(“名称”));

}

输出结果:

[张三, 李四]

[张三, 李四]

Process finished with exit code 0

[](

)value 不能重复的 Map

在 Map 中 value 的值时可以重复的,Guava 可以创建一个 value 不可重复的 Map,并且 Map 和 value 可以对调。

public static void main(String[] args) {

//会报异常

BiMap<String ,String> biMap = HashBiMap.create();

biMap.put(“key1”, “value”);

biMap.put(“key2”, “value”);

System.out.println(biMap.get(“key1”));

}

输出结果:

Exception in thread “main” java.lang.IllegalArgumentException: value already present: value

at com.google.common.collect.HashBiMap.put(HashBiMap.java:287)

at com.google.common.collect.HashBiMap.put(HashBiMap.java:262)

at org.example.clone.Test.main(Test.java:17)

Process finished with exit code 1

public static void main(String[] args) {

BiMap<String ,String> biMap = HashBiMap.create();

biMap.put(“key1”, “value1”);

biMap.put(“key2”, “value2”);

System.out.println(biMap.get(“key1”));

//key-value 对调

biMap = biMap.inverse();

System.out.println(biMap.get(“value1”));

}

输出结果:

value1

key1

Process finished with exit code 0

[](

)Guava cache

写业务的时候肯定会使用缓存,当不想用第三方作为缓存的时候,Map 又不够强大,就可以使用 Guava 的缓存。

[](

)缓存的并发级别

Guava提供了设置并发级别的API,使得缓存支持并发的写入和读取。与ConcurrentHashMap类似,Guava cache的并发也是通过分离锁实现。在通常情况下,推荐将并发级别设置为服务器cpu核心数。

CacheBuilder.newBuilder()

// 设置并发级别为cpu核心数,默认为4

.concurrencyLevel(Runtime.getRuntime().availableProcessors())

.build();

[](

)缓存的初始容量设置

我们在构建缓存时可以为缓存设置一个合理大小初始容量,由于Guava的缓存使用了分离锁的机制,扩容的代价非常昂贵。所以合理的初始容量能够减少缓存容器的扩容次数。

CacheBuilder.newBuilder()

// 设置初始容量为100

.initialCapacity(100)

.build();

[](

)设置最大存储

Guava Cache可以在构建缓存对象时指定缓存所能够存储的最大记录数量。当Cache中的记录数量达到最大值后再调用put方法向其中添加对象,Guava会先从当前缓存的对象记录中选择一条删除掉,腾出空间后再将新的对象存储到Cache中。

public static void main(String[] args) {

Cache<String, String> cache = CacheBuilder.newBuilder().maximumSize(2).build();

cache.put(“key1”, “value1”);

cache.put(“key2”, “value2”);

cache.put(“key3”, “value3”);

System.out.println(cache.getIfPresent(“key1”)); //key1 = null

}

输出结果:

null

Process finished with exit code 0

[](

)过期时间

expireAfterAccess() 可以设置缓存的过期时间。

public static void main(String[] args) throws InterruptedException {

//设置过期时间为2秒

Cache<String, String> cache1 = CacheBuilder.newBuilder().maximumSize(2).expireAfterAccess(2, TimeUnit.SECONDS).build();

cache1.put(“key1”, “value1”);

Thread.sleep(1000);

System.out.println(cache1.getIfPresent(“key1”));

Thread.sleep(2000);

System.out.println(cache1.getIfPresent(“key1”));

}

输出结果:

value1

null

Process finished with exit code 0

[](

)LoadingCache

使用自定义ClassLoader加载数据,置入内存中。从LoadingCache中获取数据时,若数据存在则直接返回;若数据不存在,则根据ClassLoaderload方法加载数据至内存,然后返回该数据。

public class Test {

public static void main(String[] args) throws Exception {

System.out.println(numCache.get(1));

Thread.sleep(1000);

System.out.println(numCache.get(1));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值