Collection集合的特征:无序,允许重复
“public interface Collection
Iterable接口表示当前对象是可遍历的,要求具体实现类中提供了一个遍历器Iterator的实现
forEach(Consumer) 使用lambda表达式遍历所有的元素,实际底层实现为foreach结构,能够使用foreach结构前提是必须实现Iterable接口
List和Set 是Collection的两个重要的子接口
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Objects;
-
- public class Test1 {
- public static void main(String[] args) {
- Collection cc = new ArrayList();
- A1 aa = new A1();
- cc.add(aa);
- cc.add(aa);
- System.out.println(cc.size());
-
- System.out.println(cc.remove(new A1()));
- System.out.println(cc.size());
- }
- }
-
- class A1 {
- private Long id;
- private String name;
-
-
-
- public Long getId() {
- return id;
- }
-
- public void setId(Long id) {
- this.id = id;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- @Override
- public int hashCode() {
- return Objects.hash(id, name);
- }
-
- @Override
- public boolean equals(Object obj) {
- // 用户自定义的比较规则
- if (this == obj)
- return true;
- if (obj == null) //当前对象不可能为null,否则空指针异常
- return false;
- if (getClass() != obj.getClass()) // 类型判断。一个类只能加载一次
- return false;
- A1 other = (A1) obj;
- //调用Objects工具类中的方法进行相等判断
- /* public static boolean equals(Object a, Object b) {
- return (a == b) || (a != null && a.equals(b));
- }
- */
- return Objects.equals(id, other.id) && Objects.equals(name, other.name);
- }
-
- }
List集合的特征:有序【有下标序号】 允许重复
“public interface List
E get(int index);按照索引序号获取指定位置上的元素,序号不能越界
E set(int index, E element);修改指定位置上的元素,一般是覆盖
void add(int index, E element);向指定位置上添加元素,原始数据后移
E remove(int index);删除指定位置上的元素,并返回被删除的元素,原始位置上的元素前移,但删除方法有可能产生二义性
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Objects;
-
- public class Test1 {
- public static void main(String[] args) {
- List list = new ArrayList();
- list.add(new A1());
- list.add(new A1());
- System.out.println("add:"+list.size());
- list.remove(new A1());
- System.out.println("remove:"+list.size());
- for(int i=0;i<5;i++)
- list.add(i); //自动装箱操作
- Iterator it=list.iterator();
- while(it.hasNext()) {
- Object tmp=it.next();
- System.out.println(tmp);
- }
- }
- }
-
- class A1 {
- private Long id;
- private String name;
-
- public Long getId() {
- return id;
- }
-
- public void setId(Long id) {
- this.id = id;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- @Override
- public int hashCode() {
- return Objects.hash(id, name);
- }
-
- @Override
- public boolean equals(Object obj) {
- // 用户自定义的比较规则
- if (this == obj)
- return true;
- if (obj == null) // 当前对象不可能为null,否则空指针异常
- return false;
- if (getClass() != obj.getClass()) // 类型判断。一个类只能加载一次
- return false;
- A1 other = (A1) obj;
- // 调用Objects工具类中的方法进行相等判断
- /*
- * public static boolean equals(Object a, Object b) { return (a == b) || (a !=
- * null && a.equals(b)); }
- */
- return Objects.equals(id, other.id) && Objects.equals(name, other.name);
- }
-
- }
Set集合的特征:无序【没有下标序号】 不允许重复
Set接口中的所有方法都继承于Collection接口,没有新方法,允许使用null元素,只对 add()、equals() 和 hashCode() 方法添加了限制。
Set中元素的存放顺序与元素的插入时间无关,是根据元素的hashCode值来排列的。如果hashcode值一样,则用equals判断值是否相等,相等则不存,不相等则存进来。
HashSet和TreeSet是Set的实现
Set接口—>HashSet实现类 --> LinkedHashSet【在hashSet的基础上给每个存储的元素额外添加一个链表,用于记录添加元素的顺序】
SortedSet —> TreeSet
- import java.util.HashSet;
- import java.util.Objects;
- import java.util.Set;
-
- public class Test1 {
- public static void main(String[] args) {
- Set set = new HashSet();
- set.add(new A1(99L, "zhangsan"));
- set.add(new A1(88L, "lisi"));
- set.add(new A1(99L, "wangwu"));
- set.forEach(System.out::println);
- }
- }
-
- class A1 {
- private Long id;
- private String name;
- @Override
- public int hashCode() {
- System.out.println(this+"::hashcode()");
- return id.hashCode();
- }
- //比较规则为:按照id进行比较,如果id相等则对象相等
- public boolean equals(Object obj) {
- System.out.println(this+"::equals()");
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- A1 other = (A1) obj;
- return Objects.equals(id, other.id);
- }
-
- public A1(Long id, String name) {
- super();
- this.id = id;
- this.name = name;
- }
-
- public Long getId() {
- return id;
- }
-
- public void setId(Long id) {
- this.id = id;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- @Override
- public String toString() {
- return "A1 [id=" + id + ", name=" + name + "]";
- }
-
- }