Java中ArrayList HashSet的使用 以及HashCode的用处

Java.uitl包中的 ArrayList 和HashSet类 我们都用过,但是我们可能都没有去,深入研究过其内部的结构 。都是实现了Collection的类 ,Collection是一个标准

ArrayList

其实就相当与一个动态数组,我们每增加一个元素,他啊都会将元素增加到ArrayList中并且为这个元素分配指定索引 就像一个数组一样 。这个索引就是从0开始 1 2 34 。。。。

HashSet

看到Hash我们就知道,它的内部结构了,学过数据结构我们都知道hash表是如何插入元素 和 搜索元素,利用hash表我们可以快速的查找元素,而不用像数组一样进行遍历 。

在HashSet中 我们插入元素的时候 ,他会首先判断HashSet中否有和这个元素相等的元素,如果有 那么就不插入 。所以说HashSet中没有重复元素。

HashSet可以实现元素的快速查找,这是利用hashCode来实现的 。

HashCode是怎样获得的呢? 在java中 如果我们不覆盖父类的 hashCode方法 那么hashCode是根据对象的内存地址计算而来。 当然我们也可以重载hashCode方法 。

在哈希表中,比如说有1-31 的索引 ,我们不是按照常规插入一个元素 索引增加一个,而是根据对象的hashCode来计算这个对象应该在表的哪个位置 .然后就把这个元素插入到这个索引对应的区域。对应的查找的时候也是这样,是根据 对象的hashCode直接 查找指定索引对应的元素,这样大大提高了速度 。

如果我们自己重写了 hashCode() 我们不应该修改产生hashCode的字段,否则的话可能导致内存的泄漏、

看下面几个程序的结果: 有一个类

class MyTest
{
int x;
int y;
public MyTest(int x,int y)
{
this.x=x ;
this.y=y ;
}
}

1、

package me.test;
import java.util.*;
public class ReflectTest2
{
public static void main(String[]args)
{
//面向父类的编程或者面向接口编程
Collection c1=new ArrayList() ; //ArrayList类是一个数组 实现了Collection接口
MyTest t1=new MyTest(2,2) ;
MyTest t2=new MyTest(3,4) ;
MyTest t3=new MyTest(2,2) ;
c1.add(t1) ;
c1.add(t2) ;
c1.add(t3) ;
c1.add(t1) ;
System.out.println(c1.size());
}

}

2、

package me.test;
import java.util.*;
public class ReflectTest2
{
public static void main(String[]args)
{
//面向父类的编程或者面向接口编程
Collection c1=new HashSet() ;
MyTest t1=new MyTest(2,2) ;
MyTest t2=new MyTest(3,4) ;
MyTest t3=new MyTest(2,2) ;
c1.add(t1) ;
c1.add(t2) ;
c1.add(t3) ;
c1.add(t1) ;
System.out.println(c1.size());
}

}


3、

package me.test;
import java.util.*;
public class ReflectTest2
{
public static void main(String[]args)
{
//面向父类的编程或者面向接口编程
Collection c1=new HashSet() ;
MyTest t1=new MyTest(2,2) ;
MyTest t2=new MyTest(3,4) ;
MyTest t3=new MyTest(2,2) ;
c1.add(t1) ;
c1.add(t2) ;
c1.add(t3) ;
c1.add(t1) ;
System.out.println(c1.size());
}

}
class MyTest
{
int x;
int y;
public MyTest(int x,int y)
{
this.x=x ;
this.y=y ;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MyTest other = (MyTest) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
}

4、

package me.test;
import java.util.*;
public class ReflectTest2
{
public static void main(String[]args)
{
//面向父类的编程或者面向接口编程
Collection c1=new HashSet() ;
MyTest t1=new MyTest(2,2) ;
MyTest t2=new MyTest(3,4) ;
MyTest t3=new MyTest(2,2) ;
c1.add(t1) ;
c1.add(t2) ;
c1.add(t3) ;
c1.add(t1) ;
t1.y=0;
c1.remove(t1);
System.out.println(c1.size());
}

}
class MyTest
{
int x;
int y;
public MyTest(int x,int y)
{
this.x=x ;
this.y=y ;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MyTest other = (MyTest) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值