Java集合框架介绍
所谓的框架就是一个类库的集合。集合框架就是一个用来表示和操作集合的统一的架构,它包含了实现集合的接口与类。
集合框架中不同的集合类有各自不同的数据结构,所以在使用中要根据应用的性能要求来选择不同的集合类。
集合类存放在java.util包中,今后进行程序编写时将大量使用集合类和相关接口。
Iterable:迭代器接口
实现该接口允许对象成功“foreach”语句的目标,即该集合对象允许迭代。
Collection是Iterable的子接口,可以迭代访问
方法:
Iterator<T> iterator(),返回一个在一组T类型的元素上进行迭代的迭代器
迭代器是实现了Iterator、ListIterator接口的类对象,可以通过遍历类集,访问操作其中的每个元素。
ListIterator扩展了父接口Iterator,允许双向遍历集合,并可以修改和删除元素。
Collection:类集接口
Collection接口定义的常用方法:
int size()
boolean isEmpty()
boolean contains(Object obj)
Iterator<T> iterator()
Object[] toArray()
boolean add(T obj)
boolean remove(Object obj)
void clear()
List:列表接口
List接口扩展了Collection,特点:有序且可重复
最常用的两个List接口的实现类是ArrayList和LinkedList
ArrayList-动态数组
ArrayList类扩展AbstractList并实现了List接口
ArrayList构造方法
ArrayList()
ArrayList(Collection c)
ArrayList(int capacity)
ArrayList常用方法
boolean add(E e)
void add(int index, E element)
T get(int index)
int indexOf(Object o)
boolean remove(int index)
boolean remove(Object o)
int size()
Object[] toArray()
boolean isEmpty()
void clear()
Set:数据集接口
Set接口扩展了Collection,特点:无序且不可重复
Queue:队列
Map:键值对接口
Map映射集合,是一个存储关键字/值对的对象。给定一个关键字,可查询得到它的值,关键字和值都可以是对象。不能使用迭代器遍历。
demo:
package pkg1; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class ArrayListDemo { public static void main(String[] args) { /* * 默认容量大小10,超过时,默认扩大到1.5倍,如果还不够就使用需要的大小(oldSize+AddSize) */ List<String> strList=new ArrayList<String>(); strList.add("No001"); strList.add("No002"); strList.add("No003"); strList.add(1,"No007");//指定位置插入 strList.set(0, "No0001");//替换 System.out.println("使用迭代器进行遍历"); Iterator<String> it = strList.iterator(); while(it.hasNext()){ System.out.println(it.next()); } System.out.println("使用增强for循环进行遍历"); for(String str:strList){ System.out.println(str); } System.out.println("——————————————————————————"); System.out.println("indexOf(abc):"+strList.indexOf("abc")); System.out.println("indexOf(No007):"+strList.indexOf("No007")); System.out.println("删除No007结果:"+strList.remove("No007"));//移除首次出现的元素,如果存在返回true System.out.println("contains(No007)结果:"+strList.contains("No007")); System.out.println("get(0)结果:"+strList.get(0)); System.out.println("size()结果:"+strList.size()); System.out.println("——————————————————————————"); ArrayList<Student> stuList=new ArrayList<Student>(); stuList.add(new Student("张三", 16)); stuList.add(new Student("李四", 15)); stuList.add(new Student("Jack", 19)); Student stu4=new Student("张三", 16); Student stu5=new Student("李四", 15); //不是同一对象,默认找不到 //重写equals方法,可以实现 System.out.println("indexOf(stu4):"+stuList.indexOf(stu4)); //删除也是用equals判断是否找到 //查看类或方法源码:ctrl+点击类名或方法名 //看不了源码,可以参考:https://blog.csdn.net/u011514810/article/details/53196371 System.out.println("remove(stu4):"+stuList.remove(stu4)); System.out.println("indexOf(stu4):"+stuList.indexOf(stu4)); System.out.println("contains(stu5):"+stuList.contains(stu5)); } } class Student{ private String name; private int age; //自动生成构造方法: //Source->Generate Constructor use Fileds public Student(String name, int age) { super(); this.name = name; this.age = age; } //自动生成get,set方法 //Source->Generate Getters and Setters 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; } //自动生成equals方法的重写 //Source->Generate hashCode() and equals(),把hashCode方法删掉即可 @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Student other = (Student) 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; } }
效果:
LinkedList双向链表
LinkedList类扩展AbstractSequentialList并实现List、Deque接口
LinkedList提供了一个链表数据结构
LinkedList双向链表添加、删除效率比ArrayList高,但查找效率比ArrayList低
构造方法
LinkedList()
LinkedList(Collection c)
除了继承的方法之外,LinkedList还有一些特有的方法
void addFirst(T obj)
void addLast(T obj)
T removeFirst()
T removeLast()
demo:
package pkg1; import java.util.LinkedList; public class LinkedListDemo { public static void main(String[] args) { LinkedList<String> list=new LinkedList<String>(); list.add("张三"); list.add("李四"); list.add("jack"); list.addFirst("wangwu");//添加为第一个 for(String str:list){ System.out.println(str); } System.out.println("removeFirst:"+list.removeFirst());//移除并返回,没有则报错 System.out.println("size:"+list.size()); System.out.println("pollFirst:"+list.pollFirst());//移除并返回,没有则返回null System.out.println("size:"+list.size()); } }
效果: