Guava中针对集合的 filter和过滤功能
package com.kaishengit;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.kaishengit.entity.User;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
public class Test {
public static void main(String[] args) {
/*User user = new User();
user.setId(100);
user.setFirstName("Jack");
User user2 = new User();
user2.setId(101);
user2.setFirstName("Rose");
User user3 = new User();
user3.setId(103);
user3.setFirstName("Rose2");
List<User> userList = Lists.newArrayList(user,user2,user3);
List<User> subList = Lists.newArrayList(Collections2.filter(userList, new Predicate<User>() {
@Override
public boolean apply(User user) {
return user.getFirstName().startsWith("Ro");
}
}));
Set<String> set = Sets.newHashSet();
Map<String,Object> map = Maps.newHashMap();*/
/*List<String> nameList = Lists.newArrayList(Collections2.transform(userList, new Function<User, String>() {
@Override
public String apply(User f) {
return f.getFirstName();
}
}));*/
/* List<String> nameList = Lists.newArrayList();
for(User u : userList) {
nameList.add(u.getFirstName());
}*/
//List<String> list = Lists.newArrayList("A1","A2");
/*List<String> list = Lists.newArrayList();
list.add("A1");
list.add("A2");*/
}
}
2017-9-26 9-10-29.png
guava使用.png
guava过滤.png
clipboard.png
缓存 测试
package com.kaishengit.service;
import com.kaishengit.entity.User;
import org.junit.Test;
import static org.junit.Assert.*;
public class UserServiceWithGuavaTest {
private UserService userService = new UserService();
@Test
public void findById() throws Exception {
User user = userService.findById(7);
user = userService.findById(7);
System.out.println(user);
}
}
配置
package com.kaishengit.service;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.kaishengit.dao.UserDao;
import com.kaishengit.entity.User;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
public class UserServiceWithGuava {
private UserDao userDao = new UserDao();
private static Cache<String,User> cache = CacheBuilder.newBuilder()
.maximumSize(100)//f放一百个数据
.expireAfterAccess(10,TimeUnit.MINUTES)//天数
.build();
public User findById(Integer id) {
User user = null;
try {
user = cache.get("user:" + id, new Callable<User>() {
@Override
public User call() {
return userDao.findById(id);
}
});
} catch (ExecutionException e) {
e.printStackTrace();
}
return user;
}
/*private static LoadingCache<String,User> cache = CacheBuilder
.newBuilder()
.maximumSize(100)
.expireAfterAccess(50, TimeUnit.SECONDS)
.build(new CacheLoader<String, User>() {
@Override
public User load(String key) {
UserDao userDao = new UserDao();
return userDao.findById(Integer.valueOf(key));
}
});
public User findById(Integer id) {
User user = cache.getUnchecked(id.toString()); //cache.get("user:"+id); 与 cache.getUnchecked(id.toString());区别当上面重写方法load()的时候getUnchecked 部抛异常
return user;
}*/
}
缓存使用
// 发送激活用户账号的邮件,写进去时间,如果没人访问就过期,有人访问就延期时间
private static Cache<String, String> cache = CacheBuilder
.newBuilder()
.expireAfterWrite(6, TimeUnit.HOURS)
.build();
// 找回密码token
private static Cache<String, String> passwordCache = CacheBuilder.newBuilder()
.expireAfterWrite(30, TimeUnit.MINUTES)
.build();
// 限制操作pinlv
private static Cache<String, String> activeCache = CacheBuilder.newBuilder()
.expireAfterWrite(30, TimeUnit.SECONDS)
.build();
测试
/**
* Created by wanggs on 2017/9/28.
*/
public class CacheTest {
public static void main(String[] args) {// 限制操作pinlv
Cache<String, String> activeCache = CacheBuilder.newBuilder()
.expireAfterWrite(1, TimeUnit.MILLISECONDS)
.build();
String id = "1";
while (true){
if (activeCache.getIfPresent(id) == null){
System.out.println("处理业务........***************************..........");
activeCache.put(id,"000000000");
}else {
System.out.println("操作频率太快");
}
}
}
}
转换器:利用 Collections2.transform() 方法来转换集合中的元素。譬如把一个 Set<Integer> 里面所有元素都转换成带格式的 String 来产生新的 Collection<String>:
@Test
public void transForm() {
HashSet<Long> set = Sets.newHashSet();
set.add(20000000000000L);
set.add(30000000000000L);
set.add(40000000000000L);
List<String> strings = Lists.newArrayList(Collections2.transform(set, new Function<Long, String>() {
@Override
public String apply(Long aLong) {
return String.valueOf(aLong).substring(11);
}
}));
for (String st:strings){
System.out.println(st);
}
}
网友评论