集合(容器)
1
1. 概述
2. Collection接口和Iterator接口
3. Set
4. List
5. Collections
6. Map
集合(容器)
1.1.1.概述
2
为了使程序方便地存储和操纵数目不固定的一组
数据,JDK类库中提供了Java集合,所有Java集
合类都位于java.util包中。与Java集合不同,Java
集合不能存放基本数据类型,而只能存放对象的
引用。为了表达方便,我们简称对象。
集合(容器)
1.1.2.集合三种类型
3
3
1
1
5 K3v2
K5v1
K2v4
K4v3
4 6
2 7
53
1
Set(集) List(列表) Map(映射)
集合(容器)
1.1.3.集合API
4
集合(容器)
1.1.4.集合(容器)API
5
在Collection接口中定义了存取一组对象的方法,其
子接口Set和List分别定义了存储方式
Set中的数据对象没有顺序且不可以重复
List中的数据对象有顺序且可以重复
Map接口定义了存储“键(key)---值(value)映射
对”的方法
集合(容器)
1.2.1.Collection接口
6
Collection接口所定义的方法
boolean add(Object o)
boolean addAll(Collection> c)
void clear()
boolean contains(Object o)
boolean equals(Object o)
int hashCode()
boolean isEmpty()
Iterator
iterator()
boolean remove(Object o)
boolean removeAll(Collection> c)
int size()
Object[] toArray()
集合(容器)
1.2.2.Collection接口—举例
7
import java.util.*;
public class CollectionDemo{
public static void main(String[] args){
Collection coll = new ArrayList();
coll.add("hell world");
String temp = new String("Bean");
coll.add(temp);
coll.add(new Long(3883238442l));
System.out.println("size="+coll.size());
System.out.println(coll);
}
}
集合(容器)
1.2.2.Collection接口—举例
8
import java.util.*;
public class CollectionSet{
public static void main(String[] args){
Collection coll = new HashSet();
coll.add("hell world");
String temp = new String("Bean");
coll.add(temp);
coll.add(new Long(3883238442l));
System.out.println("size="+coll.size());
//coll.remove(temp);
coll.remove(new String("Bean"));
coll.add(new Integer(232));
coll.remove(new Integer(231));
System.out.println(coll);
}
}
9
容器类对象在调用 remove、contains等方法时需要比较对象是否相等,
这会涉及到对象类型的equals方法和hashCode方法,
集合(容器)
1.2.3 Collection接口
10
所有实现了Collection接口的集合类都有一个iterator方法用
以返回一个实现了Iterator接口的对象
Iterator iterator()
Iterator对象称作迭代器,用以方便的实现对集合内元素的遍
历操作
Iterator 接口声明了如下方法:
boolean hasNext() ; 判读游标右边是否有元素
Object next() ; 返回游标右边的元素并将游标移动到下一
个位置
void remove() ; 从集合中删除上一个由next()方法返回的
元素
集合(容器)
1.2.4.Iterator接口
11
public class IteratorDemo{
public static void main(String[] args){
Collection coll = new ArrayList();
coll.add("hell world");
String temp = new String("Bean");
coll.add(temp);
coll.add(new String("232"));
Iterator iter = coll.iterator();
while(iter.hasNext()){
String tempV = (String)iter.next();
System.out.println(tempV);
}}}
集合(容器)
1.2.5.Iterator接口
集合(容器)
3.1.1.Set接口
12
Set是Collection的子接口,没有提供额外方法
Set是最简单的集合(容器),集合中的对象
不按照特定的方式排序,并且没有重复的对象。
Set接口主要有两个实现类:HashSet和
TreeSet。
4 6
2 7
53
1
Set(集)
集合(容器)
3.1.2Set接口
13
import java.util.*;
public class HashSetDemo{
public static void main(String[] args){
Set coll = new HashSet();
coll.add("hell world");
String temp = new String("Bean");
coll.add(temp);
coll.add(new Long(3883238442l));
coll.add(new Integer(232));
//加入相同的元素
coll.add(new String("Bean"));
System.out.println("coll="+coll);
}
}
集合(容器)
3.1.3 Set接口
14
import java.util.*;
public class TreeSetDemo{
public static void main(String[] args){
Set coll = new TreeSet();
coll.add("hell world");
String temp = new String("Bean");
coll.add(temp);
coll.add(new String("3883238442l"));
coll.add(new String("232"));
coll.add(new String("Bean"));
System.out.println("coll="+coll);
}
}
集合(容器)
3.1.4.Set接口—HashSet和TreeSet区别
15
1.HashSet无序集合,放入顺序与取出顺序不一
致。
2.TreeSet自动排好序,而且必须放相同的类型,
放不同的类型就会报错
集合(容器)
3.1.4.Set接口—Set比较顺序
16
hashCode:一个对象的散列码,什么是散列码呢,简单的
说就是通过哈希算法算出来的一大窜数字之类的东西和内存
有关. 如果对象1和对象2相等,说明他们的散列码相等!反
过来就不一样了!
判断两个对象的hashCode是否相等
如果不相等,认为两个对象也不相等,完毕
如果相等,转入第二条
判断两个对象用equals运算是否相等
如果不相等,认为两个对象也不相等
如果相等,认为两个对象相等(equals()是判断两个对
象是否相等的关键)
集合(容器)
4.1.1.List接口
17
List接口是Collection的子接口,实现List接口的集
合类中的元素师有顺序的,而且可以重复。List接
口的主要实现类包括:ArrayList和LinkedList。
List容器中的元素都对应一个序号记载其在容器中
的位置,可以根据序号存取。
10 652115
集合(容器)
4.1.2.List接口—新增的方法
18
Object get(int index)
返回列表中指定位置的元素。
int indexOf(Object o)
返回此列表中第一次出现的指定元素的索引;
set(int index, E element)
用指定元素替换列表中指定位置的元素(可选操作)。
int lastIndexOf(Object o)
返回此列表中最后出现的指定元素的索引;
Object remove(int index)
移除列表中指定位置的元素(可选操作)。
void add(int index, E element)
在列表的指定位置插入指定元素(可选操作)。
集合(容器)
4.1.3.List接口—ArrayList
19
import java.util.*;
public class ArrayListDemo{
public static void main(String[] args){
List list= new ArrayList();
list.add(new Integer(3)); list.add(new Integer(4));
list.add(new Integer(3)); list.add(new Integer(2));
for(int i=0;i> void
sort(List list)
列表中的所有元素都必须实现 Comparable 接口
集合(容器)
5.1.2.Collections.sort()对List排序
24
import java.util.*;
public class ListSortDemo{
public static void main(String[] args){
List list= new ArrayList();
list.add(new Integer(3));
list.add(new Integer(6));
list.add(new Integer(1));
list.add(new Integer(7));
printlist(list);
Collections.sort(list);
printlist(list);
}
public static void printlist(List list){
for(int i=0;i{
private String firstName;
private String lastName;
public Names(String firstname,String lastname){
this.firstName=firstname;
this.lastName=lastname;
}
public void setFirstName(String firstName){
this.firstName = firstName;
}
public String getFirstName(){
return this.firstName;
}
。。。。。。
public int compareTo(Names o){
int compareValue =
this.getLastName().compareTo(o.getLastName());
return compareValue;
}
}
集合(容器)
5.1.5.Collections.sort()对List排序
27
如果对象不能被修改,也就是说不能实现Comparable接
口,那又该怎么办呢?
Collections提供了下面的方法
public static void sort(List list, Comparator
super T> c)
根据指定比较器Comparator产生的顺序对指定列表进行排
序。
主程序里调用
Collections.sort(list,new ComparaTest());
class ComparaTest implements Comparator{
public int compare(Names o1, Names o2) {
// TODO Auto-generated method stub
return
o1.getLastName().compareTo(o2.getLastName());
}
}
集合
6.1.1.Map
28
Map 接口不是 Collection 接口的继承
向Map集合中加入元素时,必须提供一对键对象和值对象。
Map的两个主要实现类:HashMap和TreeMap。
Map类中存储的键—值对通过键来标识,所以键值不能重
复
键和值都是对象,键必须是唯一的,但值可以是重复的,
而且有一些映射可以接收null键和null值。
K3v2
K5v1
K2v4
K4v3
Map(映射)
集合(容器)
6.1.2.Map—常用方法
29
改变操作允许您从映射中添加和除去键-值对
remove(Object key) 从 Map 中删除键和关联的值
put(Object key, Object value) 将指定值与指定键相关联
clear() 从 Map 中删除所有映射
putAll(Map t) 将指定 Map 中的所有映射复制到此 map
查询操作允许您检查映射内容
get(Object key) 根据key(键)取得对应的值
containsKey(Object key) 判断Map中是否存在某键(key)
containsValue(Object value) 判断Map中是否存在某值
size() 返回Map中 键-值对的个数
isEmpty() 判断当前Map是否为空
允许把键或值的组作为集合来处理。
keySet() 返回所有的键(key),并使用Set容器存放
values() 返回所有的值(Value),并使用Collection存放
entrySet() 返回一个实现 Map.Entry 接口的元素 Set
集合(容器)
6.1.3.Map—HashMap
30
//在HashMap中的对象是无序
的
HashMap hm=new
HashMap();
hm.put("a","user 1");
hm.put("b","user 3");
hm.put("c","user 5");
//测试是否包含关键字"a”
System.out.println(hm.cont
ainsKey("a"));
System.out.println(hm.cont
ainsKey("d"));
System.out.println(hm.get(
"a"));
System.out.println(hm.entr
ySet());
Iterator
it=hm.entrySet().iterator();
while(it.hasNext()){
System.out.println(it.next());
}
//Set keySet()返回关键字的集合
it=hm.keySet().iterator();
while(it.hasNext()){
System.out.println(it.next());
}
//Collection values()返回值的集
合
it=hm.values().iterator();
while(it.hasNext()){
System.out.println(it.next());
}
集合(容器)
6.1.4.Map—TreeMap
31
SortedMap接口扩展了Map,确保了以键的升序方式保存键值
对。排序的映射提供了对子映射(即映射的子集)的非常有效的
操作
TreeMap实现了SortedSet接口,能够对的键对象进行排序。
支持自然排序和客户化排序。
集合(容器)
6.1.5.Map—TreeMap
32
TreeMap hm=new TreeMap();
hm.put("a","user 1");
hm.put("f","sser 3");
hm.put("c","user 5");
hm.put("3","user 5");
Iterator it=hm.entrySet().iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
集合(容器)
7.1.1.泛型
33
在定义集合的时候同时定义集合中对象的类型
可以在定义Collection的时候指定
也可以在循环时用Iterator指定
格式:
Set set = new HashSet()
class GenericsFoo,这样类中
的泛型T只能是Collection接口的实现类,传入非
Collection接口编译会出错。
通配泛型格式为 extends Collection>,“?”代表未
知类型,这个类型是实现Collection接口。
集合(容器)
7.1.1.泛型
34
提高了程序的安全性
将运行期遇到的问题转移到了编译期
省去了类型强转的麻烦
泛型类的出现优化了程序
设计
领导形象设计圆作业设计ao工艺污水处理厂设计附属工程施工组织设计清扫机器人结构设计
35
本章主要介绍了以下内容:
一个图
一个类Collections
六个接口(
Collection,Map,List,Set,C
omparable,Comparator)
集合(容器)
本章小结
36
1、编写一个学生类,将学生的姓名,年龄,性别分别保存
到HashSet, ArrayList,linkedList中,并利用迭代器Iterator将姓
名,年龄,性别打印出来。
2、对存在List中的学生对象进行排序,要求按照年龄排序
3、使用Map接口的实现类完成员工工资(姓名- 工资)的摸拟:
1) 添加几条信息
2) 列出所有的员工姓名
3)列出所有员工姓名及其工资
4) 删除名叫“Tom”的员工信息
5) 输出Jack的工资,并将其工资改为1500元
6) 将所有工资低于1000元的员工的工资上涨20%
集合(容器)
思考题