オブジェクトの並べ替えを実行

6097 ワード

方法①:
package collsort.comparable; 

/** 
* Created by IntelliJ IDEA. 
* User: leizhimin 
* Date: 2008-3-29 22:21:19 
* Company: LavaSoft([url]http://lavasoft.blog.51cto.com[/url]) 
*          
*/ 
public class Cat implements Comparable<Cat> { 
    private int age; 
    private String name; 

    public Cat(int age, String name) { 
        this.age = age; 
        this.name = name; 
    } 

    public int getAge() { 
        return age; 
    } 

    public void setAge(int age) { 
        this.age = age; 
    } 

    public String getName() { 
        return name; 
    } 

    public void setName(String name) { 
        this.name = name; 
    } 


    public String toString() { 
        return "Cat{" + 
                "age=" + age + 
                ", name='" + name + '\'' + 
                '}'; 
    } 

    public int compareTo(Cat o) { 
        return this.getAge() - o.getAge(); 
    } 
}
 方法②:
public static void hashMapSortTest() {
     Map<String, Integer> maps = new HashMap<String, Integer>();   
     maps.put("boy", 8);   
     maps.put("cat", 7);   
     maps.put("dog", 1);   
     maps.put("apple", 5);   
 
     Iterator i = maps.entrySet().iterator();   
     while (i.hasNext()) {   
         Map.Entry<String, Integer> entry1 = (Map.Entry<String, Integer>) i.next();   
     }   
     List<Map.Entry<String, Integer>> info = new ArrayList<Map.Entry<String, Integer>>(maps.entrySet());   
     Collections.sort(info, new Comparator<Map.Entry<String, Integer>>() {   
         public int compare(Map.Entry<String, Integer> obj1, Map.Entry<String, Integer> obj2) {   
             return obj1.getValue().compareTo(obj2.getValue()); 
         }   
     });        
}
 方法③:
import java.util.*;  
  
/*      
    List       Collections.sort()   
               Collections.reverseOrder()  ,             
*/  
class StrLenComp implements Comparator<String>  
{  
    public int compare(String o1,String o2)  
    {  
        return o1.length()==o2.length()? o1.compareTo(o2):o1.length()-o2.length();  
    }  
}  
class CollectionDemo  
{  
    public static void main(String[] args)   
    {  
        SortDemo();  
        ReverseDemo();  
    }  
    public static void ReverseDemo()  
    {  
        List<String> ls = new ArrayList<String>();  
        ls.add("yo");  
        ls.add("a");  
        ls.add("you");  
        ls.add("ada");  
        System.out.println(ls);  
        Collections.sort(ls,Collections.reverseOrder());//      
        System.out.println(ls);  
        Collections.sort(ls,Collections.reverseOrder(new StrLenComp()));    //            
        System.out.println(ls);  
    }  
    public static void SortDemo()  
    {  
        List<String> ls = new ArrayList<String>();  
        ls.add("you");  
        ls.add("a");  
        ls.add("you");  
        ls.add("ada");  
        System.out.println(ls);  
        Collections.sort(ls);       //          
        System.out.println(ls);  
        Collections.sort(ls,new StrLenComp());  //          
        System.out.println(ls);  
    }  
}  
 ケース:
 //            
    @RequestMapping(value="adminLogin",method = RequestMethod.POST)
    public String adminLogin(PrintWriter printWriter,Model model,HttpServletRequest request,String userName,String pwd)
    {
        //            
        WxUser user=userService.findUserByNameAndPwd(userName,pwd);
        request.getSession().setAttribute("sessionUser",user);
        if(null!=user)
          {

              //      ,      ajax      ,          
              Set<WxRole> wxRoleSet=user.getWxRoles();
              Set<WxGongnengquanxian> gongnengquanxians=new HashSet<WxGongnengquanxian>();
              for(WxRole wxRole:wxRoleSet){
                  gongnengquanxians=   wxRole.getWxGongnengquanxians();
              }
          
              List<WxGongnengquanxian> wxGongnengquanxianList = new ArrayList<WxGongnengquanxian>();
              wxGongnengquanxianList.addAll(gongnengquanxians);// set       list
              Collections.sort(wxGongnengquanxianList, new Comparator<WxGongnengquanxian>() {//    
                  @Override
                  public int compare(WxGongnengquanxian o1, WxGongnengquanxian o2) {
                      return o2.getWxGongnengquanxianName().compareTo(o1.getWxGongnengquanxianName());
                  }
              });
    
              model.addAttribute("gongnengquanxians",wxGongnengquanxianList);
              request.getSession().setAttribute("gnqx",gongnengquanxians);
              logService.LogStorage(request,SystemConstant.LOG_ACTION_LOGIN, SystemConstant.LOG_ACTION_SUCCESS,"    ");
              return "/background/bgIndex";

          }else {
              //         
              logService.LogStorage(request,SystemConstant.LOG_ACTION_LOGIN, SystemConstant.LOG_ACTION_FAIL,"    ");
          }
        return "/background/bglogin";

    }