構建Hashmap的時候 發現不能用double, 能夠用String。java
後來才發現double是原始數據類型, Double是它的封裝類型,用在HashMap和 List中。ide
若是用double 沒法編譯經過,會提示 required reference, found doubleui
排序就用Collections 的sortspa
import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.HashSet; import java.util.Set; import java.util.List; import java.util.ArrayList; import java.util.Collection; import java.util.Map; import java.util.Map.Entry; class Test{ public static void run(int numA, int numB){ // HashMap<String, HashMap<String, String>> HM = new HashMap<String, HashMap<String, String>>(); HashMap<Double, HashMap<Double, String>> HM = new HashMap<Double, HashMap<Double, String>>(); for(int i=0; i<= numA; i++){ // HashMap<String, String> innerHm = new HashMap<String, String>(); HashMap<Double, String> innerHm = new HashMap<Double, String>(); for(int j=0; j<= numB; j++){ double b = j * 0.01; // String bs = String.valueOf(b); // innerHm.put(bs , "test"); innerHm.put(b , "test"); } double a = i * 0.01; // String as = String.valueOf(a); // HM.put(as, innerHm); HM.put(a, innerHm); } Collection<Double> keyA = HM.keySet(); //Collection<String> keyA = HM.keySet(); List<Double> list = new ArrayList<Double>(keyA); // List<String> list = new ArrayList<String>(keyA); Collections.sort(list); // for(Iterator<String> iH = list.iterator(); iH.hasNext();){ for(Iterator<Double> iH = list.iterator(); iH.hasNext();){ double out_num = iH.next(); // String So = String.valueOf(out_num); System.out.println(out_num); HashMap<Double, String> inMap = HM.get(out_num); Collection<Double> it = inMap.keySet(); // Iterator iter = inMap.entrySet().iterator(); List<Double> listIn = new ArrayList<Double>(it); Collections.sort(listIn); Iterator<Double> ns_it = listIn.iterator(); while(ns_it.hasNext()){ //Map.Entry<String, String> entry = (Map.Entry) ns_it.next(); double in_num = ns_it.next(); // String in_num = entry.getKey(); String inStr = inMap.get(in_num); System.out.println(in_num + "\t" + inStr); } } } } public class test_SortHashMap { public static void main(String[] args){ Test t = new Test(); t.run(20, 30); } }
這樣就能夠用浮點數 ,對它們進行排序輸出了。orm
Int是java的原始數據類型,Integer是java爲int提供的封裝類。xml
Java爲每一個原始類型提供了封裝類。排序
原始類型 封裝類get
boolean Booleanit
char Characterio
byte Byte
short Short
int Integer
long Long
float Float
double Double