1. 把 set转化成 有序的 List集合
public List<Tbpremission> findPremissionByUser(Integer userId) {
//根据主键找到用户Tbuser u = userDao.findById(Tbuser.class, userId);
//根据用户找到角色
Set<Tbrole> roles = u.getTbroles();
Set<Tbpremission> setp = new HashSet<Tbpremission>();
//根据角色获取权限
for(Tbrole r :roles){
setp.addAll(r.getTbpremissions());
}
//转List
List<Tbpremission> listp = new ArrayList<>(setp);
//排序
Collections.sort(listp,new Comparator<Tbpremission>() {
public int compare(Tbpremission o1, Tbpremission o2) {
if(o1.getPreId()>o2.getPreId())
return 1;
if(o1.getPreId()<o2.getPreId())
return -1;
return 0;
}
});return listp;
}
2. 把 LIst 中的 重复项去掉:
public List<Papers> getTestPaper() {
List<Papers> p=null;
String sql="select pap_id from papers_student_map where pap_id not in(select pap_id from papers_exercise_map) ";
List<String> ps=exerciseDao.findBySql(sql);
Set<String> pss=new HashSet<String>();
pss.addAll(ps); 直接把 List 转化为 Set ,去掉重复项
if(ps!=null&&ps.size()>0){
p=new ArrayList<Papers>();
List<Papers> p=null;
String sql="select pap_id from papers_student_map where pap_id not in(select pap_id from papers_exercise_map) ";
List<String> ps=exerciseDao.findBySql(sql);
Set<String> pss=new HashSet<String>();
pss.addAll(ps); 直接把 List 转化为 Set ,去掉重复项
if(ps!=null&&ps.size()>0){
p=new ArrayList<Papers>();
for(String psm : ps){
p.add(postTextbooksMapService.getPaperBypapId(psm));
}
}
//去重
Set<Papers> sp=new HashSet<Papers>();
sp.addAll(p);
p.clear();
p.addAll(sp);
for ( int i = 0 ; i < p.size() - 1 ; i ++ ) {
for ( int j = p.size() - 1 ; j > i; j -- ) {
if (p.get(j).equals(p.get(i))) {
p.remove(j);
}
}
}
return p;
}