java去除数组中的重复元素
String[] data = {"江剑臣","武凤楼","李鸣","曹玉","侯国英","李文莲","马小倩","李鸣","曹玉","侯国英"};
1.用HashSet(去除重复元素后,数组也无序)
/**
* HashSet方法(去除重复元素后数组无序)
* @param data
* @return
*/
public static String[] getUniqueHashSet(String[] data){
Set<String> set = new HashSet<String>(Arrays.asList(data));
String[] oArray = set.toArray(new String[] {});
return oArray;
}
public static String[] getUniqueHashMap(String[] data){
List<String> list = Arrays.asList(data);
Iterator<String> it = list.iterator();
Map<String,String> map = new HashMap<String,String>();
while(it.hasNext()){
String str = it.next();
String strMap = map.get(str);
if(strMap==null){
map.put(str, str);
}
}
List<String> list2 = new ArrayList<String>(map.values());
String[] newData = list2.toArray(new String[] {});
return newData;
}