集合——Collection接口

87 阅读3分钟

集合

  • 容器,
  • 数组,数组长度一旦确定 不能改变 new int[5] ; 数组元素类型必须保持一致
  • 集合长度不限制,集合中的元素类型不要求,集合中只能放对象,不能放基本数据类型的值 ;
  • 数组,共享单车 ;动态库容,删除 数组元素移动;
  • 集合内部提供了的 较多的数据结构和算法

1. Collection接口

  • JDK不提供此接口的任何直接实现,而是提供更具体的子接口 (如: Set和List) 去实现。
  • Collection 接口是 List和Set接口的父接口,该接口里定义的方法既可用于操作 Set 集合,也可用于操作 List 集合。

1.1 添加元素

add(E obj):添加元素对象到当前集合中
addAll(Collection other):添加other集合中的所有元素对象到当前集合中,即this = this ∪ other

//父类引用指向 子类对象
Collection collection = new ArrayList();
// add添加集合元素
collection.add("java");
// Integer in = 12;
collection.add(12);//自动装箱
collection.add("oracle");
System.out.println(collection);  //[java, 12, oracle]
    
Collection collection2 = new ArrayList();
collection2.add("hello");
collection2.add("world");
collection2.add(12);

//addAll()
collection.addAll(collection2);
System.out.println(collection);  //[java, 12, oracle, hello, world, 12]      

MARK
collection.add(collection2) 与 collection.addAll(collection2)区别
image.png

1.2 判断

int size():获取当前集合中实际存储的元素个数
boolean isEmpty():判断当前集合是否为空集合
boolean contains(Object obj): 判断当前集合中是否存在一个与 obj 对象 equals 返回 true 的元素
boolean containsAll(Collection coll): 判断 coll 集合中的元素是否在当前集合中都存在。即 coll 集合是否是当前集合的“子集”
boolean equals(object obj): 判断当前集合与 obj 是否相等

Collection collection2 = new ArrayList();
collection2.add("hello");

Collection collection = new ArrayList();
collection.add("java");
collection.add(12);//自动装箱
collection.add("oracle");
collection.add(128);
collection.add(new String("你好"));
System.out.println(collection);

//isEmpty()
System.out.println(collection.isEmpty());//false

//size()
System.out.println(collection.size());//5

//contains()
System.out.println(collection.contains("java"));//true
System.out.println(collection.contains(128));//true
System.out.println(collection.contains(new String("你好")));//true

//containsAll()
System.out.println(collection.containsAll(collection2));//false

1.3 删除元素

void clear(): 清空集合元素
boolean remove(object obj) : 从当前集合中删除第一个找到的与 obj 对象 equals 返回 true 的元素
boolean removeAll(Collection col): 从当前集合中删除所有与coll集合中相同的元素
boolean retainAll(Collection col): 从当前集合中删除两个集合中不同的元素,使得当前集合仅保留与coll集合中的元素相同的元素,即当前集合中仅保留两个集合的交集

   Collection collection = new ArrayList();
   collection.add("java");
   collection.add(12);//自动装箱
   collection.add("oracle");
   collection.add(12);
   collection.add(new String("你好"));
   System.out.println(collection);//[java, 12, oracle, 12, 你好]

   //clear()
 //  collection.clear();
//   System.out.println(collection);

   //remove()
   System.out.println(collection.remove(12));
   System.out.println(collection);//[java, oracle, 12, 你好]
   System.out.println(collection.remove("java"));
   System.out.println(collection);//[oracle, 12, 你好]

1.4 其他

objectl[] toArray(): 返回包含当前集合中所有元素的数组
hashCode(): 获取集合对象的哈希值
iterator():返回选代器对象,用于集合遍历

Collection collection = new ArrayList();
collection.add("java");
collection.add(12);//自动装箱
collection.add("oracle");
collection.add(12);
System.out.println(collection);

//集合转数组   toArray()
Object[] A1 = collection.toArray();
System.out.println(Arrays.toString(A1));//[java, 12, oracle, 12]
  
//数组转集合   Arrays.asList()    
String[] arr = new Stiing[]{"AA","BB","CC"};
Collection list = Arrays.asList(arr);
System.out.println(list);

1.5 Iterator迭代器

Collection collection = new ArrayList();
collection.add("java");
collection.add(12);
collection.add("oracle");
collection.add(12);
System.out.println(collection);

//获取迭代器对象
Iterator iterator = collection.iterator();

//遍历整个容器
while (iterator.hasNext()){
    System.out.println(iterator.next());//next():①指针下移 ②将下移以后集合位置上的元素返回
MARK

foreach循环----用来遍历数组、集合。

格式:
    for(要遍历的集合或数组元素的类型 临时变量 :要遍历的集合或数组变量)
    {
        操作临时变量的输出
    }
    

//遍历集合
for(Object obj : collection){
    System.out.println(obj);
}
//遍历数组
int[] arr = new int[]{1,2,3,4,5};
for(int a : arr){
    System.out.println(a);
}

说明:

针对于集合来讲,增强 for 循环的底层仍然使用的是迭代器。
增强 for 循环的执行过程中,是将集合或数组中的元素依次赋值给临时变量。注意,循环体中对临时变量的修改,可能不会导致原有集合或数组中元素的修改。