本文实例讲述了java使用Hashtable过滤数组中重复值的方法。分享给大家供大家参考,具体如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
package org.eline.core.web.support;
import java.util.Hashtable;
/*****************************
*
* @author zdw
*
*/
public class ObjectFilter
{
public static void main(String[] args)
{
// String 测试
String arr[] =
{ "13111" , "13112" , "13111" };
ObjectFilter sf = new ObjectFilter();
Object results[] = sf.filter(arr);
System.out.println(results.length);
for ( int i = 0 ; i < results.length; ++i)
{
System.out.println(results[i]);
}
// 整形测试
Integer arr2[] =
{ 1 , 3 , 5 , 7 , 1 , 2 , 4 , 5 };
Object results2[] = sf.filter(arr2);
for ( int i = 0 ; i < results2.length; ++i)
{
System.out.println(results2[i]);
}
}
/*************************
* 过滤的方法
*
* @param arr
* 要过滤的数组
* @return
*/
public Object[] filter(Object arr[])
{
Hashtable<Object, Object> hashtable = new Hashtable<Object, Object>();
for ( int i = 0 ; i < arr.length; ++i)
{
hashtable.put(arr[i], arr[i]);
}
Object results[] = null ;
results = hashtable.values().toArray();
return results;
}
}
|
希望本文所述对大家java程序设计有所帮助。