Commit fee142f4 by 景园园

添加测试方法

parent f81ee5c2
package cn.freemud.demo;
import cn.freemud.demo.po.User;
import cn.freemud.demo.zookeeper.Q;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.*;
import com.google.common.util.concurrent.RateLimiter;
import org.junit.Test;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* Created by chenwenshun on 2018/9/27.
*/
public class GuavaTests {
@Test
public void testSplit(){
String mock = ",,a, b,c,,d,,";
String[] mocks = mock.split(",");
for (int i = 0; i < mocks.length; i++) {
System.out.println(mocks[i]);
}
List<String> list = Splitter.on(",").omitEmptyStrings().trimResults().splitToList(mock);
System.out.println(list.toString());
Joiner joiner = Joiner.on("/").skipNulls();
String result = joiner.join("a", 1, null, 'c');
System.out.println(result);
String[] names = new String[]{"bella", "tina", "ashun"};
System.out.println(joiner.join(names));
List<String> time = Splitter.on(":").splitToList("10:56");
System.out.println(time);
}
@Test
public void testRateLimite(){
RateLimiter limiter = RateLimiter.create(3);
// RateLimiter limiter = RateLimiter.create(10, 5, TimeUnit.SECONDS);
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
for (; ; ) {
limiter.acquire();
System.out.println(df.format(new Date()));
}
}
@Test
public void testMultimap(){
ListMultimap<Integer, String> listMutilMap = ArrayListMultimap.create();
listMutilMap.put(100, "1001");
listMutilMap.put(2, "21");
listMutilMap.put(2, "22");
listMutilMap.put(1, "1");
listMutilMap.put(1, "2");
listMutilMap.put(43, "431");
System.out.println(listMutilMap.keySet());//
System.out.println(listMutilMap.get(2));
}
/**
* 不可变集合
*/
@Test
public void test4(){
List<String> list = Lists.newArrayList("a", "b", "c");
Collection<String> unmodifiableCollection = Collections.unmodifiableCollection(list);
ImmutableList<String> immutableList = ImmutableList.copyOf(list);
list.add("e");
System.out.println(unmodifiableCollection);
System.out.println(immutableList);
immutableList.add("a");
}
/**
* 集合 索引
*/
@Test
public void test5(){
List<User> users = Lists.newArrayList();
users.add(new User(1L,"haha"));
users.add(new User(2L,"bella"));
users.add(new User(3L,"tina"));
users.add(new User(4L,"tom"));
users.add(new User(5L,"jerry"));
users.add(new User(6L,"tony"));
users.add(new User(7L,"tony"));
/**
* 对集合添加唯一索引
*/
ImmutableMap<Long, User> uniqueIndex = FluentIterable
.from(users).uniqueIndex(new Function<User, Long>() {
@Override
public Long apply(User user) {
return user.getId();
}
});
User user = uniqueIndex.get(3L);
System.out.println(user);
/**
* 对集合添加非唯一索引
*/
ImmutableListMultimap<String, User> index = FluentIterable
.from(users).index(new Function<User,String>() {
@Override
public String apply(User user) {
return user.getUsername();
}
});
List<User> userList = index.get("tony");
System.out.println(userList);
}
/**
* 集合运算主要有:并集、交集、差集。
*/
@Test
public void test6() {
List<Integer> list1 = Lists.newArrayList(1, 2, 2, 1, 4, 5, 4, 3);
List<Integer> list2 = Lists.newArrayList(1, 2, 3, 7, 8, 9);
Set set = new HashSet(list1);
Sets.SetView<Integer> union = Sets.union(Sets.newHashSet(list1), Sets.newHashSet(list2));
Sets.SetView<Integer> difference = Sets.difference(Sets.newHashSet(list1), Sets.newHashSet(list2));
Sets.SetView<Integer> intersection = Sets.intersection(Sets.newHashSet(list1), Sets.newHashSet(list2));
System.out.println(union);
System.out.println(difference);
System.out.println(intersection);
list1.parallelStream().forEach(System.out::println);
list1.stream().forEach(System.out::println);
int a = list1.stream().max(Comparator.comparing(e -> e)).get();
int sum = list1.stream().mapToInt(e -> e).sum();
System.out.println(sum);
}
/**
* java 8 Stream
*/
@Test
public void test7(){
List<User> users = Lists.newArrayList();
users.add(new User(1L,"haha"));
users.add(new User(2L,"bella"));
users.add(new User(3L,"tina"));
users.add(new User(4L,"tom"));
users.add(new User(5L,"jerry"));
users.add(new User(6L,"tony"));
users.add(new User(7L,"tony"));
List<Q> names = users.parallelStream()
// .filter(u -> u.getUsername().equals("tony"))
// .sorted(Comparator.comparing(User::getId).reversed())
.sorted((user1,user2) -> user1.getId().compareTo(user2.getId()))
.map(user -> {
Q q = new Q(user.getId().intValue());
return q;
})
.collect(Collectors.toList());
System.out.println(names);
Map<Long,User> userMap = users.stream().collect(Collectors.toMap(user -> user.getId(),user -> user)) ;
System.out.println(userMap);
/**
* 非唯一索引
*/
Map<Integer, List<User>> integerListMap =
Stream.generate(new UserSupplier())
.limit(100)
.collect(Collectors.groupingBy(User::getAge));
System.out.println(integerListMap);
}
private class UserSupplier implements Supplier<User>{
private long index = 0;
private Random random = new Random();
@Override
public User get() {
return new User(index++, "StormTestUser"+random.nextInt(1000), random.nextInt(100));
}
}
}
...@@ -2,10 +2,14 @@ package cn.freemud.demo; ...@@ -2,10 +2,14 @@ package cn.freemud.demo;
import org.junit.Test; import org.junit.Test;
import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
/** /**
*测试HashMap * 测试HashMap
*
* @author jingyuanyuan * @author jingyuanyuan
*/ */
public class HashMapTest { public class HashMapTest {
...@@ -13,67 +17,136 @@ public class HashMapTest { ...@@ -13,67 +17,136 @@ public class HashMapTest {
* HashMap的键和值都可以存储为null * HashMap的键和值都可以存储为null
*/ */
@Test @Test
public void test(){ public void testNull() {
System.out.println("hello this is test"); System.out.println("hello this is test");
HashMap map=new HashMap(); HashMap map = new HashMap();
map.put(null,"你好"); map.put(null, "你好");
map.put("key1",null); map.put("key1", null);
System.out.println("map size:"+map.size()); System.out.println("map size:" + map.size());
System.out.println("key is null value:"+map.get(null)); System.out.println("key is null value:" + map.get(null));
System.out.println("value is null value:"+map.get("key1")); System.out.println("value is null value:" + map.get("key1"));
} }
/** /**
* clear 清空集合 * clear 清空集合
*/ */
@Test @Test
public void clearTest(){ public void clearTest() {
HashMap map=new HashMap(); HashMap map = new HashMap();
map.put("key1","张三"); map.put("key1", "张三");
System.out.println("map size:"+map.size()); System.out.println("map size:" + map.size());
map.clear(); map.clear();
System.out.println("map size:"+map.size()); System.out.println("map size:" + map.size());
} }
/** /**
* clone 克隆集合 * clone 克隆集合
*/ */
@Test @Test
public void cloneTest(){ public void cloneTest() {
HashMap map=new HashMap(); HashMap map = new HashMap();
map.put("key1","张三"); map.put("key1", "张三");
map.put("key2","李四"); map.put("key2", "李四");
System.out.println("map size:"+map.size()); System.out.println("map size:" + map);
Object clone = map.clone(); Object clone = map.clone();
System.out.println("clone map size:"+clone); System.out.println("clone map size:" + clone);
} }
/** /**
* 测试集合中是否包含指定的key * 测试集合中是否包含指定的key
*/ */
@Test @Test
public void containsKeyTest(){ public void containsKeyTest() {
HashMap map=new HashMap(); HashMap map = new HashMap();
map.put("key1","张三"); map.put("key1", "张三");
map.put("key2","李四"); map.put("key2", "李四");
boolean key1 = map.containsKey("key1"); boolean key1 = map.containsKey("key1");
boolean key3 = map.containsKey("key3"); boolean key3 = map.containsKey("key3");
System.out.println("containsKey key1:"+key1); System.out.println("containsKey key1:" + key1);
System.out.println("containsKey key3:"+key3); System.out.println("containsKey key3:" + key3);
} }
/** /**
* 测试集合中是否包含指定的value * 测试集合中是否包含指定的value
*/ */
@Test @Test
public void containsValueTest(){ public void containsValueTest() {
HashMap map=new HashMap(); HashMap map = new HashMap();
map.put("key1","张三"); map.put("key1", "张三");
map.put("key2","李四"); map.put("key2", "李四");
boolean a = map.containsValue("张"); boolean a = map.containsValue("张");
boolean b = map.containsValue("李四"); boolean b = map.containsValue("李四");
System.out.println("containsValue 张:"+a); System.out.println("containsValue 张:" + a);
System.out.println("containsValue 李四:"+b); System.out.println("containsValue 李四:" + b);
}
/**
* 测试HashMap集合的size方法
* 返回HashMap集合里面的成员数量
*/
@Test
public void sizeTest() {
HashMap map = new HashMap();
int size = map.size();
System.out.println("size:" + size);
map.put("key1", "张三");
map.put("key2", "李四");
size = map.size();
System.out.println("size:" + size);
}
/**
* 测试HashMap集合的isEmpty方法
* 判断集合里面的成员是否为空
*/
@Test
public void isEmptyTest() {
HashMap map = new HashMap();
boolean empty = map.isEmpty();
System.out.println("empty:" + empty);
map.put("key1", "张三");
map.put("key2", "李四");
empty = map.isEmpty();
System.out.println("empty:" + empty);
}
/**
* 测试HashMap集合的get方法
* 通过key获取HashMap里面的对应的值
*/
@Test
public void getTest() {
HashMap map = new HashMap();
map.put("key1", "张三");
map.put("key2", "李四");
Object val = map.get("key1");
System.out.println("key1:" + val);
}
/**
* 测试HashMap集合的remove方法
* 通过key移除HashMap里面的对应的成员
*/
@Test
public void removeTest() {
HashMap map = new HashMap();
map.put("key1", "张三");
map.put("key2", "李四");
System.out.println("size:"+map.size());
map.remove("key1");
System.out.println("size:"+map.size());
}
/**
* 测试HashMap集合的values方法
* 返回HashMap的值作为集合Collection返回
*/
@Test
public void valuesTest() {
HashMap map = new HashMap();
map.put("key1", "张三");
map.put("key2", "李四");
System.out.println(map);
Collection values = map.values();
System.out.println(values);
} }
......
package cn.freemud.demo;
import org.junit.Test;
import java.util.HashSet;
import java.util.Set;
/**
* @author: yuanyuan.jing
* @date: 2018/11/1 10:30
* @description: Set测试类
*/
public class SetTest {
/**
* 测试Set集合的size方法
* 返回set集合里面的成员数量
*/
@Test
public void sizeTest() {
Set set = new HashSet();
int size = set.size();
System.out.println("size:" + size);
set.add("zhangsan");
size = set.size();
System.out.println("size:" + size);
}
/**
* 测试Set集合的isEmpty方法
* 判断集合里面的成员是否为空
*/
@Test
public void isEmptyTest() {
Set set = new HashSet();
boolean empty = set.isEmpty();
System.out.println("empty:" + empty);
set.add("zhangsan");
empty = set.isEmpty();
System.out.println("empty:" + empty);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment