本人菜鸟,前几天学习数据结构,看到hashtable,被它的优点深深的吸引了,
突发奇想,想到一个利用hashtable去掉集合中重复的数据,这里贴出代码,大家别笑我package fx.algorithm.hash.app;/**
* 利用hashtable去掉重复的数据
*
* @author 咖啡
*
*/
public class HashTableApp3 { private static int[] array = { 10, 10, 2, 3, 5, 7, 8, 2, 9, 2, 5, 6, 7 }; public static void main(String[] args) {
HashTable table = new HashTable(array.length);
for (int i = 0; i < array.length; i++) {
table.insert(new DataItem(array[i]));
} table.displayTable();
}
}/**
* 封装数据,可要可无
*
* @author 咖啡
*
*/
class DataItem { private int iData; public DataItem(int ii) {
this.iData = ii;
} public int getkey() {
return iData;
}
}/**
* 自定义一个table
*
* @author 咖啡
*
*/
class HashTable { private DataItem[] hashArray;
private int arraySize; public HashTable(int size) {
arraySize = size;
hashArray = new DataItem[arraySize];
} /**
* 显示table
*/
public void displayTable() {
System.out.print("Table ");
for (int i = 0; i < arraySize; i++) {
if (hashArray[i] != null) {
System.out.print(hashArray[i].getkey() + " ");
}
}
System.out.println();
} /**
* 将值直接作为table中的下标,如果该下标存在一个数据说明有冲突,不管
*/
public void insert(DataItem item) {
int key = item.getkey();
if (hashArray[key] == null) {
hashArray[key] = item;
}
}
}自己分析一下,复杂度为O(n),比较时间也是O(n),需要一个与集合相同大小的数组,遍历也是O(n)
如果集合中的数据值很大的话,可以采用比较的哈希函数进行压缩一下
突发奇想,想到一个利用hashtable去掉集合中重复的数据,这里贴出代码,大家别笑我package fx.algorithm.hash.app;/**
* 利用hashtable去掉重复的数据
*
* @author 咖啡
*
*/
public class HashTableApp3 { private static int[] array = { 10, 10, 2, 3, 5, 7, 8, 2, 9, 2, 5, 6, 7 }; public static void main(String[] args) {
HashTable table = new HashTable(array.length);
for (int i = 0; i < array.length; i++) {
table.insert(new DataItem(array[i]));
} table.displayTable();
}
}/**
* 封装数据,可要可无
*
* @author 咖啡
*
*/
class DataItem { private int iData; public DataItem(int ii) {
this.iData = ii;
} public int getkey() {
return iData;
}
}/**
* 自定义一个table
*
* @author 咖啡
*
*/
class HashTable { private DataItem[] hashArray;
private int arraySize; public HashTable(int size) {
arraySize = size;
hashArray = new DataItem[arraySize];
} /**
* 显示table
*/
public void displayTable() {
System.out.print("Table ");
for (int i = 0; i < arraySize; i++) {
if (hashArray[i] != null) {
System.out.print(hashArray[i].getkey() + " ");
}
}
System.out.println();
} /**
* 将值直接作为table中的下标,如果该下标存在一个数据说明有冲突,不管
*/
public void insert(DataItem item) {
int key = item.getkey();
if (hashArray[key] == null) {
hashArray[key] = item;
}
}
}自己分析一下,复杂度为O(n),比较时间也是O(n),需要一个与集合相同大小的数组,遍历也是O(n)
如果集合中的数据值很大的话,可以采用比较的哈希函数进行压缩一下
解决方案 »
- 求各位推荐几本关于数据结构和算法的书
- 算法题,求优化
- 我也来请教一下关于XStream的问题,请大虾们指教
- 大家好, 有一个java包访问的问题请教一下
- JAVA Calendar 类 setFirstDayOfWeek 方法详解
- comm串口问题,javax.comm.PortInUseException: Port currently owned by Unknown Windows Application
- java中字体的简单问题,求教~~
- 请问..有没有网络中线程的模式参考???
- 哪位提供一个jb7的注册码
- java事件响应,没有main方法
- 求助:编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节数截取的字符串
- groovy是什么?
private static int[] array = { 10, 10, 2, 3, 5, 7, 8, 2, 9, 2, 5, 6, 7 };
Set<Integer> set=new Set<Integer>();
for (int i = 0; i < array.length; i++) {
set.add((Integer)array[i]);
}
不过建议LZ不要用HashTable,那个类感觉快要被抛弃了....用HashSet或者HashMap或许会好点只是建议啦建议...........
什么是数据结构?
所谓的Set属于集合,那个不算,要自己写的
Set <Integer> set=new Set <Integer>();
for (int i = 0; i < array.length; i++) {
set.add((Integer)array[i]);
}
你将如何把1000000001放到长度只有3的数组中?
支持!!!