/**
* Collection测试
*
*
* PriorityQueue--迭代时,不保障元素的迭代顺序
*
* equals 与 hashCode 的区别
* 1、If equal, then same hash codes too.
2、Same hash codes no guarantee of being equal.
不同数据类型生成的hashcode值不一样
如何重写equals与hashCode方法 依据具体的需求而定
* @author u1
*
*/
/**
* List测试
* LinkedList--允许为空,具有双端队列和列表功能
* ArrayList--数组大小可变的列表
* Vector--数组大小可随着添加或者删除元素变化
* Stack--堆栈--先入后出
* @author u1
*
*/
/**
* Map测试
* HashMap--key-value,迭代时,不保障元素的顺序
* LinkedHashMap--使用双端链表连接元素,迭代式,保障元素的顺序
* IdentityHashMap--比较key的时候,使用引用相等判断替代对象相等
* WeakHashMap--当key不在使用时,移除其关联的对象
* Hashtable--任何非空的对象都能够作为key或者value
* Properties--能够从流中加载或者保存到流中
* TreeMap--迭代时,保证元素的顺序,同时元素也是按照顺序排放的
* EnumMap--使用枚举类型作为key
* @author Administrator
*
*/
/**
* Queue--队列
* PriorityQueue--迭代时,不保障元素的迭代顺序
* @author Administrator
*
*/
/**
* Set集合测试
* 集合中的每一个元素都只能存在一份
* HashSet--迭代时,不能保证元素的顺序
* LinkedHashSet--迭代时,保障元素的顺序按照它们插入的顺序
* TreeSet--迭代时,保障元素的顺序按照它们插入的顺序,同时在集合内部对元素进行升序排序
* EnumSet--枚举类型的集合
* @author Administrator
*
*/
用PD花了点时间 画了个Collection的框架图
Collection--集合测试
package com.undergrowth.collection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.PriorityQueue;
import java.util.TreeSet;
import org.junit.Before;
import org.junit.Test;
/**
* Collection测试
*
*
* PriorityQueue--迭代时,不保障元素的迭代顺序
*
* equals 与 hashCode 的区别
* 1、If equal, then same hash codes too.
2、Same hash codes no guarantee of being equal.
不同数据类型生成的hashcode值不一样
如何重写equals与hashCode方法 依据具体的需求而定
* @author u1
*
*/
public class CollectionTest {
/**
* 集合的三种子类型
*/
Collection<Integer> collectionList,collectionSet,collectionQueue;
/**
* 创建集合的三种子类型
*/
@Before
public void before(){
//创建List Set Queue 三种子Collection类型 的实现类
collectionList=new ArrayList<Integer>();
collectionSet=new TreeSet<Integer>();
collectionQueue=new PriorityQueue<Integer>();
//向三种子类型填充数据
initCollection(collectionList);
initCollection(collectionSet);
initCollection(collectionQueue);
}
/**
* 向集合中填充数据
* @param collectionList2
*/
private void initCollection(Collection<Integer> collection) {
// TODO Auto-generated method stub
Collection<Integer> c=Arrays.asList(13,2,56,100,87,87);
collection.addAll(c);
}
/**
* 迭代三种子类型
*/
@Test
public void testIterator(){
iteratorCollection(collectionList);
iteratorCollection(collectionSet);
iteratorCollection(collectionQueue);
}
/**
* 迭代集合
* @param collectionList2
*/
private void iteratorCollection(Collection<Integer> collection) {
// TODO Auto-generated method stub
System.out.print(collection.getClass().getSimpleName()+"\t");
for (Integer Integer : collection) {
System.out.print(Integer+"\t");
}
System.out.println();
}
/**
* 移除集合中的元素
*/
@Test
public void testRemove(){
//移除前
System.out.println("移除前集合");
testIterator();
removeCollection(collectionList);
removeCollection(collectionSet);
removeCollection(collectionQueue);
//再进行迭代
System.out.println("移除后集合");
testIterator();
}
/**
* 移除集合中的元素
* @param collection
*/
private void removeCollection(Collection<Integer> collection) {
// TODO Auto-generated method stub
Collection<Integer> c=Arrays.asList(2,56,200);
collection.removeAll(c);
}
/**
* 测试集合是否为空
*/
@Test
public void testIsEmpty(){
System.out.println(collectionList.isEmpty());
System.out.println(collectionSet.isEmpty());
System.out.println(collectionQueue.isEmpty());
}
/**
* 测试集合是否包含元素
*/
@Test
public void testContains(){
Collection<Integer> c=Arrays.asList(100);
System.out.println(collectionList.containsAll(c));
System.out.println(collectionSet.containsAll(c));
System.out.println(collectionQueue.containsAll(c));
}
/**
* 测试集合转换为数组
*/
@Test
public void testToArray(){
Integer[] a=new Integer[collectionList.size()];
System.out.println(Arrays.toString(collectionList.toArray(a)));
a=new Integer[collectionSet.size()];
System.out.println(Arrays.toString(collectionSet.toArray(a)));
a=new Integer[collectionQueue.size()];
System.out.println(Arrays.toString(collectionQueue.toArray(a)));
}
/**
* 测试清除集合
*/
@Test
public void testClear(){
//清除前
System.out.println("清除前");
testIterator();
collectionList.clear();
collectionSet.clear();
collectionQueue.clear();
//清除后
System.out.println("清除后");
testIterator();
}
/**
* 测试保留集合
*/
@Test
public void testRetain(){
//保留前
System.out.println("保留前");
testIterator();
Collection<Integer> c=Arrays.asList(100);
collectionList.retainAll(c);
collectionSet.retainAll(c);
collectionQueue.retainAll(c);
//保留后
System.out.println("保留后");
testIterator();
}
}
上面那张图 主要分四块
Queue、Set、List、Map
Queue --队列
队列测试代码
package com.undergrowth.collection;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import org.junit.Before;
import org.junit.Test;
/**
* Queue--队列
* PriorityQueue--迭代时,不保障元素的迭代顺序
* @author Administrator
*
*/
public class QueueTest {
Queue<Integer> linkedList,arrayDeque,priorityQueue;
@Before
public void before(){
linkedList=new LinkedList<Integer>();
arrayDeque=new ArrayDeque<Integer>();
priorityQueue=new PriorityQueue<Integer>(20,new IntegerComparator());
//初始化
initQueue(linkedList);
initQueue(arrayDeque);
initQueue(priorityQueue);
}
/**
* 初始化队列
* @param queue
*/
private void initQueue(Queue<Integer> queue) {
// TODO Auto-generated method stub
Collection<Integer> c=Arrays.asList(12,34,1,11,789,65);
queue.addAll(c);
}
/**
* 迭代Queue元素
*/
@Test
public void testQueue(){
iteratorQueue(linkedList);
iteratorQueue(arrayDeque);
iteratorQueue(priorityQueue);
}
/**
* 迭代队列元素
* @param queue
*/
private void iteratorQueue(Queue<Integer> queue) {
// TODO Auto-generated method stub
System.out.print(queue.getClass().getName()+"\t");
for (Integer integer : queue) {
System.out.print(integer+"\t");
}
System.out.println();
}
/**
* 测试LinkedList的Deque特性
*/
@Test
public void testLinkedList(){
LinkedList<Integer> linkedListCopy=(LinkedList<Integer>) linkedList;
System.out.println("查看队列头元素,不删除元素:"+linkedListCopy.peek());
System.out.println("查看队列头元素,不删除元素:"+linkedListCopy.peekFirst());
System.out.println("查看队列尾元素,不删除元素:"+linkedListCopy.peekLast());
iteratorQueue(linkedListCopy);
System.out.println("查看队列头元素,删除元素:"+linkedListCopy.poll());
System.out.println("查看队列头元素,删除元素:"+linkedListCopy.pollFirst());
System.out.println("查看队列尾元素,删除元素:"+linkedListCopy.pollLast());
iteratorQueue(linkedListCopy);
System.out.println("添加对头元素"+linkedListCopy.offerFirst(300));
System.out.println("添加对尾元素"+linkedListCopy.offer(400));
System.out.println("添加对尾元素"+linkedListCopy.offerLast(500));
iteratorQueue(linkedListCopy);
}
}
List--列表
列表代码
package com.undergrowth.collection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
import java.util.Vector;
import org.junit.Before;
import org.junit.Test;
/**
* List测试
* LinkedList--允许为空,具有双端队列和列表功能
* ArrayList--数组大小可变的列表
* Vector--数组大小可随着添加或者删除元素变化
* Stack--堆栈--先入后出
* @author u1
*
*/
public class ListTest {
List<Integer> arrayList,vectorList,stackList,linkedList;
/**
* 构建List的四种子类
*/
@Before
public void before(){
arrayList=new ArrayList<Integer>();
vectorList=new Vector<Integer>();
stackList=new Stack<Integer>();
linkedList=new LinkedList<Integer>();
initList(arrayList);
initList(vectorList);
initList(stackList);
initList(linkedList);
}
/**
* 向列表中填充数据
* @param list
*/
private void initList(List<Integer> list) {
// TODO Auto-generated method stub
List<Integer> c=Arrays.asList(13,2,56,100,87,87);
list.addAll(c);
}
/**
* 测试迭代元素
*/
@Test
public void testIterator(){
iteratorList(arrayList);
iteratorList(vectorList);
iteratorList(stackList);
iteratorList(linkedList);
}
/**
* 迭代元素
* @param list
*/
private void iteratorList(List<Integer> list) {
// TODO Auto-generated method stub
System.out.print(list.getClass().getName()+"\t");
for (Integer integer : list) {
System.out.print(integer+"\t");
}
System.out.println();
}
/**
* 测试set与get方法
*/
@Test
public void testGetSet(){
System.out.println("调用set方法之前");
iteratorList(arrayList);
//设置索引值为0的元素的值为10000
arrayList.set(0, 10000);
System.out.println("调用set方法之后");
iteratorList(arrayList);
System.out.println("最后一个元素为:"+arrayList.get(arrayList.size()-1));
}
/**
* 测试subList方法
*/
@Test
public void testSubList(){
iteratorList(vectorList.subList(0, vectorList.size()-2));
}
/**
* 测试indexOf
*/
@Test
public void testIndexOf(){
System.out.println("100这个元素的索引为:"+stackList.indexOf(100));;
}
/**
* 测试LinkedList的Deque特性
*/
@Test
public void testLinkedList(){
LinkedList<Integer> linkedListCopy=(LinkedList<Integer>) linkedList;
System.out.println("查看队列头元素,不删除元素:"+linkedListCopy.peek());
System.out.println("查看队列头元素,不删除元素:"+linkedListCopy.peekFirst());
System.out.println("查看队列尾元素,不删除元素:"+linkedListCopy.peekLast());
iteratorList(linkedListCopy);
System.out.println("查看队列头元素,删除元素:"+linkedListCopy.poll());
System.out.println("查看队列头元素,删除元素:"+linkedListCopy.pollFirst());
System.out.println("查看队列尾元素,删除元素:"+linkedListCopy.pollLast());
iteratorList(linkedListCopy);
System.out.println("添加对头元素"+linkedListCopy.offerFirst(300));
System.out.println("添加对尾元素"+linkedListCopy.offer(400));
System.out.println("添加对尾元素"+linkedListCopy.offerLast(500));
iteratorList(linkedListCopy);
}
/**
* 测试Stack的堆栈特性
*/
@Test
public void testStack(){
Stack<Integer> stack=(Stack<Integer>) stackList;
iteratorList(stack);
System.out.println("查看堆栈出栈,不删除元素:"+stack.peek());
System.out.println("查看堆栈出栈,删除元素:"+stack.pop());
iteratorList(stack);
}
}
Set--集合
集合测试代码
package com.undergrowth.collection;
import java.util.Arrays;
import java.util.Collection;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import org.junit.Before;
import org.junit.Test;
/**
* Set集合测试
* 集合中的每一个元素都只能存在一份
* HashSet--迭代时,不能保证元素的顺序
* LinkedHashSet--迭代时,保障元素的顺序按照它们插入的顺序
* TreeSet--迭代时,保障元素的顺序按照它们插入的顺序,同时在集合内部对元素进行升序排序
* EnumSet--枚举类型的集合
* @author Administrator
*
*/
public class SetTest {
Set<Operation> enumSet;
Set<Integer> hashSet,linkedHashSet,treeSet;
Set<String> treeSetComparator;
@Before
public void before(){
//创建四种Set的子类型
enumSet=EnumSet.of(Operation.ADD, Operation.MULTI,Operation.ADD);
hashSet=new HashSet<Integer>();
linkedHashSet=new LinkedHashSet<Integer>();
treeSet=new TreeSet<Integer>();
//使用比较器构建TreeSet 按照比较器的方式进行排列Set中的元素
StringIntegerComparator comparator=new StringIntegerComparator();
treeSetComparator=new TreeSet<String>(comparator);
treeSetComparator.addAll(Arrays.asList("100","45","78","120","87","89","87"));
//初始化Set值
initSet(hashSet);
initSet(linkedHashSet);
initSet(treeSet);
}
/**
* 向Set种填充数据
* @param hashSet2
*/
private void initSet(Set<Integer> set) {
// TODO Auto-generated method stub
Collection<Integer> c=Arrays.asList(100,45,78,120,87,89,87);
set.addAll(c);
}
/**
* 迭代元素
*/
@Test
public void testIterator(){
iteratorSet(hashSet);
iteratorSet(linkedHashSet);
iteratorSet(treeSet);
iteratorEnumSet(enumSet);
iteratorTreeSet(treeSetComparator);
}
/**
* 迭代TreeSet
* @param treeSetComparator2
*/
private void iteratorTreeSet(Set<String> treeSetComparator2) {
// TODO Auto-generated method stub
System.out.print(treeSetComparator2.getClass().getName()+"\t");
for (String string : treeSetComparator) {
System.out.print(string+"\t");
}
System.out.println();
}
/**
* 迭代集合
* @param set
*/
private void iteratorSet(Set<Integer> set) {
// TODO Auto-generated method stub
System.out.print(set.getClass().getName()+"\t");
for (Integer integer : set) {
System.out.print(integer+"\t");
}
System.out.println();
}
/**
* 迭代EnumSet
* @param enumSet
*/
private void iteratorEnumSet(Set<Operation> enumSet){
System.out.print(enumSet.getClass().getName()+"\t");
for (Operation operation : enumSet) {
System.out.print(operation.ordinal()+":"+operation.getName()+"\t");
}
System.out.println();
}
/**
* 测试NavigableSet的11个特有方法
*/
@Test
public void testNavigableSet(){
TreeSet<Integer> treeSetCopy=(TreeSet<Integer>) treeSet;
//集合中维持的元素 2个方法
System.out.print("集合中维持的元素\t");
iteratorSet(treeSetCopy);
System.out.print("集合中倒序元素\t");
iteratorSet(treeSetCopy.descendingSet());
Iterator<Integer> iterator=treeSetCopy.descendingIterator();
//截取部分集合 3个方法
System.out.println("截取小于100的集合\t");
SortedSet<Integer> sortedSet=treeSetCopy.headSet(100);
iteratorSet(sortedSet);
System.out.println("截取大于100的集合,包括100这个元素\t");
sortedSet=treeSetCopy.tailSet(100,true);
iteratorSet(sortedSet);
System.out.println("截取制定范围内的集合\t");
sortedSet=treeSetCopy.subSet(87, 120);
iteratorSet(sortedSet);
//截取单个元素 4个方法
System.out.println("截取大于等于100的元素\t");
System.out.println(treeSetCopy.ceiling(100));
System.out.println("截取小于等于100的元素\t");
System.out.println(treeSetCopy.floor(100));
System.out.println("截取大于100的元素\t");
System.out.println(treeSetCopy.higher(100));
System.out.println("截取小于100的元素\t");
System.out.println(treeSetCopy.lower(100));
//查看并删除元素
iteratorSet(treeSetCopy);
System.out.println("移除集合第一个元素:"+treeSetCopy.pollFirst());
System.out.println("移除集合最后一个元素:"+treeSetCopy.pollLast());
iteratorSet(treeSetCopy);
}
}
Map--key-value 键值对
key-value 测试代码
package com.undergrowth.collection;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collection;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.IdentityHashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.NavigableMap;
import java.util.NavigableSet;
import java.util.Properties;
import java.util.Set;
import java.util.TreeMap;
import java.util.WeakHashMap;
import org.junit.Before;
import org.junit.Test;
/**
* Map测试
* HashMap--key-value,迭代时,不保障元素的顺序
* LinkedHashMap--使用双端链表连接元素,迭代式,保障元素的顺序
* IdentityHashMap--比较key的时候,使用引用相等判断替代对象相等
* WeakHashMap--当key不在使用时,移除其关联的对象
* Hashtable--任何非空的对象都能够作为key或者value
* Properties--能够从流中加载或者保存到流中
* TreeMap--迭代时,保证元素的顺序,同时元素也是按照顺序排放的
* EnumMap--使用枚举类型作为key
* @author Administrator
*
*/
public class MapTest {
Map<String,String> hashMap,linkedHashMap,identityHashMap,weakHashMap,hashtable,treeMap;
EnumMap<Operation, String> enumMap;
Properties properties;
@Before
public void before() throws IOException{
//创建对象
hashMap=new HashMap<String, String>();
linkedHashMap=new LinkedHashMap<String, String>();
identityHashMap=new IdentityHashMap<String, String>();
weakHashMap=new WeakHashMap<String, String>();
hashtable=new Hashtable<String, String>();
properties=new Properties();
treeMap=new TreeMap<String, String>();
enumMap=new EnumMap<Operation, String>(Operation.class);
//进行初始化
initiMap(hashMap);
initiMap(linkedHashMap);
initiMap(identityHashMap);
initiMap(weakHashMap);
initiMap(hashtable);
initiMap(treeMap);
//初始化Properties
InputStream isInputStream=ClassLoader.getSystemResourceAsStream("test.properties");
properties.load(isInputStream);
//初始化EnumMap
initEnumMap(enumMap);
}
/**
* 初始化EnumMap
* @param enumMap
*/
private void initEnumMap(EnumMap<Operation, String> enumMap) {
// TODO Auto-generated method stub
enumMap.put(Operation.ADD, "+");
enumMap.put(Operation.MINUS, "-");
enumMap.put(Operation.MULTI, "*");
enumMap.put(Operation.DIVIDE, "/");
}
/**
* 初始化Map
* @param map
*/
private void initiMap(Map<String, String> map) {
// TODO Auto-generated method stub
map.put("q", "qq");
map.put("w", "ww");
map.put("e", "ee");
map.put("r", "rr");
map.put("t", "tt");
map.put("y", "yy");
}
/**
* 迭代Map
*/
@Test
public void testIteratorMap(){
iteratorMap(hashMap);
iteratorMap(linkedHashMap);
iteratorMap(identityHashMap);
iteratorMap(weakHashMap);
iteratorMap(hashtable);
iteratorMap(treeMap);
iteratorProperties(properties);
}
/**
* 迭代Properties
* @param properties2
*/
private void iteratorProperties(Properties properties2) {
// TODO Auto-generated method stub
Set<Entry<Object, Object>> sets=properties2.entrySet();
System.out.print(sets.getClass().getName()+"\t");
for (Entry<Object, Object> entry : sets) {
System.out.print(entry.getKey()+":"+entry.getValue()+"\t");
}
System.out.println();
}
/**
* 迭代Map
* @param map
*/
private void iteratorMap(Map<String, String> map) {
// TODO Auto-generated method stub
System.out.print(map.getClass().getName()+"\t");
for (Map.Entry<String, String> entry:map.entrySet()) {
System.out.print(entry.getKey()+":"+entry.getValue()+"\t");
}
System.out.println();
}
/**
* 迭代Set
* @param keySet
*/
private void iteratorSet(NavigableSet<String> keySet) {
// TODO Auto-generated method stub
System.out.print(keySet.getClass().getName()+"\t");
for (String string : keySet) {
System.out.print(string+"\t");
}
System.out.println();
}
/**
* 测试可导航的Map
*/
@Test
public void testNavigableMap(){
iteratorMap(treeMap);
TreeMap<String, String> treeMapCopy=(TreeMap<String, String>) treeMap;
//遍历降序的Map
iteratorMap(treeMapCopy.descendingMap());
NavigableSet<String> keySet=treeMapCopy.descendingKeySet();
//遍历降序的key
iteratorSet(keySet);
//截取子Map
System.out.print("截取小于t的值得Map子集合"+"\t");
iteratorMap(treeMapCopy.headMap("t"));
System.out.print("截取大于t的值得Map子集合"+"\t");
iteratorMap(treeMapCopy.tailMap("t"));
System.out.print("截取键的范围[t,y)之间的Map子集合"+"\t");
iteratorMap(treeMapCopy.subMap("t","y"));
//截取key
System.out.print("截取大于等于t键值的key"+"\t");
System.out.println(treeMapCopy.ceilingKey("t"));
System.out.print("截取小于等于t键值的key"+"\t");
System.out.println(treeMapCopy.floorKey("t"));
System.out.print("截取大于t键值的key"+"\t");
System.out.println(treeMapCopy.higherKey("t"));
System.out.print("截取小于t键值的key"+"\t");
System.out.println(treeMapCopy.lowerKey("t"));
//截取Entry
System.out.print("截取大于等于t键值的Entry"+"\t");
System.out.println(treeMapCopy.ceilingEntry("t"));
System.out.print("截取小于等于t键值的Entry"+"\t");
System.out.println(treeMapCopy.floorEntry("t"));
System.out.print("截取大于t键值的Entry"+"\t");
System.out.println(treeMapCopy.higherEntry("t"));
System.out.print("截取小于t键值的Entry"+"\t");
System.out.println(treeMapCopy.lowerEntry("t"));
//查看并移除Entry
System.out.println("查看并移除第一个Entry:"+treeMapCopy.pollFirstEntry());
System.out.println("查看并移除最后一个Entry:"+treeMapCopy.pollLastEntry());
iteratorMap(treeMapCopy);
}
}
枚举
package com.undergrowth.collection;
public enum Operation {
ADD("+"),MINUS("-"),MULTI("*"),DIVIDE("/");
private String name;
Operation(String name){
this.name=name;
}
public String getName() {
return name;
}
}
整数比较器
package com.undergrowth.collection;
import java.util.Comparator;
public class IntegerComparator implements Comparator<Integer> {
@Override
public int compare(Integer o1, Integer o2) {
// TODO Auto-generated method stub
if(o1>o2) return 1;
if(o1<o2) return -1;
return 0;
}
}
字符串比较器
package com.undergrowth.collection;
import java.util.Comparator;
/**
* 用于将String类型的数据转换为Integer 进行比较
* @author Administrator
*
*/
public class StringIntegerComparator implements Comparator<String> {
@Override
public int compare(String o1, String o2) {
// TODO Auto-generated method stub
if(Integer.valueOf(o1)>Integer.valueOf(o2)) return 1;
if(Integer.valueOf(o1)<Integer.valueOf(o2)) return -1;
return 0;
}
}
技术的研究 真的无止境啊 还需努力啊 嘎嘎