集合目录
一、Java集合框架概述
1、Java集合框架概述
为什么使用集合,而不使用数组,下面来看看数组的弊端:
a、集合的使用场景
b、Collection接口继承树
详细版:
从上面的集合框架图可以看到,Java 集合框架主要包括两种类型的容器,一种是集合(Collection),存储一个元素集合,另一种是图(Map),存储键/值对映射。Collection 接口又有 3 种子类型,List、Set 和 Queue,再下面是一些抽象类,最后是具体实现类,常用的有 ArrayList、LinkedList、HashSet、HashMap 等等。
c、Map接口继承树
二、 Collection接口方法
1、Collection 接口方法
实例:
package ex.helllo.er.day25;
import org.junit.Test;
import java.util.*;
/**
* @Description:
* @Author: dyq
* @Date: 2021/5/8
*/
public class CollectionTest {
@Test
public void Test1(){
Collection collection = new ArrayList();
//add(Object e)
collection.add("AA");
collection.add("BB");
collection.add("CC");
collection.add(new Date());
collection.add(123);//自动装箱
collection.add('女');
//size();
System.out.println("集合长度:"+collection.size()); //集合长度:6
//addAlL(Collection coll1):将coLl1集合中的元素添加到当前的集合中
Collection collection1 = new ArrayList();
collection1.add("牛逼");
collection1.add(682645223L);
collection.addAll(collection1);
System.out.println("集合长度:"+collection.size()); //集合长度:8
//boolean isEmpty() //判断集合是否为空
System.out.println("判断集合是否为空"+collection.isEmpty()); //false
//遍历集合
System.out.println("*******遍历集合***********");
for (Object collection2:collection) {
System.out.println(collection2);
}
//清空集合
collection.clear();
System.out.println("清空集合后,判断集合是否为空"+collection.isEmpty()); //true
}
@Test
public void Test2(){
Collection coll = new ArrayList();
coll.add("AA");
coll.add(new Date());
coll.add(123);//自动装箱
coll.add('女');
coll.add(new String("Tom"));
// Person p =new Person("Jerry",20);
// coll.contains(p);
coll.add(new Person("Jerry",20));
coll.add(new String("Tom"));
coll.add(false);
//1. boolean contains(Object o);判断当前集合中是否包含obj
//我们在判断时会调用obj对象所在类的equals()
boolean contains = coll.contains(123);
System.out.println(contains);//true
System.out.println(coll.contains(new String("Tom"))); //true
// System.out.println(coll.contains(p));//true
System.out.println(coll.contains(new Person("jak",20)));//false
//2. boolean containsAll(Collection<?> c);判断形参coll1中的所有元紊是否都存在于当前集合电
Collection coll1 = Arrays.asList(123,456);
System.out.println(coll.containsAll(coll1)); //false
}
@Test
public void Test3(){
Collection coll3 = new ArrayList();
//add(Object e)
coll3.add(123);
coll3.add(345);
coll3.add("678");
coll3.add('男');
boolean q = coll3.remove(123);
System.out.println(q); //true
System.out.println(coll3); //[345, 678, 男]
//removeAll(Collection coll1):从当前集合中移除coll1当中所有的元素
Collection coll1 = Arrays.asList(123,456);
coll3.removeAll(coll1);
System.out.println(coll1); //[123, 456]
}
@Test
public void Test4(){
Collection coll = new ArrayList();
coll.add(123);
coll.add(345);
coll.add(678);
coll.add('女');
//5.retainAll(Collection coll1):交集:获取当前集合和coll1集合的交集,并返回给当前集合
// Collection coll1 = Arrays.asList(123,345,'男');
// coll.retainAll(coll1);
// System.out.println(coll); //[123, 345]
//equals(Object obj):
Collection coll1 = new ArrayList();
coll1.add(123);
coll1.add(345);
coll1.add(678);
coll1.add('女');
System.out.println(coll.equals(coll1)); //true 如果123和345调换位置结果就为flase 因为这是有序的集合
}
@Test
public void Test5(){
Collection coll = new ArrayList();
coll.add("AA");
coll.add(new Date());
coll.add(123);//自动装箱
coll.add('女');
coll.add(new String("Tom"));
coll.add(false);
//hashCode():返回当前对象的哈希值
System.out.println(coll.hashCode()); //1440641423
//集合----》数组:toArray()
Object[] arr = coll.toArray();
for (int i =0;i<arr.length;i++){
System.out.print(arr[i]+" "); //AA Sat May 08 17:05:40 CST 2021 123 女 Tom false
}
//拓展:数组----》集合
List<String> list= Arrays.asList(new String[]{"AA", "BB", "CC", "DD"}); //[AA, BB, CC, DD]
System.out.println(list);
List arr1 = Arrays.asList(123,456);
System.out.println(arr1); //[123, 456]
List arr3 = Arrays.asList(new int[]{123,456});
System.out.println(arr3.size()); //1
List arr2 = Arrays.asList(new Integer[]{13,56});
System.out.println(arr2.size());//2
}
}