• Collection、List和Set接口


    Collection:

           Collection集合的特征:无序,允许重复
           “public interface Collection extends Iterable”一般说Collection是集合框架的顶级接口,但是事实上并不是顶级接口,它继承于Iterable接口。
           Iterable接口表示当前对象是可遍历的,要求具体实现类中提供了一个遍历器Iterator的实现
           forEach(Consumer) 使用lambda表达式遍历所有的元素,实际底层实现为foreach结构,能够使用foreach结构前提是必须实现Iterable接口
           List和Set 是Collection的两个重要的子接口

    常见的方法有:

    1.  int size();获取集合中的元素个数
    2. boolean contains(Object o) 判断集合中是否有指定的对象。元素相等是依赖于equals方法实现的
    3. Object[] toArray();将集合转换为Object类型的数组
    4. boolean add(E e);向集合中新增元素
    5. boolean remove(Object o);删除指定的元素o,相等判断使用equals
    6. void clear(); 清空集合中的所有元素
    1. import java.util.ArrayList;
    2. import java.util.Collection;
    3. import java.util.Objects;
    4. public class Test1 {
    5. public static void main(String[] args) {
    6. Collection cc = new ArrayList();
    7. A1 aa = new A1();
    8. cc.add(aa);
    9. cc.add(aa);
    10. System.out.println(cc.size());
    11. System.out.println(cc.remove(new A1()));
    12. System.out.println(cc.size());
    13. }
    14. }
    15. class A1 {
    16. private Long id;
    17. private String name;
    18. public Long getId() {
    19. return id;
    20. }
    21. public void setId(Long id) {
    22. this.id = id;
    23. }
    24. public String getName() {
    25. return name;
    26. }
    27. public void setName(String name) {
    28. this.name = name;
    29. }
    30. @Override
    31. public int hashCode() {
    32. return Objects.hash(id, name);
    33. }
    34. @Override
    35. public boolean equals(Object obj) {
    36. // 用户自定义的比较规则
    37. if (this == obj)
    38. return true;
    39. if (obj == null) //当前对象不可能为null,否则空指针异常
    40. return false;
    41. if (getClass() != obj.getClass()) // 类型判断。一个类只能加载一次
    42. return false;
    43. A1 other = (A1) obj;
    44. //调用Objects工具类中的方法进行相等判断
    45. /* public static boolean equals(Object a, Object b) {
    46. return (a == b) || (a != null && a.equals(b));
    47. }
    48. */
    49. return Objects.equals(id, other.id) && Objects.equals(name, other.name);
    50. }
    51. }

    List接口:

          List集合的特征:有序【有下标序号】   允许重复
          “public interface List extends Collection”List接口继承于Collection接口,但特殊的方法 就是引入序号

    常见方法有:

          E get(int index);按照索引序号获取指定位置上的元素,序号不能越界
          E set(int index, E element);修改指定位置上的元素,一般是覆盖
          void add(int index, E element);向指定位置上添加元素,原始数据后移
          E remove(int index);删除指定位置上的元素,并返回被删除的元素,原始位置上的元素前移,但删除方法有可能产生二义性

    • list.remove(3);  调用的是remove(int)方法,不是指定元素删除,而是指定位置删除
    • list.remove(Integer.valueOf(3)); 删除指定元素,不是序号

    同时List有三个实现类:

    • - ArrayList:数组实现,查询快,增删慢,轻量级;(线程不安全)
    • - LinkedList:双向链表实现,增删快,查询慢 (线程不安全)
    • - Vector:数组实现,重量级 (线程安全、使用少)
    1. import java.util.ArrayList;
    2. import java.util.Iterator;
    3. import java.util.List;
    4. import java.util.Objects;
    5. public class Test1 {
    6. public static void main(String[] args) {
    7. List list = new ArrayList();
    8. list.add(new A1());
    9. list.add(new A1());
    10. System.out.println("add:"+list.size());
    11. list.remove(new A1());
    12. System.out.println("remove:"+list.size());
    13. for(int i=0;i<5;i++)
    14. list.add(i); //自动装箱操作
    15. Iterator it=list.iterator();
    16. while(it.hasNext()) {
    17. Object tmp=it.next();
    18. System.out.println(tmp);
    19. }
    20. }
    21. }
    22. class A1 {
    23. private Long id;
    24. private String name;
    25. public Long getId() {
    26. return id;
    27. }
    28. public void setId(Long id) {
    29. this.id = id;
    30. }
    31. public String getName() {
    32. return name;
    33. }
    34. public void setName(String name) {
    35. this.name = name;
    36. }
    37. @Override
    38. public int hashCode() {
    39. return Objects.hash(id, name);
    40. }
    41. @Override
    42. public boolean equals(Object obj) {
    43. // 用户自定义的比较规则
    44. if (this == obj)
    45. return true;
    46. if (obj == null) // 当前对象不可能为null,否则空指针异常
    47. return false;
    48. if (getClass() != obj.getClass()) // 类型判断。一个类只能加载一次
    49. return false;
    50. A1 other = (A1) obj;
    51. // 调用Objects工具类中的方法进行相等判断
    52. /*
    53. * public static boolean equals(Object a, Object b) { return (a == b) || (a !=
    54. * null && a.equals(b)); }
    55. */
    56. return Objects.equals(id, other.id) && Objects.equals(name, other.name);
    57. }
    58. }

    Set接口:

          Set集合的特征:无序【没有下标序号】 不允许重复
          Set接口中的所有方法都继承于Collection接口,没有新方法,允许使用null元素,只对 add()、equals() 和 hashCode() 方法添加了限制。
          Set中元素的存放顺序与元素的插入时间无关,是根据元素的hashCode值来排列的。如果hashcode值一样,则用equals判断值是否相等,相等则不存,不相等则存进来。
    HashSet和TreeSet是Set的实现
          Set接口—>HashSet实现类 --> LinkedHashSet【在hashSet的基础上给每个存储的元素额外添加一个链表,用于记录添加元素的顺序】
          SortedSet —> TreeSet

    1. import java.util.HashSet;
    2. import java.util.Objects;
    3. import java.util.Set;
    4. public class Test1 {
    5. public static void main(String[] args) {
    6. Set set = new HashSet();
    7. set.add(new A1(99L, "zhangsan"));
    8. set.add(new A1(88L, "lisi"));
    9. set.add(new A1(99L, "wangwu"));
    10. set.forEach(System.out::println);
    11. }
    12. }
    13. class A1 {
    14. private Long id;
    15. private String name;
    16. @Override
    17. public int hashCode() {
    18. System.out.println(this+"::hashcode()");
    19. return id.hashCode();
    20. }
    21. //比较规则为:按照id进行比较,如果id相等则对象相等
    22. public boolean equals(Object obj) {
    23. System.out.println(this+"::equals()");
    24. if (this == obj)
    25. return true;
    26. if (obj == null)
    27. return false;
    28. if (getClass() != obj.getClass())
    29. return false;
    30. A1 other = (A1) obj;
    31. return Objects.equals(id, other.id);
    32. }
    33. public A1(Long id, String name) {
    34. super();
    35. this.id = id;
    36. this.name = name;
    37. }
    38. public Long getId() {
    39. return id;
    40. }
    41. public void setId(Long id) {
    42. this.id = id;
    43. }
    44. public String getName() {
    45. return name;
    46. }
    47. public void setName(String name) {
    48. this.name = name;
    49. }
    50. @Override
    51. public String toString() {
    52. return "A1 [id=" + id + ", name=" + name + "]";
    53. }
    54. }

  • 相关阅读:
    【小月电子】国产安路FPGA开发板系统学习教程-LESSON9简易测试系统
    多行文本转成一行的实现方法
    干货 | 读懂这篇文,玩游戏还会卡顿?
    信号和电源隔离的有效设计技术
    web 面试高频考点 —— HTTP 篇
    Web Components详解-Shadow DOM样式控制
    猿创征文|MySQL基本查询语句的应用(有实例与代码)
    身份证阅读器和社保卡读卡器Harmony鸿蒙系统ArkTS语言SDK开发包
    oracle常用命令
    android基础学习
  • 原文地址:https://blog.csdn.net/m0_59749255/article/details/126582065