/**
* 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 collectionList,collectionSet,collectionQueue;
/**
* 创建集合的三种子类型
*/
@Before
public void before(){
//创建List Set Queue 三种子Collection类型 的实现类
collectionList=new ArrayList();
collectionSet=new TreeSet();
collectionQueue=new PriorityQueue();
//向三种子类型填充数据
initCollection(collectionList);
initCollection(collectionSet);
initCollection(collectionQueue);
}
/**
* 向集合中填充数据
* @param collectionList2
*/
private void initCollection(Collection collection) {
// TODO Auto-generated method stub
Collection 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 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 collection) {
// TODO Auto-generated method stub
Collection 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 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 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 linkedList,arrayDeque,priorityQueue;
@Before
public void before(){
linkedList=new LinkedList();
arrayDeque=new ArrayDeque();
priorityQueue=new PriorityQueue(20,new IntegerComparator());
//初始化
initQueue(linkedList);
initQueue(arrayDeque);
initQueue(priorityQueue);
}
/**
* 初始化队列
* @param queue
*/
private void initQueue(Queue queue) {
// TODO Auto-generated method stub
Collection 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 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 linkedListCopy=(LinkedList) 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 arrayList,vectorList,stackList,linkedList;
/**
* 构建List的四种子类
*/
@Before
public void before(){
arrayList=new ArrayList();
vectorList=new Vector();
stackList=new Stack();
linkedList=new LinkedList();
initList(arrayList);
initList(vectorList);
initList(stackList);
initList(linkedList);
}
/**
* 向列表中填充数据
* @param list
*/
private void initList(List list) {
// TODO Auto-generated method stub
List 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 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 linkedListCopy=(LinkedList) 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 stack=(Stack) 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 enumSet;
Set hashSet,linkedHashSet,treeSet;
Set treeSetComparator;
@Before
public void before(){
//创建四种Set的子类型
enumSet=EnumSet.of(Operation.ADD, Operation.MULTI,Operation.ADD);
hashSet=new HashSet();
linkedHashSet=new LinkedHashSet();
treeSet=new TreeSet();
//使用比较器构建TreeSet 按照比较器的方式进行排列Set中的元素
StringIntegerComparator comparator=new StringIntegerComparator();
treeSetComparator=new TreeSet(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 set) {
// TODO Auto-generated method stub
Collection 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 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 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 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 treeSetCopy=(TreeSet) treeSet;
//集合中维持的元素 2个方法
System.out.print("集合中维持的元素\t");
iteratorSet(treeSetCopy);
System.out.print("集合中倒序元素\t");
iteratorSet(treeSetCopy.descendingSet());
Iterator iterator=treeSetCopy.descendingIterator();
//截取部分集合 3个方法
System.out.println("截取小于100的集合\t");
SortedSet 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 hashMap,linkedHashMap,identityHashMap,weakHashMap,hashtable,treeMap;
EnumMap enumMap;
Properties properties;
@Before
public void before() throws IOException{
//创建对象
hashMap=new HashMap();
linkedHashMap=new LinkedHashMap();
identityHashMap=new IdentityHashMap();
weakHashMap=new WeakHashMap();
hashtable=new Hashtable();
properties=new Properties();
treeMap=new TreeMap();
enumMap=new EnumMap(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 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 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> sets=properties2.entrySet();
System.out.print(sets.getClass().getName()+"\t");
for (Entry
枚举
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 {
@Override
public int compare(Integer o1, Integer o2) {
// TODO Auto-generated method stub
if(o1>o2) return 1;
if(o1
package com.undergrowth.collection;
import java.util.Comparator;
/**
* 用于将String类型的数据转换为Integer 进行比较
* @author Administrator
*
*/
public class StringIntegerComparator implements Comparator {
@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)
技术的研究 真的无止境啊 还需努力啊 嘎嘎