import java.util.*;
/**
* 数组存储数据的特点:
* ①一旦初始化,大小确定
* ②一旦定义,存储的类型确定且相同。
* 数组存储的弊端:
* ①大小无法改变。
* ②方法较少,插入、删除元素不便且效率较低。
* ③数据有序、可重复,对于无序、不可重复的无法满足要求。
* ④无法直接获取数组中实际元素的个数。
*/
/** Collection接口方法集合
col.add(); //添加单个元素
col.addAll(); //传入Collection
col.size(); //实际存储的个数
col.isEmpty(); //判断是否为空
col.contains(); //判断是否包含,调用equals()方法
col.containsAll();
col.iterator(); //返回迭代器对象
col.toArray(); //返回Object类型的数组
col.remove(); //移除,查找时调用equals()方法 删除成功,返回true 否则返回false
col.removeAll(); //求差集,并修改当前集合
col.retainAll(); //求交集
col.clear(); //清空
col.hashCode();
col.equals();
*/
public class CollectionTest {//单列集合15个方法测试
public static void main(String[] args) {
//1.Collection下的方法*********************
Collection col1 = new ArrayList();
col1.add(123);
col1.add("Jack");
col1.add(new Person(12,"Lisa"));
col1.add(false);
col1.add(12.0);
System.out.println(col1.contains(new Person(12,"Lisa"))); //true Person类重写了equals()方法
System.out.println(col1.size());//5
System.out.println(col1.isEmpty());//false
Object[] arr = col1.toArray();//返回Object数组
for(Object obj: arr){
System.out.println(obj);
}
//123
//Jack
//ColletionStudy.Person@2429e0 Person类没有实现toString()方法
//false
//12.0
Collection col2 = new ArrayList();
col2.add(123);
col2.add("Jack");
col2.add(new Person(12,"Lisa"));
col1.retainAll(col2);//求交集,并修改col1
System.out.println("交集:");
for(Object obj:col1){
System.out.println(obj);
}
col1.removeAll(col2);//col中删除col2的部分
for(Object obj:col1){
System.out.println(obj);
}
//false
//12.0
//1end***********************************
//2.迭代器遍历*****************
Iterator iterator = col1.iterator();//每次调用iterator()返回的都是一个新的迭代器。指针指向第一个元素的前一个位置。
while (iterator.hasNext()){//迭代器原理:刚开始,指针指向第一个元素的前一个位置。
System.out.println(iterator.next());//先调用next,指针下移;再返回下移后指针指向的元素
iterator.remove();
//调用迭代器的方法,移除当前元素,而不是调集合的方法
}
//可能报NoSuchElementException异常:已经没有了,还调用next()
//可能报IllegalStateException异常:没有指向,掉remove()
//2end**************************
//3.Arrays.asList()***************
List list1 = Arrays.asList(123,456);//传入可变参数
System.out.println("list1 = " + list1);//list1 = [123, 456]
List list2 = Arrays.asList(new int[]{123,456});//会被当成一个元素
System.out.println("list2 = " + list2);//list2 = [[I@75b84c92]
//error: List<Integer> list3 = Arrays.asList(new int[]{123,456});
List<Integer> list3 = Arrays.asList(new Integer[]{123,456});
System.out.println("list3 = " + list3);//list3 = [123, 456]
List list4 = Arrays.asList(new Integer[]{123,456});//传入对象数组
System.out.println("list4 = " + list3);//list3 = [123, 456]
//3end*******************************
}
}
import java.util.Objects;
/**
* @author :Zhifei Zhang
* @date :Created in 2021-03-03 23:31
* @description:
* @modified By:Hickey
* @version: $
*/
public class Person {
int age;
String name;
public Person() {
}
public Person(int age, String name) {
this.age = age;
this.name = name;
}
@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);
}
@Override
public int hashCode() {
return Objects.hash(age, name);
}
}
原文:https://www.cnblogs.com/WhiteThornZzf/p/14489107.html