package com.semovy.test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.SortedSet;
import java.util.TreeSet;
/**
*
* @author semovy@gmail.com
* Collection
* | |
* List Set______________________________________________
* |__________ | | |
* | | | | |
* ArrayList LinkedList HashSet LinkedHashSet SortedSet
* |
* TreeSet
*/
public class CollectionTest {
/* Set容器中的对象是唯一的,所以加入Set容器的对象,必须重新equals()方法.作为唯一性的标识
* HashSet的排序规则是利用HashTable,所以HashSet容器的对象必须重新定义hashCode()方法
* 利用hashCode()方法,可以让快速找到容器中的对象,在比较两加入到容器中的对象是否相同时,会比较
* hashCode()方法返回是否相同,如果相同,则再利用equals()方法比较,如果两者都相同,则被看作相同的对象
* String 对象的hashCode(),equals已经被重新定义
*/
public static void main(String[] args)
{
//String 对象的hashCode(),equals已经被重新定义
//结果重复的内容对象被除去,并按按照hashCode()升序排序
Collection<String> colStr = new HashSet<String>();
colStr.add("ArrayList");
colStr.add("LinkedList");
colStr.add("HashSet");
colStr.add("LinkedHashSet");
colStr.add("LinkedSortedSet");
colStr.add("ArrayList");
CollectionTest ct = new CollectionTest();
ct.display(colStr);
//自定义类Employee.如果没有重新定义hashCode(),equals()方法,则不会除去重复内容的对象,不会自动按hashCode()排序
Collection<Employee> colEmp = new HashSet<Employee>();
colEmp.add(new Employee(1,"no.1","semovy","he is a hero."));
colEmp.add(new Employee(2,"no.2","superman_wshm","from Shenzhen China"));
colEmp.add(new Employee(3,"no.3","韦善茂","来自中国"));
colEmp.add(new Employee(4,"no.4","joke","note1"));
colEmp.add(new Employee(3,"no.3","韦善茂","来自中国"));
ct.display(colEmp);
//LinkedHashSet,既去除了内容重复的对象,又按时插入容器时的顺序,被迭代.
Collection<Employee> linkedEmp = new LinkedHashSet<Employee>();
linkedEmp.add(new Employee(1,"no.1","semovy","he is a hero."));
linkedEmp.add(new Employee(2,"no.2","superman_wshm","from Shenzhen China"));
linkedEmp.add(new Employee(3,"no.3","韦善茂","来自中国"));
linkedEmp.add(new Employee(4,"no.4","joke","note1"));
linkedEmp.add(new Employee(3,"no.3","韦善茂","来自中国"));
ct.display(linkedEmp);
//SortedSet接口,可以去除重复,并有比较器.
SortedSet<String> ss = new TreeSet<String>();
ss.add("1");
ss.add("2");
ss.add("3");
ss.add("4");
ss.add("4");
ct.display(ss);
// SortedSet接口,可以去除重复,并有比较器.
SortedSet<Employee> treeEmp = new TreeSet<Employee>(
new Comparator<Employee>()//使用Employee泛型,匿名内部类比较器
{
public int compare(Employee arg0, Employee arg1) {
return arg1.getName().compareTo(arg0.getName());//按照名称比较降序排序
}
}
);
treeEmp.add(new Employee(1,"no.1","semovy","he is a hero."));
treeEmp.add(new Employee(2,"no.2","superman_wshm","from Shenzhen China"));
treeEmp.add(new Employee(3,"no.3","韦善茂","来自中国"));
treeEmp.add(new Employee(4,"no.4","joke","note1"));
treeEmp.add(new Employee(3,"no.3","韦善茂","来自中国"));
ct.display(treeEmp);
//SortedSet接口,可以去除重复,并有比较器.比较器名为comparatorById
ComparatorById comparatorById = new ComparatorById();
SortedSet<Employee> treeEmp1 = new TreeSet<Employee>(comparatorById);
treeEmp1.add(new Employee(1,"no.1","semovy","he is a hero."));
treeEmp1.add(new Employee(2,"no.2","superman_wshm","from Shenzhen China"));
treeEmp1.add(new Employee(3,"no.3","韦善茂","来自中国"));
treeEmp1.add(new Employee(4,"no.4","joke","note1"));
treeEmp1.add(new Employee(3,"no.3","韦善茂","来自中国"));
ct.display(treeEmp1);
System.out.println();
System.out.println(treeEmp1.last().toString());
System.out.println(treeEmp1.first().toString());
//用List接口的实现
//ArrayList.基于数组的实现,添加与删除的效率慢于LinkedList,
//LinkedList基于双向链表的实现 ,添加与删除的效率快于ArrayList,但迭代遍历的效率不如ArrayList
List<Employee> empList = new ArrayList<Employee>();
empList.add(new Employee(1,"no.1","semovy","he is a hero."));
empList.add(new Employee(2,"no.2","superman_wshm","from Shenzhen China"));
empList.add(new Employee(3,"no.3","韦善茂","来自中国"));
empList.add(new Employee(4,"no.4","joke","note1"));
empList.add(new Employee(3,"no.3","韦善茂","来自中国"));
ct.display(empList);
Employee[] arrEmp = empList.toArray(new Employee[0]);
Arrays.sort(arrEmp, comparatorById);//用Arrays.sort静态方法排序,比较器是comparatorById
ct.display(arrEmp);
LinkedList<Employee> linkedListEmp = new LinkedList<Employee>();
linkedListEmp.add(new Employee(1,"no.1","semovy","he is a hero."));
linkedListEmp.add(new Employee(2,"no.2","superman_wshm","from Shenzhen China"));
linkedListEmp.add(new Employee(3,"no.3","韦善茂","来自中国"));
linkedListEmp.add(new Employee(4,"no.4","joke","note1"));
linkedListEmp.add(new Employee(3,"no.3","韦善茂","来自中国"));
ct.display(linkedListEmp);
ListIterator<Employee> ll = linkedListEmp.listIterator();
while(ll.hasNext())
{
Employee em = ll.next();
System.out.println("index:" + ll.nextIndex());
if(em.getId() == 3)
ll.remove();//删除刚跨过的元素
}
ct.display(linkedListEmp);
}
/**
* display all the objects in Collection
* @prama Conllection
*/
public void display(Collection c)//用for循环集合
{
System.out.println();
for(Object obj : c)
System.out.println(obj.toString() + " ");
}
public void display(Object[] arr)//用for循环数组
{
System.out.println();
for(Object obj : arr)
System.out.println(obj.toString() + " ");
}
}
/**
* 比较器接口Comparator 按id.升序
*/
class ComparatorById implements Comparator<Employee>
{
public int compare(Employee e1 ,Employee e2)
{
return e1.getId() - e2.getId();//按照id比较升序排序
}
}
//==============================================================================
结果:
ArrayList
HashSet
LinkedSortedSet
LinkedHashSet
LinkedList
[ ID: 3 , No.: no.3 , Name: 韦善茂 , Note: 来自中国 ]
[ ID: 2 , No.: no.2 , Name: superman_wshm , Note: from Shenzhen China ]
[ ID: 1 , No.: no.1 , Name: semovy , Note: he is a hero. ]
[ ID: 4 , No.: no.4 , Name: joke , Note: note1 ]
[ ID: 1 , No.: no.1 , Name: semovy , Note: he is a hero. ]
[ ID: 2 , No.: no.2 , Name: superman_wshm , Note: from Shenzhen China ]
[ ID: 3 , No.: no.3 , Name: 韦善茂 , Note: 来自中国 ]
[ ID: 4 , No.: no.4 , Name: joke , Note: note1 ]
1
2
3
4
[ ID: 3 , No.: no.3 , Name: 韦善茂 , Note: 来自中国 ]
[ ID: 2 , No.: no.2 , Name: superman_wshm , Note: from Shenzhen China ]
[ ID: 1 , No.: no.1 , Name: semovy , Note: he is a hero. ]
[ ID: 4 , No.: no.4 , Name: joke , Note: note1 ]
[ ID: 1 , No.: no.1 , Name: semovy , Note: he is a hero. ]
[ ID: 2 , No.: no.2 , Name: superman_wshm , Note: from Shenzhen China ]
[ ID: 3 , No.: no.3 , Name: 韦善茂 , Note: 来自中国 ]
[ ID: 4 , No.: no.4 , Name: joke , Note: note1 ]
[ ID: 4 , No.: no.4 , Name: joke , Note: note1 ]
[ ID: 1 , No.: no.1 , Name: semovy , Note: he is a hero. ]
[ ID: 1 , No.: no.1 , Name: semovy , Note: he is a hero. ]
[ ID: 2 , No.: no.2 , Name: superman_wshm , Note: from Shenzhen China ]
[ ID: 3 , No.: no.3 , Name: 韦善茂 , Note: 来自中国 ]
[ ID: 4 , No.: no.4 , Name: joke , Note: note1 ]
[ ID: 3 , No.: no.3 , Name: 韦善茂 , Note: 来自中国 ]
[ ID: 1 , No.: no.1 , Name: semovy , Note: he is a hero. ]
[ ID: 2 , No.: no.2 , Name: superman_wshm , Note: from Shenzhen China ]
[ ID: 3 , No.: no.3 , Name: 韦善茂 , Note: 来自中国 ]
[ ID: 3 , No.: no.3 , Name: 韦善茂 , Note: 来自中国 ]
[ ID: 4 , No.: no.4 , Name: joke , Note: note1 ]
[ ID: 1 , No.: no.1 , Name: semovy , Note: he is a hero. ]
[ ID: 2 , No.: no.2 , Name: superman_wshm , Note: from Shenzhen China ]
[ ID: 3 , No.: no.3 , Name: 韦善茂 , Note: 来自中国 ]
[ ID: 4 , No.: no.4 , Name: joke , Note: note1 ]
[ ID: 3 , No.: no.3 , Name: 韦善茂 , Note: 来自中国 ]
index:1
index:2
index:3
index:3
index:4
[ ID: 1 , No.: no.1 , Name: semovy , Note: he is a hero. ]
[ ID: 2 , No.: no.2 , Name: superman_wshm , Note: from Shenzhen China ]
[ ID: 4 , No.: no.4 , Name: joke , Note: note1 ]