java TreeMap() 排序代碼

來源:互聯網
上載者:User

treemap中所有的元素都保持著某種固定的順序,如果你需要得到一個有序的結果你就應該使用treemap(hashmap中元素的排列順序是不固定的)。

<%@ page import="java.util.*" %>
<%
   enumeration enames = request.getparameternames();
   if (enames.hasmoreelements()) {
      string title = "parameters";
      map entries = new treemap();
      while (enames.hasmoreelements()) {
         string name = (string) enames.nextelement();
         string[] values = request.getparametervalues(name);
         if (values.length > 0) {
            string value = values[0];
            for (int i = 1; i < values.length; i++) {
               value += "," + values[i];
            }
            entries.put(name, value);
         }
      }
      request.setattribute("_table_title", title);
      request.setattribute("_table_entries", entries);
   }
%>

看一個學產生績排序方法

package ch12;
import java.util.*;
public class num2{
 public static void main(string[] args) {
 // 定義出treemap,其中key為integer類型的成績值,value為姓名和成績組 // 成的字串。
  //使用map聲明對象,是泛型的使用。
  map<integer, string> treemap = new treemap<integer, string>();
  treemap.put(56,"張三56");
  treemap.put(88,"小白88");
  treemap.put(75,"小黑75");
  treemap.put(66,"小蘭66");
  treemap.put(98,"小鋒98");
  set<integer> keyset = treemap.keyset()); //得到key set
  system.out.print("全部的成績為 :" + keyset);
  system.out.print("前三名為 :");

  int i = 0;
 // jdk 的新for each效率更高。
 for (integer key : keyset ) {
    if (i++ == 3) {
       // i增長為3說明到了第4個,退出
       break;
    }
  system.out.print(treemap.get(key)+" ");
 
   }
}
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.