Java集合框架
java集合框架体系如下图所示,Collection是List和Set的父类,它们都是接口,分别有各自实现类。此外,需要注意的是Map与Collection并无直接关系。
Collection接口
Collection接口存储的是一组不唯一,无序的对象。其是List和Set的父类。
List接口
List接口存储的是一组不唯一(可重复),无序的对象,其是ArrayList、LinkedList的父类。
ArrayList
ArrayList实现了长度可变的数组,在内存中分配连续的空间,遍历元素和随机访问元素的效率比较高。其常用的如下:
示例:
// 存储新闻,新闻数量未知
public class News{
private int titile;
private String content;
private String author;
public News() {
}
public News(int titile, String content, String author) {
this.titile = titile;
this.content = content;
this.author = author;
}
public int getTitile() {
return titile;
}
public void setTitile(int titile) {
this.titile = titile;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Override
public String toString() {
return "News{" + "titile=" + titile +
", content='" + content + '\'' +
", author='" + author + '\'' +
'}';
}
}
// 测试类
public class TestArrayList {
public static void main(String[] args) {
ArrayList newsList = new ArrayList();
News news = new News(1,"nn","jj");
News news2 = new News(2,"nn1","jj1");
News news3 = new News(3,"nn2","jj2");
News news4 = new News(4,"nn13","jj3");
News news5 = new News(5,"nn15","jj5");
newsList.add(news);// 在集合中加入元素
newsList.remove(news);// 在集合中删除元素
newsList.add(news);
newsList.add(news2);
newsList.get(0);// 获取指定下标的值
// newsList.set(0,1);// 修改指定下标的值
newsList.add(news3);
newsList.add(news4);
// for循环输出
for (int i = 0; i < newsList.size(); i++) {
System.out.println(newsList.get(i));
}
System.out.println("--------------------------------------------------");
// 增强for遍历
for(Object obj : newsList){
News n = (News)obj;
System.out.println(n);
}
System.out.println("--------------------------------------------------");
newsList.add(1,news5);// 指定位置插入
// 迭代器遍历输出
Iterator itor = newsList.iterator();
while(itor.hasNext()){
News n = (News)itor.next();
System.out.println(n);
}
newsList.remove(1);//通过下标删除值
System.out.println(newsList.toString());
}
}
LinkedList
LinkedList采用链表存储方式,插入、删除元素时效率比较高
除了ArrayList具有的方法,LinkedList还具有一下方法:
示例:
// 还以新闻例子,采用LinkedList
public class TestLinkedList {
public static void main(String[] args) {
News news = new News(1,"nn","jj");
News news2 = new News(2,"nn1","jj1");
News news3 = new News(3,"nn2","jj2");
News news4 = new News(4,"nn13","jj3");
News news5 = new News(5,"nn15","jj5");
LinkedList list = new LinkedList();
list.add(news);
list.addFirst(news2);
list.add(news3);
list.add(news4);
list.add(news5);
list.addLast(news4);
list.remove();// 等同于removeFirst();
list.element();// 等同于getFirst();
list.peek();// 等同于getFirst();若为空则返回null
Iterator itor = list.iterator();
while(itor.hasNext()){
News n = (News)itor.next();
System.out.println(n);
}
list.poll();//弹出首元素,相当于removeFirst();
list.pop();//等同于removeFirst();
list.push("1");// 等同于addFirst();
// 将LinkedList数组内容复制到ArrayList
ArrayList arrayList = new ArrayList();
arrayList.addAll(list);
}
}
ArrayList和LinkedList有何异同?
ArrayList:底层是可变数组:遍历元素更快,改变值更快
LinkedList:底层是双向链表:插入、删除更快
ArrayList、LinkedList可以通过for循环、增强for、迭代器是三种方式遍历
Set
Set接口存储一组唯一,无序的对象。HashSet是Set接口常用的实现类。
例题:
// 判断下列输出
Set set=new HashSet();
String s1=new String("java");
String s2=s1;
String s3=new String("JAVA");
set.add(s1);
set.add(s2);
set.add(s3);
System.out.println(set.size());// 输出结果为2
System.out.println("-------------------------------------------");
Set set=new HashSet();
String s1=new String("java");
String s2=s1;
String s3=new String ("java");
set.add(s1);
set.add(s2);
set.add(s3);
System.out.println(set.size());//输出结果为1
Set采用对象的equals()方法比较两个对象是否相等,因此会输出如上结果
HashSet
public class TestSet {
public static void main(String[] args) {
Set set = new HashSet();
set.add(1);
set.add("abc");
set.add("hello");
set.add("abc");
set.add(1);
set.add(2);
System.out.println(set.size());
// 迭代器遍历
Iterator itor = set.iterator();
while(itor.hasNext()){
System.out.println(itor.next());
}
if(set.contains("abc")){
set.remove("abc");
}
}
}
由于Set无序,因此没有下标的概念,所有遍历时只能用增强for和迭代器