集合框架一

来源:互联网 发布:视频音乐提取软件 编辑:程序博客网 时间:2024/06/10 05:51
学习Collection类的时候先看一下API怎么解释的:


Collection类:
public interface Collection<E>extends Iterable<E>
Collection层次结构中的根接口。
下面我们要学习的两个子接口:List<E>  Set<E>
--Collection
-List
-ArrayList
-LinkedList
-Set
-HashSet
-TreeSet


Map类:
public interface Map<k,v>;
将键映射到值的对象。一个映射不能包含重复的键;每个键最多只能映射到一个值
要学习的实现类:
HashMap   LinkedHashMap   TreeMap

Collection 接口,针对的集合是单层集合
而Map<k,v>接口是以键值对出现的。我个人称为多层集合


第一步:学习Collection类:《父接口》
一:Collection类的概述:
Collection类:


位于:java.util包下,所以在使用的时候结的导入这个包。
public interface Collection<E>extends Iterable<E>,是根接口。


Collection类表示一组对象,这些对象也称为Collection的元素。
一些 collection 允许有重复的元素,而另一些则不允许。
一些 collection 是有序的,而另一些则是无序的

注意:哪些集合类可以重复?哪些集合类不能重复?
List集合可以重复------Set集合不能重复
 哪些集合类表示有序?哪些集合类不表示有序了?
List集合有序-------Set集合无序

说明:有序表示谁先存储,谁先出来
Collection是从1.2版本开始使用的


二、集合和数组的区别:
1.长度的区别:
A:数组的长度是固定的
B:集合的长度是可变的
2.存储的内容不一样,
A:数组存储的是同一种数据类型的元素
B:集合可以存储不同的类型的元素
3.存储的数据类型不一样:
A:数组可以存储基本数据类型,也可以存储引用数据类型
B:集合只能存储引用数据类型
关键代码:
Collection c = new ArrayList();
c.add(1);//自动转换成Integer类,调用的valueOf()方法
c.add(2);
c.add(3);
c.add('a');
c.add("a");//自动转换成包装类 调用的valueOf()方法
c.add(false);
System.out.println("c:"+c);


----------------------------
源码:
Collection c = new ArrayList();
c.add(Integer.valueOf(1));
c.add(Integer.valueOf(2));
c.add(Integer.valueOf(3));
c.add(Character.valueOf('a'));
c.add("a");
c.add(Boolean.valueOf(false));
System.out.println("c:" + c);
补充:在分析问题的时候:
从具体到抽象
在实现代码的时候:
从抽象到具体
在使用的时候:
使用具体
三、Collection接口的成员方法:
1.添加方法:
boolean add(Object obj):添加一个元素;
boolean addAlll(Collection c);添加一个集合元素;谁调用就往哪个里面添加所有
2.删除功能:
void clear();移除所有的元素
boolean remove(Object o);移除一个元素
boolean removeAll(Collection c)移除一个集合的元素《只要移除了一个元素,都返回true》
3判断功能:
boolean contains(Object o):判断集合中是否包含指定的元素
boolean containsAll(Collection c):判断集合中是否包含指定的集合元素(包含了所有元素才返回true)
boolean isEmpty();是否为空
4.长度:
集合的长度是通过:int size()方法
而数组是通过length属性来获取数组的长度的
字符串中有length方法。
5.把集合转换成数组:
Object[] toArray();
6.交集功能:
boolean retainAll(Collection c);两个集合是否有交集
思考:
思考元素去哪了,返回的boolean又是什么意思呢?
如果有两个集合A和B
A对B做交集,最终的结果保存在A中,
返回值表示的是A是否发生过改变
7.集合框架的节点:Iterator
Collection<String> c = new ArrayList<String>();
c.add("hello");
c.add("world");
c.add("java");
c.add("kevin");
c.add("cidy");

Iterator<String> it = c.iterator();
while(it.hasNext()){//判断是否存在下一个元素
String s = it.next();
System.out.println(s):
}
案例:用集合存储5个学生对象,并把学生对象进行遍历:
/*
 * 学生类:
 */
public class Student {
//姓名
private String name;
//年龄:
private int age;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(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;
}
//重写了toString()方法
@Override
public String toString() {
return name+"-----"+age;
}
}



import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;


/*
 * 用集合存储5个学生对象,并把学生对象进行遍历
 */
public class CollectionTest {
public static void main(String[] args) {
Collection<Student> c = new ArrayList<Student>();


Student s1 = new Student("tom", 24);
Student s2 = new Student("kevin", 28);
Student s3 = new Student("cidy", 22);
Student s4 = new Student("Jon", 24);
Student s5 = new Student("Dive", 29);


c.add(s1);
c.add(s2);
c.add(s5);
c.add(s4);
c.add(s3);


Object[] arr = c.toArray();
for (int i = 0; i < arr.length; i++) {
// 如果不重写toString()方法,打印的是地址值,在Studnet类中重写toString方法
System.out.println(arr[i]);
}
System.out.println("----------------------");
// 转换成数组后可以使用增强for
for (Object o : arr) {
System.out.println(o);


System.out.println("--------------------------");
Iterator<Student> it = c.iterator();
while (it.hasNext()) {
Student s = it.next();
System.out.println(s.getName() + "---" + s.getAge());
//java.util.NoSuchElementException
//不要多次调用it.next();汇报上面的错误的
//System.out.println(it.next().getName()+"---"+it.next().getAge());

}
}
}
}


思考:迭代器为什么定义成一个接口,而不是类了?
假如把迭代器定义一个类,那么都可以创建对象,通过对象调用方法
从而来遍历集合,但是在java中有有多种不同的集合,每一种集合的数据结构不一样,
存储数据和遍历方式应该不一样,所以把迭代器定义为一个接口。

无论你是哪种集合的遍历,都应该具备判断和获取功能,每种集合遍历的方式不一样,
我们把具有相同功能抽取出来,并不提供具体的实现。
那么正直的实现,应该在子类中,以内部类的方式体现
0 0