今天时间学习ConcurrentSkipListSet api,该类是JUC原子包中的类,通过单元测试代码把所有public api方法跑了一遍,大致了解了底层实现,初学乍练,有很多一知半解的地方,待后续有了深入理解再来补充
package test.java.util.concurrent;
import java.util.*;
import java.util.concurrent.ConcurrentSkipListSet;
import org.junit.Test;
/**
* ConcurrentSkipListSet的测试类
*
* @date 2020-07-09 00:21:55
*/
public class ConcurrentSkipListSetTest {
/**
*无参构造函数,使用ConcurrentSkipListMap的key
* @Param
*/
@Test
public void testConstruct0()throws Exception{
ConcurrentSkipListSet testObj=new ConcurrentSkipListSet();
System.out.println(testObj.toString());
}
/**
* 初始化comparator比较器通过构造函数,使用ConcurrentSkipListMap的key
* @Param
*/
@Test
public void testConstruct1()throws Exception{
Comparator comparator=((o1, o2) -> Integer.parseInt(o1.toString())+Integer.parseInt(o2.toString()));
ConcurrentSkipListSet testObj=new ConcurrentSkipListSet(comparator);
System.out.println(testObj.toString());
}
/**
*初始化skipListSet,并将参数Collection中的值对放入新的skipListSet中,使用ConcurrentSkipListMap的key
* @Param
*/
@Test
public void testConstruct2()throws Exception{
Set<Integer> set=new HashSet<>();
set.add(123);
ConcurrentSkipListSet testObj=new ConcurrentSkipListSet(set);
System.out.println(testObj.toString());
}
/**
*通过sortedSet初始化ConcurrentSkipListSet,并将sortedSet中的元素放入新ConcurrentSkipListSet,使用ConcurrentSkipListMap的key
* @Param
*/
@Test
public void testConstruct3()throws Exception{
Set<Integer> sortedSet=new ConcurrentSkipListSet<>();
sortedSet.add(1);
sortedSet.add(2);
sortedSet.add(4);
sortedSet.add(3);
ConcurrentSkipListSet testObj=new ConcurrentSkipListSet(sortedSet);
System.out.println(testObj.toString());
}
/**
* 浅拷贝
* @Param
*/
@Test
public void testClone()throws Exception{
ConcurrentSkipListSet testObj=new ConcurrentSkipListSet<>();
testObj.add(213);
ConcurrentSkipListSet set2=testObj.clone();
System.out.println(testObj.hashCode());
System.out.println(set2.hashCode());
}
/**
* map中node数量
* @Param
*/
@Test
public void testSize()throws Exception{
ConcurrentSkipListSet<Integer> set=new ConcurrentSkipListSet<>();
set.add