关于利用HashSet,split,deleteCharAt等方法详解

1.首先了解一下HashSet的原理:

Set接口
  Set是对数学上集的抽象,Set中不包含重复的元素.如何界定是否是重复元素?Set最多可含一个null元素;对于任意的非null元素e1和e2,都满足e1.equals(e2)==false.
  Object.hashcode()的约定:
a.在程序的一次执行中,无论何时在同一个java对象上重复调用hashcode(),都必须一致地返回同一个整数值,并不像Object.equals()那样提供Object是否被修改了的信息,但这个整数值不必在同一个应用程序的多次运行之间保持一致.
b.如果两个Object通过equals()判断是相等的,那么,在这两个Object上调用hashcode()必返回相同的值.

c.如果两个Object通过equals()判断是不相等的,那么,并不要求这两个Object.hashcode()返回不同的整数值.


Set实现
  HashSet是使用一个哈希表存储元素的,是非排序的,可以随机访问,是Set的最优性能实现.TreeSet实现了SortedSet接口,使用一个红黑树来存储元素,提供了元素的有序存储和访问.
   HashSet(HashSet在底层借用HashMap)
  HashSet在底层实现上依赖于HashMap.
    HashSet的源码片断:

// Dummy value to associate with an Object in the backing Map
    private static final Object PRESENT = new Object();
 
    public HashSet() {
 map = new HashMap<E,Object>();
    }
 
    public boolean add(E o) {
 return map.put(o, PRESENT)==null;
    }
 
    public boolean remove(Object o) {
 return map.remove(o)==PRESENT;
    }
 
    public boolean contains(Object o) {
       return map.containsKey(o);
    }

从代码可见,HashSet在底层借用HashMap,使用一个Object类型的哑元值作为HashSet中元素在底层HashMap存储中的映射值.它抓住了HashMap的键不允许重复的特性.对于add()中调用底层映射的put(),将欲添加的元素和一个PRESENT哑元值放入底层map.如果底层Map返回null,说明原来的集合中并不存在该键.对于Map接口的put()返回null时有两种解释,一是原来的Map中不包含该键;另一种可能是原来的Map中已经存储了该健,但该键映射到null.而在HashSet中的add()和remove()等中的返回null只有一种解释,即原来的集合不包含该元素.这是因为HashSet的底层的映射中存储的都是一个名为PRESENT的Object类型的对象,不可能是null.

2.示例代码如下:

          UserLookupSearchCriteria criteria =(UserLookupSearchCriteria) theForm.getSearchCriteria2();
                String tempFilterUserCodes=criteria.getFilterUserCodes();//这里指从页面上获取的filterUserCodes
 
                String filterUserCodes=printDlg.getFileUserCodes();//调用一个接口,从数据库查询的filterUserCodes
 
                                                             //然后现在有两个filterUserCodes,他们的值是不同的,所以需要遍历来获取他们一个不重复的set集合,然后传入后台进行过滤。
 
                Set<String> userCodesSet=new HashSet<String>();//新建一个Hashset集合
            
                if(!StringUtil.isEmpty(tempFilterUserCodes)){
                    String[] tempUserCodesArray=tempFilterUserCodes.split(",");
                    for(String userCode:tempUserCodesArray){
                        userCodesSet.add(userCode);
                    }
                }                                                      //判断如果tempFilterUserCodes不为空,就使用split方法转为数组进行遍历,然后add到set集合中
                if(!StringUtil.isEmpty(filterUserCodes)){
                    String[] userCodesArray=filterUserCodes.split(",");
                    for(String userCode:userCodesArray){
                        userCodesSet.add(userCode);
                    }
                 }                                                   //判断如果filterUserCodes 不为空,就使用split方法转为数组进行遍历,然后add到set集合中,但是在这里如果两个String的值都不为空,根据set集合的特性,会过滤掉重复的值,所以得到的set集合是一个没有重复值的集合。
                StringBuffer userCodeStr=null;
                if(userCodesSet!=null&&userCodesSet.size()>0){
                    userCodeStr=new StringBuffer();
                    for(String userCode:userCodesSet){
                        userCodeStr.append(userCode).append(",");
                    }//然后去遍历userCodesSet集合,转为一个String,
                    userCodeStr.deleteCharAt(userCodeStr.length()-1);
 
                                        //使用String的方法deleteCharAt 过滤掉最后一个逗号
 
                }
                criteria.setFilterUserCodes(userCodeStr==null?"":userCodeStr.toString());//拿到新的String传入后台
                UserLookupSearchCriteria userLookupSearchCriteria = printDlg.getUserLookupList(criteria);
                theForm.setUsers(userLookupSearchCriteria.getUsers());
                theForm.setSearchCriteria2(userLookupSearchCriteria);
                
                forwardName = "userlookupList";

note:这里为什么要使用这样的过滤呢?是因为一个是从页面上传入过来的userCode,还有一个是从后台读取的userCode,而这两个String都是需要过滤掉的,所以需要使用整合过滤。

posted @ 2018-10-18 10:51  heart~  阅读(527)  评论(0)    收藏  举报