文章目录
一、Java集合的整体介绍
- Java 集合类可以用于存储数量不等的多个对象,还可用于保存具有映射关系的
关联数组。很好的应对了数组array的弊端。 - 使用的场景:
①Android客户端 ②服务器 ③端数据库
二、Collection接口
Collection接口:单列数据,定义了存取一组对象的方法的集合。
- Collection 接口是 List、Set 和 Queue 接口的父接口,该接口里定义的方法 既可用于操作 Set 集合,也可用于操作 List 和 Queue 集合。
- JDK不提供此接口的任何直接实现,而是提供更具体的子接口(如:Set和List) 实现。
- Java 集合会丢失容器中所有对象的数据类型,把所有对象都当成 Object 类型处理; 但可以增加泛型,来实现Java 集合记住容器中对象的数据类型。
1、Collection接口方法
方法 | 方法说明 |
---|---|
add(Object obj) | 添加元素 |
addAll(Collection coll) | 添加整个集合 |
int size() | 获取有效元素的个数 |
void clear() | 清空集合 |
boolean isEmpty() | 是否是空集合 |
boolean contains(Object obj) | 是否包含某个元素,是通过元素的equals方法来判断是否是同一个对象 |
boolean containsAll(Collection c) | 是否包含某个元素, 也是调用元素的equals方法来拿两个集合的元素挨个比较。 |
boolean remove(Object obj) | 通过元素的equals方法判断是否是要删除的那个元素。只会删除找到的第一个元素 |
boolean removeAll(Collection coll) | 删除集合中所有的此元素(取当前集合的差集) |
boolean retainAll(Collection c) | 取两个集合中的交集(把交集的结果存在当前集合中,不影响c) |
boolean equals(Object obj) | 判断集合是否相等 |
Object[] toArray() | 转成对象数组 |
hashCode() | 获取集合对象的哈希值 |
iterator() | 返回迭代器对象,用于集合遍历 |
package Collection接口;
import org.junit.Test;
import java.util.*;
public class CollectionTest {
@Test
public void test1(){
Collection coll= new ArrayList();
coll.add(123);
coll.add(456);
coll.add(new String("Jerry"));
Person p = new Person("Tom",15);
coll.add(p);
coll.add(true);
// 1.contains(object obj):判断当前集合中是否包含obj
// 在判断时会调用obj对象所在类的equals()
boolean contains = coll.contains(123);
System.out.println(contains); //true
System.out.println(coll.contains(new String("Jerry"))); //true
System.out.println(coll.contains(new Person("Tom", 15))); //false ---> true 进行重写equals方法
System.out.println(coll.contains(p)); //true
//2.containsAll(Collection coll1) : 判断形参coll1中的所有元素是否都存在于当前集合中
Collection coll1 = Arrays.asList(123,456);
System.out.println(coll.containsAll(coll1)); //true
}
@Test
public void test2(){
//3.remove(Object obj):
Collection coll= new ArrayList();
coll.add(123);
coll.add(456);
coll.add(new String("Jerry"));
Person p = new Person("Tom",15);
coll.add(p);
coll.add(false);
coll.remove(1234);
System.out.println(coll); //[123, 456, Jerry, Collection接口.For_Iterator.Person{name='Tom', age=15}, false]
coll.remove(new Person("Tom",15));
System.out.println(coll); //[123, 456, Jerry, false]
//4.removeAll(Collection coll1):从当前的集合中移除从从coll1中的所有元素
Collection coll1 = Arrays.asList(123,456);
coll.removeAll(coll1);
System.out.println(coll); //[Jerry, false]
}
@Test
public void test3(){
Collection coll = new ArrayList();
coll.add(123);
coll.add(456);
coll.add(new String("Jerry"));
Person p = new Person("Tom",15);
coll.add(p);
coll.add(false);
/* // 5.retainAll(Collection coll): 交集 :获取当前集合和从coll1集合的交集,并返回给当前集合
Collection coll1 = Arrays.asList(123,456,789);
coll.retainAll(coll1);
System.out.println(coll); //[123, 456]*/
//6.equals(Object obj) :
Collection coll1 = new ArrayList(); //Array是有序的,如果add的顺序不同,依然输出false
coll1.add(123);
coll1.add(456);
coll1.add(new String("Jerry"));
coll1.add(new Person("Tom",15));
coll1.add(false);
System.out.println(coll.equals(coll1)); //true
}
@Test
public void test4(){
Collection coll= new ArrayList();
coll.add(123);
coll.add(456);
coll.add(new String("Jerry"));
Person p = new Person("Tom",15);
coll.add(p);
coll.add(false);
// 7.hasCode() : 返回当前对象的哈希值
System.out.println(hashCode());
//8.集合 --> 数组 :toArray()
Object[] arr = coll.toArray();
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
//拓展 :数组 --> 集合 【注意asList要用包装类,否则将视为一个元素】
List<String> strings = Arrays.asList(new String[]{
"AA", "BB", "CC", "DD"});
System.out.println(strings); //[AA, BB, CC, DD]
List ints = Arrays.asList(new int[]{
12, 34, 56});
System.out.println(ints.size()); // 1
List ints1 = Arrays.asList(new Integer[]{
12, 34, 56});
System.out.println(ints1.size()); // 3
// 9.iterator(): 返回Iterator()接口的实例,用于遍历集合元素。放在IteratorTest.java 中测试
}
}
class Person{
private String name;
private int age;
public Person(){
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}