核心类库-集合-Collection1

本文深入讲解了Java集合框架的基本概念,包括Collection接口及其子接口List和Set的不同应用场景,以及Map接口的特点。此外,还详细介绍了Collection接口的主要方法,如add、size、addAll等,并通过示例演示了这些方法的具体使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.junit.Test;


/*
  * 集合框架
  * |---Collection接口:单列集合,用来存储一个一个的对象
  * 	|---List接口:存储有序的、可重复的数据。 -->"动态"数组
  * 
  * 	|---Set接口:存储无序的、不可重复的数据  -->高中讲的“集合”
  * 		|---HashSet、LinkedHashSet、TreeSet
  * 	
  *
  *|---Map接口:双列集合,用来存储一对(key-value)一对的数据  -->高中函数:y=f(x)
  *		|---HashMap、LinkedHashMap、TreeMap、Hashtable、Properties
 * 
 * 
 * 
 * 
 * ---------------Collection方法
 *  add(Object e):将元素e添加到集合coll中
 *  coll.add("AA");
 *  coll.add(new Date());
 *  
 *  size():获取添加的元素的个数
 *  System.out.println(coll.size());
 *  
 *  addAll(Collection coll1):将coll1集合中的元素添加到当前的集合中
 *  coll1.add(456);
 *  coll1.add("CC");
 *  coll.addAll(coll1);
 *  
 *  clear():清空集合元素
 *  coll.clear();
 *  
 *  isEmpty():判断当前集合是否为空
 *  System.out.println(coll.isEmpty());
 *  
 * 
 * ---------------Collection方法的测试
 * contaions
 * containsAll
 * remove
 * removeAll
 * 
 * 结论:
 * 向Collection接口的实现类的对象中添加数据obj时,要求obj所在类要重写equals()。
 */
public class CollectionTest1 {

	@Test
	public void test1() {
		Collection coll=new ArrayList();
		coll.add(123);
		coll.add(456);
//		Person p=new Person("Jerry",20);
//		coll.add(p);
		coll.add(new Person("Jerry",20));
		coll.add(new String("Tom"));
		coll.add(false);
//		1.contaions(Object obj):判断当前集合中是否包含obj
//		我们在判断时会调用obj对象所在类的equals()
		boolean contains=coll.contains(123);
		System.out.println(contains);
		System.out.println(coll.contains(new String("Tom")));
//		  System.out.println(coll.contains(p)); //true
		System.out.println(coll.contains(new Person("Jerry",20))); //false-->true
		
		//2.conatinsAll(Collection coll1):判断形参coll1中的所有元素是否都存在与当前集合中。
		Collection coll1=Arrays.asList(123,456);
		System.out.println(coll.containsAll(coll1));
		
		}
	@Test
	public void test2() {
		Collection coll=new ArrayList();
		coll.add(123);
		coll.add(456);
		coll.add(new Person("Jerry",20));
		coll.add(new String("Tom"));
		coll.add(false);
		
		coll.remove(1234);
		System.out.println(coll);
		
		coll.remove(new Person("Jerry",20));
		System.out.println(coll);
		
		//4.removeAll(Collection coll1):从当前集合中移除coll1中所有的元素
		Collection coll1=Arrays.asList(123,456);
		coll.removeAll(coll1);
		System.out.println(coll);
	}
	
	@Test
	public void test3() {
		Collection coll=new ArrayList();
		coll.add(123);
		coll.add(456);
		coll.add(new Person("Jerry",20));
		coll.add(new String("Tom"));
		coll.add(false);
		
//		5.retainAll(Collection coll1):交集:获取当前集合和coll1集合的交集,并返回给当前集合
//		Collection coll1=Arrays.asList(123,456,789);
//		coll.retainAll(coll1);
//		System.out.println(coll);
		
		//6.equals(Object obj):要想返回true,需要当前集合和形参集合的元素都相同
		Collection coll1=new ArrayList();
		coll1.add(456);
		coll1.add(123);
		coll1.add(new Person("Jerry",20));
		coll1.add(new String ("Tome"));
		coll.add(false);
		
		System.out.println(coll.equals(coll1));
	}
	
	@Test
	public void test4() {
		Collection coll=new ArrayList();
		coll.add(123);
		coll.add(456);
		coll.add(new Person("Jerry",20));
		coll.add(new String("Tom"));
		coll.add(false);
		
		//7.hashCode():返回当前对象的哈希值
		System.out.println(coll.hashCode());
		
		//8.集合-->数组:toArray()
		Object[] arr=coll.toArray();
		for(int i=0;i<arr.length;i++) {
			System.out.println(arr[i]);
		}
	
		//拓展:数组 -->集合:调用Arrays类的静态方法asList()
		List<String> list=Arrays.asList(new String[] {"AA","BB","CC"});
		System.out.println(list);
		
		List arr1=Arrays.asList(new int[] {123,456});
		System.out.println(arr1.size());//1
		
		List arr2=Arrays.asList(new Integer[] {123,456});
		System.out.println(arr2.size());
		
		
	//9.iterator():返回Iterator接口的实例,用于遍历集合元素。放在IteratorTest.java中
	
	}
}


-------------------------------------------------------------------------------------------------------
public class Person {

	private String name;
	private int age;
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Person other = (Person) obj;
		if (age != other.age)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Person(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	public Person() {
		super();
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值