package com.yanek.test;
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;public class Test {
/**
* @param args */ public static void main(String[] args) {//去掉重复的数字,返回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);}
/** * 获取ID列表 * @param param * @param regex * * 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 * * 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; }}