Map 四种遍历:
Map<String,String> map = new HashMap<String, String>();
map.put("one","java");
map.put("two","cn");
map.put("three","love");
第一种:取值遍历
for(String key:map.keySet()){
System.out.println("key="+key+"and value=" +map.get(key));
}
第二种:Iterator遍历
Iterator<Map.Entry<String,String>> it = map.entrySet().iterator();
while(it.hasNext()){
Map.Entry<String,String> entry=it.next();
System.out.println("key=" +entry.getKey() +" and value="+entry.getValue());
}
第三种:遍历所有的Value值
for(String v:map.values()){
System.out.println("value= "+ v);
}
该方式取得不了key值,直接遍历map中存放的value值。
第四种:使用entrySet遍历
for(Map.Entry<String,String > entry:map.entrySet()){
System.out.println("key=" +entry.getKey() +" and value="+entry.getValue());
}
map排序:
按 key 排序:
public class MapSortDemo { public static void main(String[] args) { Map<String, String> map = new TreeMap<String, String>(); map.put("KFC", "kfc");
map.put("WNBA", "wnba");
map.put("NBA", "nba");
map.put("CBA", "cba"); Map<String, String> resultMap = sortMapByKey(map); //按Key进行排序 for (Map.Entry<String, String> entry : resultMap.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
} /**
* 使用 Map按key进行排序
* @param map
* @return
*/
public static Map<String, String> sortMapByKey(Map<String, String> map) {
if (map == null || map.isEmpty()) {
return null;
} Map<String, String> sortMap = new TreeMap<String, String>(
new MapKeyComparator()); sortMap.putAll(map); return sortMap;
}
} 比较器类 class MapKeyComparator implements Comparator<String>{ @Override
public int compare(String str1, String str2) { return str1.compareTo(str2);
}
}
按 value 排序:
//如果在Treemap里面想按照value进行排序,我们必须借助工具类Collections.sort(List,Comparator);
TreeMap<String,Object> map2 = new TreeMap<String,Object>();
map2.put("a","a");
map2.put("b","cccccc");
map2.put("c","bbbbb");
map2.put("d","eeee");
map2.put("e","dd");
ArrayList<Map.Entry<String,Object>> list = new ArrayList<Map.Entry<String,Object>>(map2.entrySet());
Collections.sort(list,new Comparator<Map.Entry<String,Object>>() { @Override
public int compare(Entry<String, Object> o1, Entry<String, Object> o2) {
//变成按照value排列
// return o2.getValue().toString().compareTo(o1.getValue().toString());
//按照value的长度排序
Integer o11 = o1.getValue().toString().length();
Integer o22 = o2.getValue().toString().length();
return o22.compareTo(o11);
} }); for(Map.Entry<String,Object> l :list){
System.out.println(l.getKey()+":"+l.getValue());
}
在 map 中根据 value 获取 key:
//根据map的value获取map的key
private static String getKey(Map<String,String> map,String value){
String key="";
for (Map.Entry<String, String> entry : map.entrySet()) {
if(value.equals(entry.getValue())){
key=entry.getKey();
}
}
return key;
}
若要取 map 中 value 的最大值 或 与之对应的 key(整型或浮点型):可利用list
1 //利用list取最大值
List<Double> listmap = new ArrayList<Double>();
for(String key:mapp.keySet()){
listmap.add(mapp.get(key));
}
//取到最大值的value
double valueMax = Collections.max(listmap);
//根据map的value获取map的key
String emotionMax = "";
for (Map.Entry<String, Double> entry : mapp.entrySet()) {
if(valueMax == entry.getValue()){
emotionMax = entry.getKey(); //取到最大值的 value 对应的 key
}
}