面向对象语言对事物的体现都是以对象的形式,为了方便对多个对象的操作,就要对对象进行存储。使用 Array 存储对象方面具有一些弊端,而 Java 集合就像一种容器,可以动态地把多个对象的引用放入容器中。
目录
数组在内存存储方面的特点
1. 数组初始化以后,长度就确定了
2. 数组声明的类型就决定了进行元素初始化时的类型
数组在存储数据方面的弊端
1. 数组初始化以后,长度就不可变了,不便于扩展
2. 数组中提供的属性和方法少,不便于进行添加、删除、插入等操作,且效率不高。同时无法直接获取存储元素的个数
3. 数组存储的数据是有序的、可以重复的,存储数据单一
Java集合的框架
Collection 接口:单列集合,用来存储一个一个的对象
List 接口:存储有序的、可重复的数据(“动态” 数组)
ArrayList、LinkedList、Vector
Set 接口:存储无序的、不可重复的数据
HashSet、LinkedHashSet、TreeSet
Map 接口:双列集合,用来存储一对(key - value)的数据(映射)
HashMap、LinkedHashMap、TreeMap、Hashtable、Properties
Collection接口的使用
添加 | add(Object obj) | |
addAll(Collection coll) | ||
获取有效元素的个数 | int size() | |
清空集合 | void clear() | |
是否是空集合 | boolean isEmpty() | |
是否包含某个元素 | boolean contains(Object obj) | 是通过元素的 equals 方法来判断是否是同一个对象 |
boolean containsAll(Collection coll) | 也是调用元素的 equals 方法来比较,拿两个集合的元素挨个比较 | |
删除 | boolean remove(Object obj) | 通过元素的 equals 方法判断是否是要删除的元素。(只会删除找到的第一个元素) |
boolean removeAll(Collection coll) | 取当前集合的差集 | |
取两个集合的交集 | boolean retainAll(Collection coll) | 把交集的结果存在当前集合中,不影响 coll |
集合是否相等 | boolean equals(Object obj) | |
转成对象数组 | Object [ ] toArray | |
获取集合对象的哈希值 | hashCode() | |
遍历 | iterator() | 返回迭代器对象,用于集合遍历 |
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
public class CollectionTest {
@Test
public void test1(){
Collection coll = new ArrayList();
//add()
coll.add("AA");
coll.add(123);
coll.add(new Date());
//size()
System.out.println(coll.size());
//addAll()
Collection coll1 = new ArrayList();
coll1.add("CC");
coll1.add(456);
coll.addAll(coll1);
System.out.println(coll.size());
//clear()
coll.clear();
//isEmpty()
System.out.println(coll.isEmpty());
}
}
>>> 3
5
true
import org.junit.Test;
import java.util.*;
public class CollectionTest {
@Test
public void test1(){
Collection coll = new ArrayList();
//add()
coll.add("AA");
coll.add(123);
coll.add(new Person("Tom",20));
coll.add(false);
coll.add("DD");
boolean contains = coll.contains(123);
System.out.println(contains);
System.out.println(coll.contains(new Person("Tom",20)));//需要重写 equals()
//containsAll()
Collection coll1 = Arrays.asList("AA",456);
System.out.println("containsAll():" + coll.containsAll(coll1));
//remove()
coll.remove(123);
System.out.println("remove():" + coll);
//removeAll()
coll.removeAll(coll1);
System.out.println("removeAll():" + coll);
//retainAll()
Collection coll2 = Arrays.asList(false,2345);
coll.retainAll(coll2);
System.out.println("retainAll():" + coll);
//hashCode()
System.out.println("hashCode():" + coll.hashCode());
coll.add(678);
//集合 ——> 数组 toArray()
Object[] arr = coll.toArray();
for (int i = 0; i < arr.length; i++) {
System.out.println("toArray():" + arr[i]);
}
//数组 ——> 集合 asList()
List<String> list = Arrays.asList(new String[]{"AA","BB"});
System.out.println("asList():" + list);
}
}
class Person{
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return age == person.age &&
Objects.equals(name, person.name);
}
}
>>> true
true
containsAll():false
remove():[AA, CollectionTest.Person@22927a81, false, DD]
removeAll():[CollectionTest.Person@22927a81, false, DD]
retainAll():[false]
hashCode():1268
toArray():false
toArray():678
asList():[AA, BB]