类集的引出
类集实际上就是动态的对象数组,因为在开发过程中用到数组的地方并不多,因为数组的长度是固定的,所以Java为了解决数组长度固定的问题,JDK1.2引出了类集。
Collection接口
Java类集中最核心的两个接口:Collection、Map,其中Collection的操作和链表的操作类似,只能对单个对象操作,JDK1.5之后Collection接口上增加了泛型,避免了ClassCastException,此接口最常用的方法:public boolean add(E e),public Iterator <E ,e> iterator();但是Collection接口在开发中用的并不多,因为其不能区分存储类型,例如:要存放的元素需要区分重复和不重复,这就用到了Collection的子接口:List(允许数据重复),Set(不允许数据重复)
List接口
在集合处理中优先考虑List接口,List接口中两个重要的方法
1.根据索引取得保存数据:public E get(int index)
2.修改数据:public E set(int index,E element)
List 是接口,要取得List的实例化对象,就必须有类实现List接口,在List接口下常用的三个子类:LinckedList 、Vector、ArrayList(其实List下面有个AbstractList的抽象类,LinckedList 、Vector、ArrayList均继承了该抽象类)
Array List:
利用ArrayList进行List的基本操作
public class TestDemo {
public static void main(String[] args) {
//此时集合中只保存String类型
List<String> list = new ArrayList<>();
list.add("hello");
//允许数据重复
list.add("hello");
list.add("world");
System.out.println(list);
}
}
运行结果:
Array List的size()、isEmpty()、remove()、contain()方法的实现
public class TestDemo {
public static void main(String[] args) {
//此时集合中只保存String类型
List<String> list = new ArrayList<>();
System.out.println(list.size() + "," + list.isEmpty());
list.add("hello");
//允许数据重复
list.add("world");
System.out.println(list);
list.remove("world");
System.out.println(list.contains("hello"));;
System.out.println(list);
}
}
运行结果
ArrayList的get方法,ArrayList通过get方法和索引获取数据,而Collection取出数据需要将集合变为对象数组(toArray方法)
public class TestDemo {
public static void main(String[] args) {
//此时集合中只保存String类型
List<String> list = new ArrayList<>();
list.add("hello");
//允许数据重复
list.add("world");
for(int i = 0;i < list.size();i++){
System.out.println(list.get(i));
}
}
}
运行结果
在以后的学习中,集合里面保存最多的数据类型,就是Java类,集合在操作Java类时需要覆写equals方法
Vector:
Vector使用较少,vector的方法的使用:
public class TestDemo {
public static void main(String[] args) {
//此时集合中只保存String类型
List<String> list = new Vector<>();
list.add("hello");
//允许数据重复
list.add("world");
System.out.println(list);
list.remove("world");
System.out.println(list);
}
}
运行结果:
LinckedList
LickedList的使用同上
Set集合接口
Set接口和List接口最大的不同时Set接口不允许数据重复,而List接口允许数据重复,同时Set接口对List接口没有扩充,而List接口对Collection接口做了扩充,所以Set接口中没有get方法
在Set接口下面有两个常用子类:HashSet(无序存储)TreeSet(有序存储)(其实是Set接口下面有个abstractSet的抽象类,Hash Set和TreeSet均继承了该抽象类)
HashSet的使用:
public class TestDemo {
public static void main(String[] args) {
Set<String> set = new HashSet<>();
set.add("hello");
set.add("hello");
set.add("world");
System.out.println(set);
}
}
运行结果:不允许数据重复,且无序
TreeSet的使用:
public class TestDemo {
public static void main(String[] args) {
Set<String> set = new TreeSet<>();
set.add("C");
set.add("C");
set.add("B");
set.add("A");
System.out.println(set);
}
}
运行结果:不允许数据重复,且有序
若用TreeSet存放Java类的实例化对象,则该对象所在的类必须实现Comparable接口并且覆写compareTo方法
注意:要使用Comparable接口进行大小比较,需要对所有的属性进行比较。若一个类的属性很多,比较起来就很麻烦,所以我们一般使用HashSet
Set接口重复元素的判断
在使用TreeSet进行数据保存时,重复元素的判断依靠的是Comparable接口,但这并不全是Set接口判断重复元素的方法,因为如果使用的是HashSet,则依靠Object的两个方法判断重复:
1.hash码:public native int hashCode();
2.对象比较:public boolean equals(Object o);
在Java中进行对象的比较需要两步:
1.通过一个对象的唯一编码找到这个对象的唯一信息
2.当编码匹配之后再调用equals方法进行内容比较
对象判断必须equals方法和hashCode方法返回值都相同,才能确保两个对象相同
集合的输出
从标准上来讲,集合输出有四种方式:Iterator、ListIterator、Enumeration、foreach
Iterator接口最初的而设计有三个抽象方法:
1.判断是否有下一个元素:public boolean hasNext()
2.取得当前元素:public E next();
3.删除元素:public default void remove();
Iterator方法输出:
public class TestDemo {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("hello");
list.add("world");
list.add("woman");
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()){
String str = iterator.next();
if(str.equals("world")){
//使用集合提供的remove方法,会产生ConcurrentModificationException
//使用iterator的remove方法则不会产生异常
iterator.remove();
continue;
}
System.out.println(str);
}
}
}
输出结果:
双向迭代接口:ListIterator
使用同Iterator,但是List Iterator既可以从前往后输出,也可以从后往前输出(想要从后往前输出,必须先从前往后输出)
ListIterator接口定义的方法:
1.public boolean hasPrevious()
2.public boolean hasNext();
3.public E previous();
4.public E next();
Iterator接口对象是由Collection接口支持的,List Iterator接口对象是由List接口支持的,List接口提供以下方法:
public ListIterator listIterator();
Enumeration枚举输出
JDK1.0就引入了Enumeration接口,1.5对其追加了泛型
Enumeration接口定义的方法:
1.判断是否有下一个元素:public boolean hasMoreElements();
2.取得元素:public E nextElement();
想要取得此接口的实例化对象只能依靠Vector子类,在Vector类中提供了以下方法取得Enumeration接口对象
public Enumeration elements();
public class TestDemo {
public static void main(String[] args) {
Vector<String> vector = new Vector<>();
vector.add("hello");
vector.add("world");
Enumeration<String> elements = vector.elements();
while (elements.hasMoreElements()){
String str = elements.nextElement();
System.out.println(str);
}
}
}
输出结果:
foreach输出
JDK1.5foreach可以输出数组,也可以输出集合
public class TestDemo {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("hello");
list.add("world");
for(String str:list){
System.out.println(str);
}
}
}
输出结果: