day12day15集合文档格式.docx
《day12day15集合文档格式.docx》由会员分享,可在线阅读,更多相关《day12day15集合文档格式.docx(94页珍藏版)》请在冰点文库上搜索。
---|Collection:
单列集合
---|List:
有存储顺序,可重复
---|ArrayList:
数组实现,查找快,增删慢
由于是数组实现,在增和删的时候会牵扯到数组
增容,以及拷贝元素.所以慢。
数组是可以直接
按索引查找,所以查找时较快
---|LinkedList:
链表实现,增删快,查找慢
由于链表实现,增加时只要让前一个元素记住自
己就可以,删除时让前一个元素记住后一个元
素,后一个元素记住前一个元素.这样的增删效
率较高但查询时需要一个一个的遍历,所以效率
较低
---|Vector:
和ArrayList原理相同,但线程安全,效率略低
和ArrayList实现方式相同,但考虑了线程安全问
题,所以效率略低
---|Set:
无存储顺序,不可重复
---|HashSet
---|TreeSet
---|LinkedHashSet
---|Map:
键值对
---|HashMap
---|TreeMap
---|HashTable
---|LinkedHashMap
为什么出现这么多集合容器,因为每一个容器对数据的存储方式不同,这种存储方式称之为数据结构(datastructure)
注意集合和数组中存放的都是对象的引用。
1.4.什么时候该使用什么样的集合
Collection
我们需要保存若干个对象的时候使用集合。
List
如果我们需要保留存储顺序,并且保留重复元素,使用List.
如果查询较多,那么使用ArrayList
如果存取较多,那么使用LinkedList
如果需要线程安全,那么使用Vector
Set
如果我们不需要保留存储顺序,并且需要去掉重复元素,使用Set.
如果我们需要将元素排序,那么使用TreeSet
如果我们不需要排序,使用HashSet,HashSet比
TreeSet效率高.
如果我们需要保留存储顺序,又要过滤重复元素,那么使用LinkedHashSet
2.集合类(Collection)
Collection接口有两个子接口:
List(链表|线性表)
Set(集)
特点:
Collection中描述的是集合共有的功能(CRUD)
List可存放重复元素,元素存取是有序的
Set不可以存放重复元素,元素存取是无序的
java.util.Collection
---|Collection描述所有接口的共性
----|List接口可以有重复元素的集合
----|Set接口不可以有重复元素的集合
学习集合对象
学习Collection中的共性方法,多个容器在不断向上抽取就出现了该体系。
发现Collection接口中具有所有容器都具备的共性方法。
查阅API时,就可以直接看该接口中的方法。
并创建其子类对象对集合进行基本应用。
当要使用集合对象中特有的方法,在查看子类具体内容。
查看api文档Collection在在java.util中(注意是大写Collection)
注意在现阶段遇到的ET之类的类型,需要暂时理解为object因为涉及到了泛型.
3:
创建集合对象,使用Collection中的List的具体实现类ArrayList
1:
Collectioncoll=newArraylist();
2.1.Collection接口的共性方法
增加:
1:
add()将指定对象存储到容器中
add方法的参数类型是Object便于接收任意对象
2:
addAll()将指定集合中的元素添加到调用该方法和集合中
删除:
3:
remove()将指定的对象从集合中删除
4:
removeAll()将指定集合中的元素删除
修改
5:
clear()清空集合中的所有元素
判断
6:
isEmpty()判断集合是否为空
7:
contains()判断集合何中是否包含指定对象
8:
containsAll()判断集合中是否包含指定集合
使用equals()判断两个对象是否相等
获取:
9:
intsize()返回集合容器的大小
转成数组10:
toArray()集合转换数组
2.1.1.增加:
publicstaticvoidmain(String[]args){
Collectionlist=newArrayList();
//增加:
list.add("
计算机网络"
);
现代操作系统"
java编程思想"
System.out.println(list);
//[计算机网络,现代操作系统,java编程思想]
//增加2将list容器元素添加到list2容器中
Collectionlist2=newArrayList();
list2.add("
java核心技术"
list2.addAll(list);
java语言程序设计"
System.out.println(list2);
//[java核心技术,计算机网络,现代操作系统,java编程思想,java语言程序设计]
}
2.1.2.删除:
//删除1remove
booleanremove=list2.remove("
System.out.println(remove);
//true
//
//删除2removeAll()将list中的元素删除
booleanremoveAll=list2.removeAll(list);
System.out.println(removeAll);
//true
//[java语言程序设计]
2.1.3.修改:
//修改clear()清空集合中的所有元素
list.clear();
//[]
2.1.4.判断:
booleanempty=list.isEmpty();
System.out.println(empty);
//false
booleancontains=list.contains("
System.out.println(contains);
//true
水许传"
booleancontainsAll=list.containsAll(list2);
System.out.println(containsAll);
2.1.5.获取:
//获取集合容器的大小
intsize=list.size();
System.out.println(size);
2.1.6.练习:
集合中添加自定义对象
该案例要求完成使用集合:
//创建集合对象
Collectioncoll=newArrayList();
//创建Person对象
Personp1=newPerson("
jack"
25);
Personp2=newPerson("
rose"
22);
Personp3=newPerson("
lucy"
20);
Personp4=newPerson("
//集合中添加一些Perosn
//删除指定Person
//删除所有Person
//判断容器中是否还有Person
//判断容器中是否包含指定Person
//获取容器中Person的个数
//将容器变为数组,遍历除所有Person
分析:
Person类
姓名和年龄
2:
重写hashCode和equals方法
如果不重写,调用Object类的equals方法,判断内存地址,为false
1:
如果是Person类对象,并且姓名和年龄相同就返回true
如果不重写,调用父类hashCode方法
如果equals方法相同,那么hashCode也要相同,需要重写hashCode方法
重写toString方法
不重写,直接调用Object类的toString方法,打印该对象的内存地址
classPerson{
privateStringname;
privateintage;
publicPerson(){
publicPerson(Stringname,intage){
this.name=name;
this.age=age;
@Override
publicinthashCode(){
returnthis.name.hashCode()+age;
publicbooleanequals(Objectobj){
if(!
(objinstanceofPerson)){
returnfalse;
}
Personp=(Person)obj;
returnthis.name.equals(p.name)&
&
this.age==p.age;
@Override
publicStringtoString(){
return"
Person:
name="
+name+"
age="
+age;
}
张三"
19);
李四"
王五"
18);
list.add(p1);
list.add(p2);
list.add(p3);
//isEmpty()判断集合是否为空
//返回集合容器的大小
//contains()判断集合何中是否包含指定对象
booleancontains=list.contains(p1);
//remove();
将指定的对象从集合中删除
list.remove(p1);
//clear()清空集合中的所有元素
//使用集合存储自定义对象2
classBook{
privatedoubleprice;
publicBook(){
publicBook(Stringname,doubleprice){
this.price=price;
publicStringgetName(){
returnname;
publicvoidsetName(Stringname){
publicdoublegetPrice(){
returnprice;
publicvoidsetPrice(doubleprice){
return(int)(this.name.hashCode()+price);
(objinstanceofBook)){
Bookbook=(Book)obj;
returnthis.name.equals(book.name)&
this.price==book.price;
book:
@name:
"
+this.name+"
price:
+this.price;
publicclassDemo1{
publicstaticvoidmain(String[]args){
Collectioncol=newArrayList();
col.add(newBook("
thinkinjava"
100));
col.add(newBook("
corejava"
200));
System.out.println(col);
2.2.List
---|Iterable接口
Iteratoriterator()
----|Collection接口
------|List接口元素可以重复,允许在指定位置插入元素,并通过索
引来访问元素
2.2.1.List集合特有方法
增加
voidadd(intindex,Eelement)指定位置添加元素
booleanaddAll(intindex,Collectionc)指定位置添加集合
删除
Eremove(intindex)删除指定位置元素
Eset(intindex,Eelement)返回的是需要替换的集合中的元素
查找:
Eget(intindex)注意:
IndexOutOfBoundsException
intindexOf(Objecto)//找不到返回-1
lastIndexOf(Objecto)
5:
求子集合
List<
E>
subList(intfromIndex,inttoIndex)//不包含toIndex
2.2.1.1.增加
Listlist=newArrayList();
//add,在0角标位置添加一本书
list.add(0,"
舒克和贝塔"
//在list2集合的1角标位置添加list集合元素
Listlist2=newArrayList();
史记"
资治通鉴"
全球通史"
booleanaddAll=list2.addAll(1,list);
System.out.println(addAll);
//trueSystem.out.println(list2);
2.2.1.2.删除
//删除0角标元素
Objectremove=list.remove(0);
2.2.1.3.修改:
//修改2角标位置的书,返回的原来2角标位置的书
Objectset=list.set(2,"
边城"
System.out.println(set);
//java编程思想
查找
Listlist=newArrayList();
//查找:
Eget(intindex)注意角标越界
Objectset=list.get(list.size()-1);
//java语言程序设计
list.get(list.size());
//IndexOutOfBoundsException
2.2.1.4.查找
publicstati