package com.yanek.test; java
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set; .net
public class Test { get
/**
* @param args
*/
public static void main(String[] args) { it
//去掉重複的數字,返回list
String ids="5;4;11;5;2;3;4;1;5;3";
List idlist=getIdList1(ids,";");
Collections.sort(idlist);
System.out.println("idlist1:"+idlist);
List idlist2=getIdList2(ids,";");
Collections.sort(idlist2);
System.out.println("idlist2:"+idlist2); io
}
/**
* 獲取ID列表
* @param param
* @param regex
* @return
* @throws Exception
*/
public static List getIdList1(String param, String regex) {
if (param == null || regex == null)
return null;
Set idSet = null;
String[] ids = param.split(regex);
if (ids != null) {
for (int i = 0; i < ids.length; i++) {
String id = ids[i];
if (id != null && id.trim().length() > 0) {
if (idSet == null) {
idSet = new HashSet();
}
idSet.add(new Integer(id.trim()));
}
}
}
return idSet == null ? null : Collections.list(Collections.enumeration(idSet));
}
/**
* 獲取ID列表
* @param param
* @param regex
* @return
* @throws Exception
*/
public static List getIdList2(String param, String regex) {
if (param == null || regex == null)
return null;
List list=new ArrayList();
Map idmap=new HashMap();
String[] ids = param.split(regex);
if (ids != null) {
for (int i = 0; i < ids.length; i++) {
String id = ids[i];
if (id != null && id.trim().length() > 0) {
if (!idmap.containsKey(id))
{
list.add(new Integer(id.trim()));
idmap.put(id,new Integer(id.trim()));
}
else
{
continue;
}
}
}
}
return list;
} class
} test