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);
	}
}

输出结果:
Java集合的互相嵌套以及TreeMap集合获取字符串中不同字符数量实例

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);
			}
		}
		
	}
}

输出结果
Java集合的互相嵌套以及TreeMap集合获取字符串中不同字符数量实例

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);
			}
			
		}
		
	}

}

输出结果
Java集合的互相嵌套以及TreeMap集合获取字符串中不同字符数量实例

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);
			}
		}
	}
}

输出结果
Java集合的互相嵌套以及TreeMap集合获取字符串中不同字符数量实例

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;
	}
	
	 
}

输出结果
Java集合的互相嵌套以及TreeMap集合获取字符串中不同字符数量实例