一、Collection接口
Collection是最基本的集合接口,定义了一组允许重复或者不重复的对象,主要需要了解它两大子类Set,List
1、List接口
继承自Collection接口,允许元素重复,元素是有序
2、ArrayList
基于数组的List
(1) //创建ArrayList对象
ArrayList<String> fruits = new ArrayList<String>();
//添加元素
fruits.add("苹果");
fruits.add("香蕉");
//获取链表长度
int size = fruits.size();
//访问内容(一一遍历出各个元素)
for (int i = 0; i < size; i++) {
String s = fruits.get(i);
System.out.println(s);
}
(2) 移除内容
使用remove(int index)表示按下标移除
String removeItem = fruits.remove(2);
使用remove(Object obj)表示按元素内容移除(首次出现的)
fruits.remove("香蕉");
(3) 修改内容
fruits.set(3, "梨子");
(4) 检测是否包含
//判断链表中是否包含某个元素
boolean isHas = fruits.contains("葡萄");
System.out.println(isHas);
检测是否为空
boolean isEpt = ls.isEmpty();
清空内容
//清空链表内容
ls.clear();
(5) 链表转为数组
//将链表转为数组
Integer[] ary = new Integer[ls.size()];
ls.toArray(ary);
//将数组转为链表
List<Integer> l = Arrays.asList(ary);
3、方法使用的规律
你要拿什么东西(获取什么)那么这个方法方法要有返回值
不要给什么东西给他,那么方法必须有参数列表
方法属于对象,调用时使用对象来调用
4、LinkedList
链式集合,跟ArrayList不一样,它基于链(头尾相连),特点增删速度快,查询速度慢。ArrayList查询快,增删慢。
LinkedList<String> list = new LinkedList<String>();
//添加到末尾
list.add("苹果");
// 添加到头部
list.addFirst("香蕉");
// 添加到尾部
list.addLast("梨子");
// 移除头部元素
String str = list.removeFirst();
removeLast(); 移除尾部元素
//读取头部元素,但不移除
String s2 = list.peek();
5、Set接口
不允许重复,无序的
(1)HashSet
允许有null元素,但是只有一个
HashSet<String> hs = new HashSet<String>();
//添加内容
hs.add("abc");
hs.add("123");
//移除内容
hs.remove("abc");
(2)TreeSet
有自己的排序方式(红黑树结构排列),元素不允许为null
TreeSet<String> ts = new TreeSet<String>();
ts.add("abc");
ts.add("123");
//获取大小
int size = ts.size();
(3)Iterator接口
可以对collection进行迭代
对Set迭代
//从集合中产生迭代器
Iterator<String> it = ts.iterator();
while(it.hasNext()){ //判断有没有下一个节点
String s = it.next(); //读取下一个节点(指针向后移动)
System.out.println(s);
}
6、 Map接口
Map接口用于维护键-值对的集合
特点:不允许重复的键
(1)HashMap
基于哈希表(散列表)
//尖括号中第一个类型描述的键的类型,第一个是值的类型
HashMap<Character,String> hm = new HashMap<Character, String>();
//添加内容
hm.put('a', "苹果");
hm.put('b', "香蕉");
hm.put('c', "香蕉");
//获取内容
String str = hm.get('a');
//检测某个键是否存在
boolean isHasKey = hm.containsKey('d');
(2) 遍历Map内容
//先将Map转为Set<Entry<K,V>>
Set<Entry<Character,String>> set = hm.entrySet();
//从Set上获取迭代器
Iterator<Entry<Character,String>> it = set.iterator();
//迭代每个键值对对象
while(it.hasNext()){
Entry<Character,String> entry = it.next();
//获取key
char key = entry.getKey();
//获取值
String value = entry.getValue();
System.out.println(key+":"+value);
}
二、栈
Stack是一种后进先出的数据结构
Stack<String> s = new Stack<String>();
//将内容压入栈
s.push("A");
s.push("B");
s.push("C");
//读取栈顶内容,不移除
String str = s.peek();
//弹出栈顶元素
String str1 = s.pop();
//搜索元素和栈顶的距离
int index = s.search("A");
empty()检测栈是否为空
三、Comparable接口
Comparable可以用于比较的实现,实现了Comparable接口的类可以通过重写它的compareTo方法来定义排序规则
public class Student implements Comparable<Student> {
private int id;
/**
* 当前学生对象跟其他学生对象比较的方法(实现Comparable接口重写)
* 返回值:-1表示小了 1表示大了 0表示相等
*/
@Override
public int compareTo(Student other) {
if (id < other.id) {
return 1;
} else if (id > other.id) {
return -1;
}
return 0;
}
}
使用Collections.sort方法排序
//对集合进行排序(自然排序)
Collections.sort(stus);
四、Comparator接口
可以对任意类型进行排列的比较器,排序时将此接口的实现传递给Collections.sort方法或者Arrays.sort方法即可
//按身份证好来排序(匿名内部类)
Collections.sort(ps, new Comparator<Person>(){
@Override
public int compare(Person o1, Person o2) {
if(o1.getId()<o2.getId()){
return -1;
}else if(o1.getId()>o2.getId()){
return 1;
}
return 0;
}
});
注意:Comparator和Comparable的区别
Comparator位于java.util包下,而Comparable位于java.lang包下,前者可以在一个独立的来中实现比较,后者需要嵌入到自身类中。