Collection子接口(List/Set/Queue/SortedSet)
2014-07-12 10:42
148 查看
Collection主要的子接口:
List:可以存放重复内容Set:不能存放重复内容,所有重复的内容靠hashCode()和equals()两个方法区分
Queue:队列接口
SortedSet:可以对集合中的数据进行排序
List接口:
总结了List接口的扩展方法,即包含有增删改查方法.List接口常用的子类:
ArrayList:可以直接通过对象的多态性为List接口实例化.Vector:算是元老级的类,使用差别不大
LinkedList:表示链表的操作类,同时实现List接口和Queue接口
Set接口:
Set接口常用的子类:
HashSet:不能存放重复元素,而且采用散列的存储方式,所以没有顺序TreeSet:不能存放重复元素,但对输入的数据进行有序排列(实现了SortedSet接口)
指定排序:
class Person implements Comparable<Person>{ private String name ; private int age ; public Person(String name,int age){ this.name = name ; this.age = age ; } public String toString(){ return "姓名:" + this.name + ";年龄:" + this.age ; } public int compareTo(Person per){ if(this.age>per.age){ return 1 ; }else if(this.age<per.age){ return -1 ; }else{ return this.name.compareTo(per.name) ; // 调用String中的compareTo()方法 } } }; public class TreeSetDemo{ public static void main(String args[]){ Set<Person> allSet = new TreeSet<Person>() ; allSet.add(new Person("张三",30)) ; allSet.add(new Person("李四",31)) ; allSet.add(new Person("王五",32)) ; allSet.add(new Person("王五",32)) ; allSet.add(new Person("王五",32)) ; allSet.add(new Person("赵六",33)) ; allSet.add(new Person("孙七",33)) ; System.out.println(allSet) ; } };
运行结果:
[姓名:张三;年龄:30, 姓名:李四;年龄:31, 姓名:王五;年龄:32, 姓名:孙七;年龄:33, 姓名:赵六;年龄:33]
去重复元素:
class Person{ private String name ; private int age ; public Person(String name,int age){ this.name = name ; this.age = age ; } public boolean equals(Object obj){ // 覆写equals,完成对象比较 if(this==obj){ return true ; } if(!(obj instanceof Person)){ return false ; } Person p = (Person)obj ; // 向下转型 if(this.name.equals(p.name)&&this.age==p.age){ return true ; }else{ return false ; } } public int hashCode(){ return this.name.hashCode() * this.age ; // 定义一个公式 } public String toString(){ return "姓名:" + this.name + ";年龄:" + this.age ; } }; public class RepeatDemo{ public static void main(String args[]){ Set<Person> allSet = new HashSet<Person>() ; allSet.add(new Person("张三",30)) ; allSet.add(new Person("李四",31)) ; allSet.add(new Person("王五",32)) ; allSet.add(new Person("王五",32)) ; allSet.add(new Person("王五",32)) ; allSet.add(new Person("赵六",33)) ; allSet.add(new Person("孙七",33)) ; System.out.println(allSet) ; } };运行结果:
[姓名:李四;年龄:31, 姓名:张三;年龄:30, 姓名:孙七;年龄:33, 姓名:王五;年龄:32, 姓名:赵六;年龄:33]
Queue接口:
队列操作接口SortedSet接口:
相关文章推荐
- Collection子接口(List/Set/Queue/SortedSet)
- Java常见集合框架(二):Iterable、Collection(List、Set、Queue)、AbstractCollection
- HashMap和HashSet的使用,区别。集合,Array、Collection(List/Set/Queue)、Map
- 常用数据结构及复杂度 array、LinkedList、List、Stack、Queue、Dictionary、SortedDictionary、HashSet、SortedSet
- HashMap和HashSet的使用,区别。集合,Array、Collection(List/Set/Queue)、Map
- Collection List Set SortedSet ArrayList LinkedList HashSet LinkedHashSet TreeSet的认识及用法
- 【Java】容器类学习之路(二)Collection详解:List、Set和Queue
- Collection、List、Set、Map、Queue关系图学习笔记2
- Map、Set、List、Collection、Queue、Stack的特点与用法。
- java中List、Map、Set、Collection、Stack、Queue等的使用
- uitl.concurrent研究(八)——超越 Map、Collection、List 和 Set的Queue
- java基础巩固系列(九):持有对象之间的使用与关系(Iterable、Collection、List、Queue、Set、Map、Stack)
- Java集合框架概述:Collection(List, Set, Queue)和Map
- java容器类持有对象:Collection Map List Set Queue
- Java学习笔记(2)----散列集/线性表/队列/集合/图(Set,List,Queue,Collection,Map)
- Java:集合,Array、Collection(List/Set/Queue)、Map的遍历,比如:ArrayList,LinkedList,HashSet,HashMap
- java集合大家族之Collection(List、Queue、Set)
- Java中容器[Collection(List,Set,Queue),Map],迭代器(Iterator)和比较器(Comparator)及列表排序
- collection单列集合子类有list和set,还有迭代器iterator
- Collection、List、Set、Map之间的关系