java.util.List接口 extends Collection 接口 List 接口的特点:
1.有序的集合,存储元素和取出元素的顺序是一致的(存储123取出123)
2.有索引,包含了一些带索引的方法
3.允许存储重复的元素
List接口中带索引的方法(特有)
-public void add(int index,E element):将指定的元素,添加到该集合中的指定位置上。
-public E get(int index):返回集合中指定位置的元素。
-public E remove(int index):移除列表中指定位置的元素,返回的是被移除的元素。
-public E set(int index,E element):用指定元素替换集合中指定位置的元素,返回值的更新前的元素。
注意:
操作索引的时候,一定要防止索引越界异常
public class demo1 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("a");
//打印集合
System.out.println("==========打印初始list==========");
System.out.println(list);
System.out.println("==========添加元素==========");
//在c 和 d 之间添加元素
list.add(3,"HelloWorld");
System.out.println(list);
System.out.println("=========移除元素===========");
//移除元素
String removeE = list.remove(2);
System.out.println("被移除的元素:"+removeE);
System.out.println("处理后的list:"+list);
System.out.println("========修改元素============");
//替换元素
String SetE = list.set(3, "C");// 索引,元素
System.out.println("修改的元素是"+SetE);
System.out.println(list);
//三种方式遍历list
System.out.println("========遍历list方式一、========");
for (int i = 0; i < list.size(); i++) {
String s = list.get(i);
System.out.println(s);
}
System.out.println("========遍历list方式二、========");
//迭代器
Iterator<String> it = list.iterator();
while (it.hasNext()){
String next = it.next();
System.out.println(next);
}
System.out.println("========遍历list方式三、========");
//增强for循环
for (String s:list
) {
System.out.println(s);
}
}
}
ArrayList集合
java.util.ArrayList 集合数据存储的结构是数组结构,元素增删慢,查找快,由于日常开发中使用最多的功能为查询数据、遍历数据,所以Arraylist是最常用的集合。
许多程序员开发时非常随意地使用ArrayList完成任何需求,并不严谨,这种用法是不提倡的。
LinkedList集合
java.util.LinkedList 集合数据存储的结构是链表结构,方便元素添加、删除的集合。
java.util.LinkedList集合 implements List接口LinkedList集合的特点:
1.底层是一个链表结构:查询慢,增删快
2.里边包含了大量操作首尾元素的方法
注意:使用LinkedList集合特有的方法,不能使用多态
-public void addFirst(E e):将指定元素插入此列表的开头。
-public void addList(E e):将指定元素添加到此列表的结尾。
-public void push(E e):将元素推入此列表所表示的堆栈。//等效于addFirst();
-public E getFirst():返回此列表的第一个元素。
-public E getList():返回此列表的最后一个元素。
-public E removeFirst():移除并返回此列表的第一个元素。
-public E removeList():移除并返回此列表的最后一个元素。
-public E pop():从此列表所表示的堆栈处弹出一个元素。//等效于removeFirst();
-public boolean isEmpty():如果列表不包含元素,则返回true。
public class demo2LinkedList {
public static void main(String[] args) {
// show01();
// show02();
show03();
}
private static void show03() {
LinkedList<String> linked = new LinkedList<>();
//使用add
linked.add("a");
linked.add("b");
linked.add("c");
String s = linked.removeFirst();
System.out.println(s);
String s1 = linked.removeLast();
System.out.println(s1);
System.out.println(linked);
String pop = linked.pop();
System.out.println(pop);
}
private static void show02() {
LinkedList<String> linked = new LinkedList<>();
//使用add
linked.add("a");
linked.add("b");
linked.add("c");
// linked.clear();
if (!linked.isEmpty()){
String first = linked.getFirst();
System.out.println(first);
String last = linked.getLast();
System.out.println(last);
}
}
private static void show01() {
/*
-public void addFirst(Ee):将指定元素插入此列表的开头。
-public void addList(E e):将指定元素添加到此列表的结尾。
-public void push(E e):将元素推入此列表所表示的堆栈。此方法等效于 addFirst(E).
*/
LinkedList<String> linked = new LinkedList<>();
//使用add
linked.add("a");
linked.add("b");
linked.add("c");
System.out.println(linked);//[a, b, c]
// linked.addFirst("www");
linked.push("www");
System.out.println(linked);//[www, a, b, c]
linked.addLast("com");
System.out.println(linked);//[www, a, b, c, com]
}
}
Vector集合
Vector类实现了可扩展的对象数组。 像数组一样,它包含可以使用整数索引访问的组件。 但是, Vector的大小可以根据需要增长或缩小,以适应在创建Vector之后添加和删除项目。
每个向量尝试通过维护capacity和capacityIncrement优化存储capacityIncrement 。 capacity总是至少与矢量大小一样大; 通常较大,因为当向量中添加组分时,向量的存储空间大小capacityIncrement 。 应用程序可以在插入大量组件之前增加向量的容量; 这减少了增量重新分配的数量。
- 从Java 2平台v1.2,这个类被改造为实现
List接口,使其成为成员Java Collections Framework 。 与新集合实现不同,Vector是同步的。 如果不需要线程安全的实现,建议使用ArrayList代替Vector。
Vector是线程安全的,所以速度慢,已经被ArryList所取代。
Vector中几个重要的方法。

public class VectorDemo {
public static void main(String[] args) {
Vector v = new Vector();
v.addElement("abc1");
v.addElement("abc2");
v.addElement("abc3");
v.addElement("abc4");
Enumeration en = v.elements(); //枚举类型
while(en.hasMoreElements()){//枚举的迭代方法
System.out.println("nextelment:"+en.nextElement());
}
Iterator it = v.iterator();
while(it.hasNext()){
System.out.println("next:"+it.next());
}
}
}
HashSet集合
java.util.Set接口 extends collection接口Set接口的特点:
1.不允许存储重复的元素
2.没有索引,没有带索引的方法,也不能使用普通的for循环遍历
java.util.HashSet集合 implements Set接口HashSet特点:
1.不允许存储重复的元素
2.没有索引,没有带索引的方法,也不能使用普通的for循环遍历
3.是一个无序的集合,存储元素和取出元素的顺序有可能不一致
4.底层是一个哈希表结构(查询的速度非常的快)
public class demo3HashSet {
public static void main(String[] args) {
Set<Integer> hashSet = new HashSet<>();
hashSet.add(1);
hashSet.add(3);
hashSet.add(2);
hashSet.add(1);
Iterator<Integer> it = hashSet.iterator();//使用迭代器遍历
while (it.hasNext()){
Integer set = it.next();
System.out.println(set);
}
System.out.println("------------------------------");
for (Integer hash:hashSet){
System.out.println(hash);
}
}
}