ArrayList嵌套HashMap /*ArrayList<HashMap<String,String>> array=new ArrayList<HashMap<String,String>>(); HashMap<String,String> hm1=new HashMap<String, String>(); hm1.put("sunce","daqiao"); hm1.put("zhouyu","xiaoqiao"); array.add(hm1); HashMap<String,String> hm2=new HashMap<String, String>(); hm2.put("guojing","huangrong"); hm2.put("yangguo","xiaolongnv"); array.add(hm2); for(HashMap<String,String> hm:array) { Set<String> keySet = hm.keySet(); for(String key:keySet) { String value=hm.get(key); System.out.println(key+","+value); } }*/ HashMap嵌套ArrayList /*HashMap<String,ArrayList<String>> hm=new HashMap<String,ArrayList<String>>(); ArrayList<String> sgyy=new ArrayList<String>(); sgyy.add("zhugeliang"); sgyy.add("zhaoyun"); hm.put("sanguoyanyi",sgyy); ArrayList<String> xyj=new ArrayList<String>(); xyj.add("tansgeng"); xyj.add("sunkukong"); hm.put("xiyouji",xyj); Set<String> keySet = hm.keySet(); for(String key:keySet) { System.out.println(key); ArrayList<String> value = hm.get(key); for(String s:value) { System.out.println("\t"+s); } }*/
统计字符串中每个字符出现的次数
Scanner sc=new Scanner(System.in);
System.out.println("shuruyigezifuchuan");
String line=sc.nextLine();
HashMap<Character,Integer> hm=new HashMap<Character,Integer>();
for(int i=0;i<line.length();i++)
{
char key=line.charAt(i);
Integer value = hm.get(key);
if(value==null)
{
hm.put(key,1);
}
else
{
value++;
hm.put(key,value);
}
}
StringBuilder sb=new StringBuilder();
Set<Character> keySet = hm.keySet();
for(Character key:keySet)
{
Integer value=hm.get(key);
sb.append(key).append("(").append(value).append(")");
}
String result = sb.toString();
System.out.println(result);