1. 程式人生 > >Java集合的互相巢狀以及TreeMap集合獲取字串中不同字元數量例項

Java集合的互相巢狀以及TreeMap集合獲取字串中不同字元數量例項

文章目錄

1、獲取輸入字串中每個字元的個數
package test12_TreeMap;

import java.util.Scanner;
import java.util.
Set; import java.util.TreeMap; /* * 功能:獲取輸入字串中每個字元的個數,並輸出 * 思路: 1、接收字串 * 2、轉化為字元陣列 * 3、使用TreeMap集合(鍵為字元,值為字元出現的次數) * 首先看集合中是否含有此字元 * 沒有則新增新的鍵值對(字元,1) * 有則更改值並存儲(字元,++次數) * 4、增強for遍歷TreeMap集合並按照指定格式進行儲存 * */ public class TreeMap_Demo { public static void main(String[]
args) { Scanner sc=new Scanner(System.in); System.out.println("請輸入字串"); String s=sc.nextLine(); sc.close(); char []c=s.toCharArray(); TreeMap<Character,Integer> tm=new TreeMap<Character,Integer>(); for(char ch: c) { Integer i=tm.get(ch); if(i==null) { tm.
put(ch, 1); }else { tm.put(ch, ++i); } } StringBuilder sb=new StringBuilder(); Set<Character> set=tm.keySet(); for(Character ch:set) { int i=tm.get(ch); //字串拼接 sb.append(ch).append("出現").append(i).append("次,"); } System.out.println(sb); } }

輸出結果:
在這裡插入圖片描述

2、ArrayList巢狀HashMap
package test13_Nest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

/*
 * ArrayList巢狀HashMap
 * 
 * 一共有兩組HashMap
 * 	小明-踢球
 * 	小王-游泳
 * 
 * 	小紅-唱歌
 * 	小麗-畫畫
 */
public class ArrayListNestHashMap {
	public static void main(String[] args) {
		ArrayList<HashMap<String,String>> al=new ArrayList<HashMap<String,String>>();
		
		HashMap<String,String> hm1=new HashMap<String,String>();
		HashMap<String,String> hm2=new HashMap<String,String>();
		
		hm1.put("小明", "踢球");
		hm1.put("小王", "游泳");
		
		hm2.put("小紅", "小紅");
		hm2.put("小麗", "畫畫");
		
		al.add(hm1);
		al.add(hm2);
		
		for(HashMap<String,String> hm:al) {
			Set <String> set=hm.keySet();
			for(String key:set) {
				String value=hm.get(key);
				System.out.println(key+"--"+value);
			}
		}
		
	}
}

輸出結果
在這裡插入圖片描述

3、HashMap巢狀ArrayList
package test13_Nest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

/*
 * 實現HashMap巢狀ArrayList
 * man
 * 		小明
 * 		小王
 * woman
 * 		小紅
 * 		小麗
 */
public class HashMapNestArrayList {
	public static void main(String[] args) {
		HashMap<String,ArrayList<String>> hm=new HashMap<String,ArrayList<String>>();
		
		ArrayList<String> al1=new ArrayList<String>();
		al1.add("小明");
		al1.add("小王");
		
		ArrayList<String> al2=new ArrayList<String>();
		al2.add("小紅");
		al2.add("小麗");
		
		hm.put("man", al1);
		hm.put("woman", al2);
		
		Set<String> set=hm.keySet();
		for(String key:set) {
			ArrayList<String> value=hm.get(key);
			System.out.println(key);
			for(String s:value) {
				System.out.println("\t"+s);
			}
			
		}
		
	}

}

輸出結果
在這裡插入圖片描述

4、HashMap巢狀HashMap
package test13_Nest;

import java.util.HashMap;
import java.util.Set;

/*
 * 實現HashMap巢狀HashMap
 * 		鍵	值		鍵		值
 * 		Man	男人Map	:	
 * 				1、	小明		20
 * 				2、	小王		22
 * 		Woman 女人Map:
 * 				1、	小紅		18
 * 				2、	小麗		16
 */
public class HashMapNestHashMap {
	public static void main(String[] args) {
		//建立HashMap物件
		HashMap<String,HashMap<String,Integer>> peopleMap=new HashMap<String,HashMap<String,Integer>>();
		
		HashMap<String,Integer> manMap=new HashMap<String,Integer>();
		
		HashMap<String,Integer> womanMap=new HashMap<String,Integer>();
		
		//新增元素
		manMap.put("小明", 20);
		manMap.put("小王", 22);
		
		peopleMap.put("man", manMap);
		
		womanMap.put("小紅", 18);
		womanMap.put("小麗", 16);
		
		peopleMap.put("woman", womanMap);
		
		//遍歷
		Set <String> peopleMapSet=peopleMap.keySet();
		for(String peopleMapKey:peopleMapSet) {
			
			HashMap<String,Integer> peopleMapValue=peopleMap.get(peopleMapKey);
			System.out.println(peopleMapKey);
			Set<String> peopleMapValueSet=peopleMapValue.keySet();
			for(String peopleMapValueKey:peopleMapValueSet) {
				Integer peopleMapValueValue=peopleMapValue.get(peopleMapValueKey);
				System.out.println("\t"+peopleMapValueKey+":"+peopleMapValueValue);
			}
		}
	}
}

輸出結果
在這裡插入圖片描述

5、HashMap巢狀HashMap再巢狀ArrayList

三層巢狀,ArrayList儲存的是學生物件
巢狀程式

package test13_Nest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

/*
 * 實現三層巢狀:HashMap巢狀HashMap再巢狀ArrayList
 * 這次的ArrayList儲存的是學生物件
 * 
 * 	通訊學院:
 * 			2班	
 * 				小紅18歲
 * 				小李18歲
 * 			12班
 * 				小王19歲
 * 				小麗18歲
 * 	電子學院:
 * 			1班
 * 				小芳18歲
 * 				小明20歲
 * 			11班
 * 				小趙20歲
 * 				小軍19歲
 */
public class ThreeNest {
	public static void main(String[] args) {
		HashMap<String,HashMap<String,ArrayList<Student>>> hm=
		new HashMap<String,HashMap<String,ArrayList<Student>>>();
		
		HashMap<String,ArrayList<Student>> hm1=new HashMap<String,ArrayList<Student>>();
		HashMap<String,ArrayList<Student>> hm2=new HashMap<String,ArrayList<Student>>();
		
		ArrayList<Student> a1=new ArrayList<Student>();
		ArrayList<Student> a2=new ArrayList<Student>();
		ArrayList<Student> a3=new ArrayList<Student>();
		ArrayList<Student> a4=new ArrayList<Student>();
		
		Student s1=new Student("小紅",18);
		Student s2=new Student("小李",18);
		Student s3=new Student("小王",19);
		Student s4=new Student("小麗",18);
		Student s5=new Student("小芳",18);
		Student s6=new Student("小明",20);
		Student s7=new Student("小趙",20);
		Student s8=new Student("小軍",19);
		
		a1.add(s1);
		a1.add(s2);
		
		a2.add(s3);
		a2.add(s4);
		
		a3.add(s5);
		a3.add(s6);
		
		a4.add(s7);
		a4.add(s8);

		hm1.put("2班", a1);
		hm1.put("12班", a2);
		
		hm2.put("1班", a3);
		hm2.put("11班", a4);
		
		hm.put("通訊學院", hm1);
		hm.put("電子學院", hm2);
		
		Set <String> hmSet=hm.keySet();
		for(String hmKey: hmSet) {
			HashMap<String,ArrayList<Student>> hmValue=hm.get(hmKey);
			System.out.println(hmKey);
			Set<String> hmValueSet=hmValue.keySet();
			for(String hmValueKey:hmValueSet) {
				ArrayList<Student> hmValueValue=hmValue.get(hmValueKey);
				System.out.println("\t"+hmValueKey);
				for(Student al:hmValueValue) {
					System.out.println("\t\t"+al.getName()+al.getAge()+"歲");
				}
			}
		}
		
	}	
}

學生類程式

package test13_Nest;

public class Student {
	 private String name;
	 private int age;
	 public Student() {
			super();
			// TODO Auto-generated constructor stub
	}
	public Student(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
	 
}

輸出結果
在這裡插入圖片描述