1.概述:
集合是一种容器,用于存储对象。由于集合内部的数据结构不同,因此多种具体容器,将其共性不断向上抽取,形成集合框架。
2.集合特点:
长度可变
集合中不可以存储基本数据类型值
3.集合框架的构成和分类:
a.图例:
4.Collection集合:
a.概述:Collection是一个接口,是高度抽象出来的集合,它包含了集合的基本操作:添加、删除、清空、遍历(读取)、是否为空、获取大小、是否保护某元素等。
b.常见方法:
代码示例:
Collection c1=new ArrayList();
Collection c2=new ArrayList();
//1.添加
//boolean add(E e); 确保此 collection 包含指定的元素(可选操作)。
c1.add("abc1");
c1.add("abc2");
c1.add("abc3");
c2.add("abc1");
c2.add("abc5");
c2.add("abc4");
c1.addAll(c2);
System.out.println(c1);
//2.删除
//boolean remove(object obj); 从此 collection 中移除指定元素的单个实例,如果存在的话(可选操作)。
//boolean removeAll(Collection coll); 移除此 collection 中那些也包含在指定 collection 中的所有元素(可选操作)。
boolean b1=c1.removeAll(c2);
System.out.println("removeAll:"+b1);
System.out.println("c1:"+c1);
//void clear(); 移除此 collection 中的所有元素(可选操作)。
//3.判断
//boolean contains(objec obj); 如果此 collection 包含指定的元素,则返回 true。
boolean b2=c1.contains("abc1");
System.out.println(b2);
//boolean containsAll(Collection coll); 如果此 collection 包含指定 collection 中的所有元素,则返回 true。
boolean b3=c1.containsAll(c2);
System.out.println(b3);
//boolean isEmpty();
//4.获取
//int size(); 返回此 collection 中的元素数。
//iterator(); 取出元素的方式:迭代器 是对所有的Collection容器进行元素取出的公共接口 具体实现细节被不同的容器封装隐藏
// Iterator it=c1.iterator();
// while(it.hasNext()){
// System.out.println(it.next());
// }
for(Iterator it=c1.iterator();it.hasNext();){
System.out.println(it.next());
}
//5.其他
//boolean retainAll(Collection coll);//取交集 保留和指定交集相同的元素
boolean b4=c1.retainAll(c2);
System.out.println(c1);
//Object[] toArray();//将集合转成数组
5.List集合
a.概述:List是一个继承了 Collection 的接口,是抽象出来的集合。代表一个元素有序,可重复的集合,集合中每个元素都有对应的顺序索引。
b.ArrayList集合:实现了List接口,内部是数组数据结构,是不同步的,代替了Vector集合,查询速度快。
代码示例:
// 1.添加元素
// boolean add(E e); 向列表的尾部添加指定的元素(可选操作)。
list.add("abc1");
list.add("abc2");
list.add("abc3");
// void add(index,collection);
// 插入元素 在列表的指定位置插入指定元素(可选操作)。
list.add(1, "abc9");
System.out.println(list);
// 2.删除元素
// Object remove(index); 移除列表中指定位置的元素(可选操作)。
System.out.println("remove:" + list.remove(2));
// 3.修改元素
// Object set(index,element); 用指定元素替换列表中指定位置的元素(可选操作)。
System.out.println("set:" + list.set(1, "abc8"));
// 4.取出元素 Object get(index); 返回列表中指定位置的元素。
System.out.println("get:" + list.get(0));
// int indexOf(object); 返回此列表中第一次出现的指定元素的索引;如果此列表不包含该元素,则返回 -1。
// int lastIndexOf(object); 返回此列表中最后出现的指定元素的索引;如果列表不包含此元素,则返回 -1。
// 5.获取子列表 List subList(fromIndex,toIndex); 返回列表中指定的 fromIndex(包括 )和
// toIndex(不包括)之间的部分视图。
System.out.println("sublist:" + list.subList(1, 3));
// 6.List额外提供的一个listIterator()方法,提供了专门操作List的方法。
ListIterator it = list.listIterator();
while (it.hasNext()) {
Object obj = it.next();
if (obj.equals("abc2")) {
it.add("abc9");
}
}
System.out.println("list:" + list);
// ArrayList 集合存储自定义对象
import java.util.ArrayList;
import java.util.Iterator;
class Person {
private String name;
private int age;
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class ArrayListTest {
public static void main(String[] args) {
ArrayList a1 = new ArrayList();
a1.add(new Person("lisi", 21));
a1.add(new Person("wangwu", 20));
a1.add(new Person("zhaoliu", 19));
a1.add(new Person("yanqi", 18));
Iterator it = a1.iterator();
while (it.hasNext()) {
Person p = (Person) it.next();// 向下转型
System.out.println(p.getName() + "--" + p.getAge());
}
}
}
c.Vector集合:实现了List接口,内部是数组数据结构,是同步的,增删、查询速度都很慢。
代码示例:
Vector v=new Vector();
//void addElement(E obj); 将指定的组件添加到此向量的末尾,将其大小增加1
v.addElement("abc1");
v.addElement("abc2");
v.addElement("abc3");
v.addElement("abc4");
//Enumeration接口 是Vector集合特有的的功能,此功能和Iterator接口的功能是重复的
//此外Iterator接口添加了一个可选的移除才做,并使用较短的方法名
Enumeration en=v.elements();
while(en.hasMoreElements()){
System.out.println("nextelement:"+en.nextElement());
}
Iterator it=v.iterator();
while(it.hasNext()){
System.out.println("next:"+it.next());
}
d.LinkedList集合:实现了List接口,内部是双向链表数据结构, 是不同步的,增删速度快。
代码示例:
LinkedList link=new LinkedList();
//void add(E element); 在此列表中指定的位置插入指定的元素。
link.addFirst("abc1");
link.addFirst("abc2");
link.addFirst("abc3");
link.addFirst("abc4");
Iterator it=link.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
System.out.println(link);
// E removeFirst(); 移除并返回此列表的第一个元素。
System.out.println(link.removeFirst());
System.out.println(link.removeFirst());
System.out.println(link.removeFirst());
System.out.println(link.removeFirst());
System.out.println(link);
//addFirst(); 将指定元素插入此列表的开头。
//addLast(); 将指定元素添加到此列表的结尾。
//JDK1.6后出现
//offerFirst();
//offerLast();
//getFirst(); 获取但不移除,如果链表为空,抛出 NoSuchElementException
//getLast();
//JDK1.6后出现
//peekFirst(); 获取但不移除,如果链表为空,返回 null
//peekLast();
//removeFirst();获取并移除,如果链表为空,抛出 NoSuchElementException
//removeLast();
//JDK1.6后出现
//pollFirst(); 获取并移除,如果链表为空,返回 null
//pollLast();
6.Set集合
a.概述:Set是一个继承了 Collection 的接口,是抽象出来的集合,存储的元素无序且不可重复。
b.HashSet集合:内部的数据结构是哈希表,是不同步的
代码示例:
import java.util.HashSet;
import java.util.Iterator;
/*
* 往 hashSet集合中存储Person对象 如果姓名和年龄相同,视为同一个人,视为相同元素
* HashSet 集合数据结构是哈希表,所以存储元素时,使用元素的hashCode方法来确定位置,如果位置相同,再通过元素的equals方法来确定是否相同。
*/
class Teacher {
private String name;
private int age;
public Teacher(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public int hashCode() {// 覆盖父类的hashCode方法
return name.hashCode() + age;
}
@Override
public boolean equals(Object obj) {// 覆盖父类equals方法
Teacher t = (Teacher) obj;
return this.name.equals(t.name) && this.age == t.age;
}
public String toString() {
return name + ":" + age;
}
}
public class HashSetDemo {
public static void main(String[] args) {
HashSet hs = new HashSet();
// HashSet存储自定义对象
hs.add(new Teacher("Wang1", 45));
hs.add(new Teacher("Wang3", 40));
hs.add(new Teacher("Wang2", 56));
hs.add(new Teacher("Wang4", 24));
hs.add(new Teacher("Wang4", 24));
Iterator it = hs.iterator();
while (it.hasNext()) {
// System.out.println(it.next());
Teacher t = (Teacher) it.next();
System.out.println(t.getName() + ":" + t.getAge());
}
}
}
c.LinkedHashSet集合:
代码示例:
// LinkedHashSet 此链接列表定义了迭代顺序,即按照将元素插入到 set 中的顺序(插入顺序)进行迭代。
HashSet hs=new LinkedHashSet();
hs.add("add1");
hs.add("add2");
hs.add("add3");
hs.add("add3");
Iterator it=hs.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
d.TreeSet集合:
代码示例:
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
//TreeSet:可以对Set集合中的元素进行排序,是不同步的
//判断元素唯一性的方式:根据比较方法的返回结构是否是0,如果是0,就是相同元素,不再存储
//TreeSet对元素进行排序的方式一:
//让元素自身具备比较功能,要实现 Comparable接口 并覆盖其中的 CompareTo 方法,自定义对象的比较方式
class Teacher implements Comparable {
private String name;
private int age;
public Teacher(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
return name + ":" + age;
}
@Override
public int compareTo(Object o) {// 覆盖compareTo方法
Teacher t = (Teacher) o;
// int temp = this.age - t.age;// 比较年龄大小
// return temp == 0 ? this.name.compareTo(t.name) :
// temp;//如果年龄相同继续比较姓名并返回,否则返回年龄
int temp = this.name.compareTo(t.name);// 比较姓名
return temp == 0 ? this.age - t.age : temp;// 如果姓名相同继续比较年龄并返回,否则返回姓名
}
}
// TreeSet对元素进行排序的方式二:
// 如果不按照对象中具备的自然顺序进行排序,可以使用 Comparator比较器 覆盖 compare方法
// 将该类对象作为参数传递给TreeSet集合的构造函数 让集合自身具备比较功能
class ComparatorByName implements Comparator {
@Override
public int compare(Object o1, Object o2) {
Teacher t1 = (Teacher) o1;
Teacher t2 = (Teacher) o2;
int temp = t1.getName().compareTo(t2.getName());
return temp == 0 ? t1.getAge() - t2.getAge() : temp;
}
}
public class TreeSetDemo {
public static void main(String[] args) {
TreeSet ts = new TreeSet(new ComparatorByName());
/*
* 以 Teacher 对象年龄进行从小到大排序
*/
ts.add(new Teacher("zhangsan", 28));
ts.add(new Teacher("lisi", 21));
ts.add(new Teacher("zhouqi", 29));
ts.add(new Teacher("zhaoliu", 25));
ts.add(new Teacher("wangwu", 29));
Iterator it = ts.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
7.Map集合
a.概述:Map<K,V> 是一个继承自 Object 的接口,是抽象出来的集合,一次添加一对元素,用于存储键值对。
b.HashMap集合:内部结构是哈希表,是不同步的,允许null作为键,null作为值。
代码示例:
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/*
* Map集合中存储的是键值对
* Map集合必须保证键的唯一性
*/
/*
* 添加 value put(key,value); 返回前一个和key关联的值,如果没有返回null
* 删除 void clear(); 清空Map集合 value remove(key);根据指定的key删除这个键值对
* 判断 boolean containsKey(key); boolean constainsValue(value); boolean isEmpty();
* 获取 value get(key);通过键获取值,如果没有返回 null 当然可以通过返回null,来判断是否包含支指定键 int size();获取键值对的个数
*
*/
public class MapDemo {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<Integer, String>();
// method(map);
// method_keySet(map);
method_entrySet(map);
}
//通过Map转成set就可以迭代 使用方法 entrySet
//该方法将键和值的映射关系作为对象存储到Set集合,这个映射关系的类型就是Map.Entry类型
private static void method_entrySet(Map<Integer, String> map) {
map.put(8, "wangwu");
map.put(2, "zhaoliu");
map.put(7, "xiaoqiang");
map.put(6, "wangcai");
Set<Map.Entry<Integer, String>> entrySet=map.entrySet();
Iterator<Map.Entry<Integer, String>> it=entrySet.iterator();
while(it.hasNext()){
Map.Entry<Integer, String> me=it.next();
Integer key=me.getKey();
String value=me.getValue();
System.out.println(key+":"+value);
}
}
private static void method_keySet(Map<Integer, String> map) {
map.put(8, "wangwu");
map.put(2, "zhaoliu");
map.put(7, "xiaoqiang");
map.put(6, "wangcai");
// 取出Map中的所有元素
// 原理:通过ketSet方法获取Map中所有的键所在的Set集合,
// 再通过Set的迭代器获取到每一个键,再对每一个键通过map集合的get方法获取其对应的值即可
Set<Integer> keySet = map.keySet();
Iterator<Integer> it = keySet.iterator();
while (it.hasNext()) {
Integer key = it.next();
String value = map.get(key);
System.out.println(key + ":" + value);
}
}
public static void method(Map<Integer, String> map) {// 学号和姓名
// 添加元素
System.out.println(map.put(8, "wangcai"));// null
System.out.println(map.put(8, "xiaoqiang"));// wangcai 存相同键,值会覆盖
map.put(2, "zhangsan");
map.put(7, "zhaoliu");
System.out.println(map);
//判断
System.out.println(map.isEmpty());
//删除
System.out.println("remove:" + map.remove(2));
System.out.println(map);
// 获取
System.out.println("get:" + map.get(6));
}
}
HashMap集合存储自定义对象
代码示例:
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
class Teacher {
private String name;
private int age;
public Teacher(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
return name + ":" + age;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Teacher other = (Teacher) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
public class HashMapDemo {
public static void main(String[] args) {
// 将老师对象和老师的归属地通过键与值存储到map集合中
HashMap<Teacher, String> hm = new HashMap<Teacher, String>();
hm.put(new Teacher("zhangsan", 38), "北京");
hm.put(new Teacher("lisi", 44), "上海");
hm.put(new Teacher("lisi", 44), "东京");
hm.put(new Teacher("wangwu", 56), "广州");
hm.put(new Teacher("zhouliu", 50), "深圳");
Set<Teacher> keySet = hm.keySet();
Iterator<Teacher> it = keySet.iterator();
while (it.hasNext()) {
Teacher key = it.next();
String value = hm.get(key);
System.out.println(key.getName() + ":" + key.getAge() + "---" + value);
}
}
}
c.Hashtable集合:内部结构是哈希表,是同步的,不允许null作为键,null作为值。
d.LinkedHashMap集合:Map 接口的哈希表和链接列表实现,具有可预知的迭代顺序,是不同步的。
e.TreeMap集合:内部是二叉树,是不同步的,可以对Map集合中的键进行排序,保证它的元素按key生升序排序。
代码示例:
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
class Teacher {
private String name;
private int age;
public Teacher(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
return name + ":" + age;
}
}
// TreeSet对元素进行排序的方式二:
// 如果不按照对象中具备的自然顺序进行排序,可以使用 Comparator比较器 覆盖 compare方法
// 将该类对象作为参数传递给TreeSet集合的构造函数 让集合自身具备比较功能
class ComparatorByName implements Comparator {
@Override
public int compare(Object o1, Object o2) {
Teacher t1 = (Teacher) o1;
Teacher t2 = (Teacher) o2;
int temp = t1.getName().compareTo(t2.getName());
return temp == 0 ? t1.getAge() - t2.getAge() : temp;
}
}
public class TreeMap {
public static void main(String[] args) {// TreeMap的底层是TreeSet
TreeMap<Teacher, String> tm = new TreeMap<Teacher, String>();
tm.put(new Teacher("zhangsan", 38), "北京");
tm.put(new Teacher("lisi", 44), "上海");
tm.put(new Teacher("lisi", 44), "东京");
tm.put(new Teacher("wangwu", 56), "广州");
tm.put(new Teacher("zhouliu", 50), "深圳");
Set<Map.Entry<Teacher, String>> entrySet = tm.entrySet();
Iterator<Map.Entry<Teacher, String>> it = entrySet.iterator();
while (it.hasNext()) {
Map.Entry<Teacher, String> me = it.next();
Teacher key = me.getKey();
String value = me.getValue();
System.out.println(key.getName() + ":" + key.getAge() + "---" + value);
}
}
}