集合の总结_(:з」∠)_

本文探讨了ArrayList的动态数组特性、HashSet的唯一性和不可重复元素,以及HashMap的键值对存储和无序性。还介绍了如何使用Iterator进行集合遍历。同时涵盖了Hashcode和equals方法的实现细节。

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

ArrayList

  1. 底层由数组实现
  2. 元素有序且可以重复
  3. 可以动态增长,以满足应用程序的需求
  4. 元素值可以为null

HashSet

  1. 元素无序并且不可以重复
  2. 只允许一个null元素

HashMap

  1. 键不能重复
  2. 允许使用null值和null键
  3. HashMap中的Entry对象是无序排列的

Iterator(迭代器)

Iterator接口以统一的方式对各种集合元素进行遍历

Iterator<String> it=set.iterator();
while(it.hasNext()){
    System.out.print(it.next()+" ";
}

HashCode()

public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + age;
    result = prime * result + ((name == null) ? 0 : name.hashCode());
    result = prime * result + ((species = = null) ? 0 : species.hashCode());
    return result;
}

equals()
 

public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if(obj.getClass()==Cat.class){
        Cat cat=(Cat)obj;
    return cat.getName().equals(name)&&(cat.getAge()==age)&&cat.getSpecies().equals(species);
    }
    return false;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值