Java-based sorting of Map sets

Source: Internet
Author: User

Java-based sorting of Map sets

Step 1: Add a temporary sort key to Map

 

// Initialize the Map set List
 
  
> Columns = new ArrayList
  
   
> (); Map
   
    
C1 = new HashMap
    
     
(); C1.put (sort, 8); c1.put (title, www.chuweibo iao.com); columns. add (c1); Map
     
      
C2 = new HashMap
      
        (); C2.put (sort, 5); c2.put (title, www.chuweibo iao.com); columns. add (c2); Map
       
         C3 = new HashMap
        
          (); C3.put (sort, 13); c3.put (title, www.chuweibo iao.com); columns. add (c3 );
        
       
      
     
    
   
  
 

Step 2: Sort

 

 

/*** Sort Map-type element sets * @ param columns * Map-type Element Set */private void listSortingForMapTypeElement (List
 
  
> Columns) {Collections. sort (columns, new Comparator
  
   
> () {Public int compare (Map
   
    
Last, Map
    
     
Next) {Object lastSort = last. get (sort); Object nextSort = next. get (sort); if (lastSort = null | nextSort = null) {return 0;} else {return Integer. parseInt (String. valueOf (lastSort)> Integer. parseInt (String. valueOf (nextSort ))? 1: 0 ;}}});}
    
   
  
 

 

Step 3: remove the temporary key

 

 

/*** Remove the temporary sorting key * @ param columns * Map type Element Set */private void removeSortKey (List
 
  
> Columns) {for (Map
  
   
Column: columns) {column. remove (sort );}}
  
 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.