This article describes the method of using Hashtable to filter duplicate values in an array. Share it for your reference, as follows:
package org.eline.core.web.support;import java.util.Hashtable;/****************************************** * * @author zdw * */public class ObjectFilter{ public static void main(String[] args) { // String test 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]); } // Plastic Surgery Test 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]); } } /************************************ * Filtering method * * @param arr * Array to filter* @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; }}For more information about Java related content, please check out the topics of this site: "Java Data Structure and Algorithm Tutorial", "Summary of Java Operation DOM Node Tips", "Summary of Java File and Directory Operation Tips" and "Summary of Java Cache Operation Tips"
I hope this article will be helpful to everyone's Java programming.