Collection 和 数组区别#
- 数组可以存储基本数据类型, 也可以存储引用数据类型
- 集合可以存储引用数据类型, 也能存基本数据类型, 存储时会自动装箱
- 数组的长度固定, 不可以自由增加
- 集合长度可变, 可以自由增加
List Set 集合#
List#
Set#
- 无序集合, 存取位置不一致
- 无索引
- 不可存储重复数据
List 体系 ArrayList LinkedList Vector#
ArrayList#
- 底层是数组, 查询快, 增删慢
- 线程不安全, 效率高
LinkedList#
- 链表实现, 查询慢, 增删快
- 线程不安全, 效率高
Vection#
Set 体系 HashSet TreeSet#
HashSet#
TreeSet#
ArrayList 的#
- boolean add(E e) 方法一直返回true, Set 集合存取重复元素时, 则返回false
- boolean remove(Object o) 一次删除一个对象
- int size() 打印集合对象数
- void clear() 清空集合
- boolean isEmpty() 判断集合是否为空
public class Demo2_Collection {
public static void main(String[] args) {
Collection collection = new ArrayList();
boolean b1 = collection.add("abc");
boolean b2 = collection.add(true);
boolean b3 = collection.add(100);
boolean b4 = collection.add(new Student("小红",18));
boolean b5 = collection.add("abc");
System.out.println(b1);
System.out.println(b2);
System.out.println(b3);
System.out.println(b4);
System.out.println(b5);
System.out.println(collection);
System.out.println(collection.contains("abc"));
collection.remove("abc");
System.out.println(collection);
collection.remove("abc");
System.out.println(collection.size());
System.out.println(collection);
System.out.println(collection.isEmpty());
collection.clear();
System.out.println(collection.isEmpty());
System.out.println(collection);
}
}
- Object[] toArray() 集合转换成数组
import com.lizicai.bean.Student;
import java.util.ArrayList;
import java.util.Collection;
public class Demo3_Collection {
public static void main(String[] args) {
Collection c = new ArrayList();
c .add( new Student("小明", 24));
c .add( new Student("小红", 22));
c .add( new Student("小张", 30));
Object [] oArray = c.toArray();
for(Object o:oArray){
if( o instanceof Student){
Student s = (Student) o;
System.out.println(s.getName()+s.getAge());
} else{
System.out.println(o);
}
}
}
}
List 中带有All的方法#
- boolean addAll(Collection<? extends E> c) 添加另一个集合
- boolean removeAll(Collection> c) 移除集合中所有与C集合一样的元素
- boolean containsAll(Collection> c) 返回结果c集合全部 是 调用集合内元素的判断结果
- boolean retainAll(Collection> c) 调用集合和c集合取交集, 调用集合改变则是true, 没改是false
private static void Demo4() {
Collection c = new ArrayList();
c.add("a");
c.add("b");
c.add("c");
Collection c2 = new ArrayList();
c2.add("a");
c2.add("b");
System.out.println(c.retainAll(c2));
System.out.println(c);
}
private static void Demo3() {
Collection c = new ArrayList();
c.add("a");
c.add("b");
c.add("b");
c.add("c");
Collection c2 = new ArrayList();
c2.add("a");
c2.add("b");
c2.add("c");
System.out.println(c.containsAll(c2));
System.out.println(c);
System.out.println(c2);
}
private static void Demo2() {
Collection c = new ArrayList();
c.add("a");
c.add("b");
c.add("b");
c.add("c");
Collection c2 = new ArrayList();
c2.add("a");
c2.add("b");
c2.add("cc");
c.removeAll(c2);
System.out.println(c);
}
private static void Demo1() {
Collection c = new ArrayList();
c.add("a");
c.add("b");
c.add("c");
Collection c2 = new ArrayList();
c2.add("aa");
c2.add("bb");
c2.add("cc");
c.addAll(c2);
c2.add(c);
System.out.println(c);
System.out.println(c2);
}
集合的迭代器遍历#
import com.lizicai.bean.Student;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Demo1_Iterator {
public static void main(String[] args) {
Collection c = new ArrayList();
c.add(new Student("小明", 24));
c.add(new Student("小红", 22));
c.add(new Student("小张", 30));
Iterator it = c.iterator();
while (it.hasNext()){
Object obj = it.next();
if( obj instanceof Student){
Student s = (Student)obj;
System.out.println(s.getName()+s.getAge());
} else {
System.out.println(obj);
}
}
}
}
List 集合#
- void add(int index, E element)
- E remove(int index)
- E get(int index)
- E set(int index, E element)
import java.util.ArrayList;
import java.util.List;
public class Demo1_List {
public static void main(String[] args) {
List list = new ArrayList();
list.add("a");
list.add("b");
list.add("c");
list.add(0,"d");
System.out.println(list);
list.remove(0);
System.out.println(list);
list.set(2,"w");
System.out.println(list);
System.out.println(list.get(2));
}
}
List 遍历的2种方式#
public class Demo2_List {
public static void main(String[] args) {
List list = new ArrayList();
list.add("a");
list.add("b");
list.add("c");
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
Iterator it = list.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
}
}
List 的 ListIterator 可以在遍历的时候添加元素, List不能在遍历的时候添加元素#
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class Demo3_List {
public static void main(String[] args) {
List list = new ArrayList();
list.add("a");
list.add("ab");
list.add("abc");
list.add("Hello");
// Iterator li = list.iterator();
// while (li.hasNext()){
// String s = (String)li.next();
// if("Hello".equals(s)){
// list.add("World");
// }
// System.out.println(s);
// }
ListIterator li = list.listIterator();
while (li.hasNext()){
String s = (String)li.next();
if("Hello".equals(s)){
li.add("World");
}
}
System.out.println(list);
}
}
Vector#
import java.util.Enumeration;
import java.util.Vector;
public class Demo1_Vector {
public static void main(String[] args) {
Vector v = new Vector();
v.addElement("a");
v.addElement("b");
v.addElement("c");
v.addElement("d");
Enumeration en = v.elements();
while (en.hasMoreElements()){
System.out.println(en.nextElement());
}
}
}