设为首页 加入收藏

TOP

Java中ArrayList HashSet的使用 以及HashCode的用处(一)
2014-11-24 14:51:21 来源: 作者: 【 】 浏览:92
Tags: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

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C#的Process类的一些用法 下一篇Java反射的高级应用,模拟开发环境..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: