这是一个工具类,可以检查 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:” + arrayList3);
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
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
需要zi料+ 绿色徽【vip1024b】
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
最后
面试前一定少不了刷题,为了方便大家复习,我分享一波个人整理的面试大全宝典
- Java核心知识整理
Java核心知识
- Spring全家桶(实战系列)
- 其他电子书资料
Step3:刷题
既然是要面试,那么就少不了刷题,实际上春节回家后,哪儿也去不了,我自己是刷了不少面试题的,所以在面试过程中才能够做到心中有数,基本上会清楚面试过程中会问到哪些知识点,高频题又有哪些,所以刷题是面试前期准备过程中非常重要的一点。
以下是我私藏的面试题库:
esent(“key1”));
Thread.sleep(2000);
System.out.println(cache1.getIfPresent(“key1”));
}
输出结果:
value1
null
Process finished with exit code 0
最后
面试前一定少不了刷题,为了方便大家复习,我分享一波个人整理的面试大全宝典
- Java核心知识整理
[外链图片转存中…(img-RXF27njn-1710350481808)]
Java核心知识
- Spring全家桶(实战系列)
[外链图片转存中…(img-XHfi3HVR-1710350481809)]
- 其他电子书资料
[外链图片转存中…(img-Cac4n1q9-1710350481809)]
Step3:刷题
既然是要面试,那么就少不了刷题,实际上春节回家后,哪儿也去不了,我自己是刷了不少面试题的,所以在面试过程中才能够做到心中有数,基本上会清楚面试过程中会问到哪些知识点,高频题又有哪些,所以刷题是面试前期准备过程中非常重要的一点。
以下是我私藏的面试题库:
[外链图片转存中…(img-HNUNQvZb-1710350481809)]