本文实例讲述了java使用Hashtable过滤数组中重复值的方法。分享给大家供大家参考,具体如下:
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程序设计有所帮助。