Map兩種遍歷方式
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* Created by jiaobuchong on 1/24/16.
*/
public class CountChNums {
public static void main(String[] args) {
String[] a = {"a", "b", "a", "b", "c", "a", "b", "c", "b"};
Map<String, Integer> countNums = new HashMap<>();
for (int i = 0; i < a.length; i++) {
//key 為字符, value為字符出現的次數
if (countNums.containsKey(a[i])) {
int temp = countNums.get(a[i]);
countNums.replace(a[i], temp + 1);
} else {
countNums.put(a[i], 1);
}
}
/**
* Map兩種遍歷方式
* 方式1
*/
Set<Map.Entry<String, Integer>> entrySet = countNums.entrySet();
for (Map.Entry<String, Integer> entry : entrySet) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
System.out.println();
/**
* 方式2 現獲取所有的key的集合
*/
Set<String> set = countNums.keySet();
for (String key : set) {
System.out.println(key + ": " + countNums.get(key));
}
}
}
Map栗1
貼上另外一個例子,以作對比:
/*
* 輸入:Shall I Compare Thee To Summer's Day ddd
* 結果:'(1)C(1)D(1)I(1)S(2)T(2)a(3)d(3)e(4)h(2)l(2)m(3)o(2)p(1)r(2)s(1)u(1)y(1)
*/
public class TreeMapDemo {
public static void main(String[] args) {
// 定義一個字符串(可以改進為鍵盤錄入)
Scanner sc = new Scanner(System.in);
System.out.println("請輸入一個字符串:");
String line = sc.nextLine().replaceAll(" +", ""); //并去掉所有空格
// 定義一個TreeMap集合,可以按照字符的先后順序輸出
TreeMap<Character, Integer> tm = new TreeMap<Character, Integer>();
//把字符串轉換為字符數組
char[] chs = line.toCharArray();
//遍歷字符數組,得到每一個字符
for(char ch : chs){
//拿剛才得到的字符作為鍵到集合中去找值,看返回值
Integer i = tm.get(ch);
//是null:說明該鍵不存在,就把該字符作為鍵,1作為值存儲
if(i == null){
tm.put(ch, 1);
}else {
//不是null:說明該鍵存在,就把值加1,然后重寫存儲該鍵和值
i++;
tm.put(ch,i);
}
}
//定義字符串緩沖區變量
StringBuilder sb= new StringBuilder();
//遍歷集合,得到鍵和值,進行按照要求拼接
Set<Character> set = tm.keySet();
for(Character key : set){
Integer value = tm.get(key);
sb.append(key).append("(").append(value).append(")");
}
//把字符串緩沖區轉換為字符串輸出
String result = sb.toString();
System.out.println("result: "+result);
}
}
Map栗2
獲取List中重復元素的個數,按map的value進行排序
import java.util.*;
public class ListEleCount {
public static void main(String[] args) {
List<String> names = new ArrayList<>();
names.add("jack");
names.add("jack");
names.add("jack");
names.add("tom");
names.add("tom");
names.add("lily");
names.add("jiaobuchong");
names.add("ijiaobu");
names.add("ijiaobu");
names.add("ijiaobu");
names.add("ijiaobu");
names.add("ijiaobu");
/**
* 統計這個list中重復元素的個數 并按個數降序輸出
*/
Map<String, Integer> count = new HashMap<>();
for (String name : names) {
if (count.keySet().contains(name)) {
count.put(name, count.get(name) + 1);
} else {
count.put(name, 1);
}
}
// 排序
System.out.println(count);
List<Map.Entry<String, Integer>> dataList = new ArrayList<>(count.entrySet());
Collections.sort(dataList, new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
return o2.getValue() - o1.getValue();
}
});
System.out.println(dataList);
}
}
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。