欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

集合嵌套练习题

程序员文章站 2022-07-12 09:36:06
...

List嵌套List

public static void main(String[] args) {
		//建立集合,存储字符串
		List<String> smallList = new ArrayList<>();
		smallList.add("how");
		smallList.add("are");
		smallList.add("you");
		smallList.add("yyx");
		
		List<String> smallList2 = new ArrayList<>();
		smallList.add("how");
		smallList.add("do");
		smallList.add("you");
		smallList.add("do");
		
		//建立集合,存储小集合
		List<List<String>> bigList = new ArrayList<>();
		//小集合,存储大集合
		bigList.add(smallList);
		bigList.add(smallList2);
		
		iterator(bigList);
	}

	/*
	 * 方法,遍历集合,遍历是大集合
	 */
	public static void iterator(List<List<String>> bigList) {
		//迭代器,遍历大集合
		Iterator<List<String>> bigit = bigList.iterator();
		while(bigit.hasNext()) {
			List<String> smallList = bigit.next();
			
			Iterator<String> smallIt = smallList.iterator();
			while(smallIt.hasNext()) {
				String string = smallIt.next();
				System.out.println(string);
			}
		}
	}

List嵌套Map

public static void main(String[] args) {
		//创建Map集合,键值字符串
		Map<String, String> bigMap1 = new HashMap<String, String>();
		bigMap1.put("a", "1");
		bigMap1.put("b", "2");
		bigMap1.put("c", "3");
		
		Map<String, String> bigMap2 = new HashMap<String, String>();
		bigMap2.put("aa", "11");
		bigMap2.put("bb", "22");
		bigMap2.put("cc", "33");
		
		List<Map<String, String>> bigList = new ArrayList<Map<String,String>>();
		bigList.add(bigMap1);
		bigList.add(bigMap2);
		iterator(bigList);
	}
	
	public static void iterator(List<Map<String, String>> bigList) {
		Iterator<Map<String, String>> bigIt = bigList.iterator();
		while(bigIt.hasNext()) {
			
			Map<String, String> map = bigIt.next();
			//keySet遍历map
			Set<String> set = map.keySet();
			Iterator<String> setIt = set.iterator();
			while(setIt.hasNext()) {
				String key = setIt.next();
				String value = map.get(key);
				System.out.println(key+"=="+value);
				}
			
		}
	}

Map嵌套Map

public static void main(String[] args) {
		//阶段集合,包含学号,姓名
		Map<String, String> firstMap = new HashMap<String, String>();
		//第一阶段,集合,存储字符串
		firstMap.put("001", "张三");
		firstMap.put("002", "李四");
		
		Map<String, String> secondMap = new HashMap<String, String>();
		//第二阶段集合,存储字符串
		secondMap.put("003", "王五");
		secondMap.put("004", "赵六");
		
		//创建学科集合, 键是阶段名,值是阶段 集合
		Map<String, Map<String, String>> javaMap = new HashMap<String, Map<String,String>>();
		javaMap.put("第一", firstMap);
		javaMap.put("第二", secondMap);
		//keySet(javaMap);
		entrySet(javaMap);
		
	}
	/*
	 * 方法遍历javaMap集合,entrySet()方式
	 */
	public static void entrySet(Map<String, Map<String, String>> javaMap) {
		//学科集合javaMap,方法entrySet(),获取学科集合的键值对,映射关系对象Entry
		Set<Map.Entry<String, Map<String, String>>> javaMapEntrySet = javaMap.entrySet();
		//迭代器,遍历Set集合
		Iterator<Map.Entry<String, Map<String, String>>> javaMapEntryIt = javaMapEntrySet.iterator();
		while(javaMapEntryIt.hasNext()) {
			//取出Set集合元素,元素是javaMap学科集合的键值对关系对象
			Map.Entry<String, Map<String, String>> javaMapEntry = javaMapEntryIt.next();
			//获取javaMap阶段集合的键,是阶段名
			String javaMapKey = javaMapEntry.getKey();
			//获取javaMap集合值,是阶段的集合 (姓名,学号)
			Map<String, String> jieDuanMap = javaMapEntry.getValue();
			//遍历阶段jieDuanMap,方法entrySet()取出集合的键值对映射关系对象
			Set<Map.Entry<String, String>> jieDaunEntrySet = jieDuanMap.entrySet();
			//迭代器,遍历Set集合
			Iterator<Map.Entry<String, String>> jieDuanEntryIt = jieDaunEntrySet.iterator();
			while(jieDuanEntryIt.hasNext()) {
				//取出Set集合的键,是阶段集合的键值对映射关系对象
				Map.Entry<String, String> jieDuanMapEntry = jieDuanEntryIt.next();
				//阶段集合的键,学号
				String key = jieDuanMapEntry.getKey();
				//阶段集合的值,姓名
				String value = jieDuanMapEntry.getValue();
				System.out.println(javaMapKey+"=="+key+"=="+value);
			}
		}
	}
	
	
	/*
	 * 方法遍历javaMap集合,keySet()方式
	 */
	public static void keySet(Map<String, Map<String, String>> javaMap) {
		//学科Map集合,遍历,方法keySet(),存储键的Set集合
		Set<String> javaSet = javaMap.keySet();
		//迭代器遍历Set集合
		Iterator<String> javaIt = javaSet.iterator();
		while(javaIt.hasNext()) {
			//取出Set集合元素,是javaMap集合的键,阶段名字
			String javaMapKey = javaIt.next();
			//取出javaMap学科集合的值,值是个阶段的Map集合
			Map<String, String> jieDuanMap =  javaMap.get(javaMapKey);
			//遍历阶段的Map集合, 调用方法keySet()拿出阶段Map集合的键
			Set<String> jieDuanSet =  jieDuanMap.keySet();
			//迭代器遍历Set集合
			Iterator<String> jieDuanIt = jieDuanSet.iterator();
			while(jieDuanIt.hasNext()) {
				//取出Set集合的元素,是阶段Map集合的键,学号
				String key = jieDuanIt.next();
				//取出阶段集合的值,姓名
				String value = jieDuanMap.get(key);
				System.out.println(javaMapKey+"=="+key+"=="+value);
            }
		}
	}

### 练习三 : 斗地主排序

原理 : 需要自定义顺序

顺序 : 333344445555…AAAA2222王王

自定义顺序的思想 : 编号思想 0123…53 (数字的自然顺序)

每一个牌(字符串)和编号进行对应, 键值对思想实现. 编号为键,牌为值
集合嵌套练习题

public class Poker {
	public static void main(String[] args) {
		Map<Integer, String> pokerMap = new HashMap<Integer, String>();
		List<Integer> pokerList = new ArrayList<Integer>();
		int index = 0;
		String[] colors = {"♥","♠","♦","♣"};
		String[] numbers = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
		for(String number : numbers) {
			for(String color : colors) {
				pokerMap.put(index, color+number);
				pokerList.add(index);
				index++;
			}
		}
		pokerMap.put(52, "大王");
		pokerMap.put(53, "小王");
		pokerList.add(52);
		pokerList.add(53);
	
		
		Collections.shuffle(pokerList);
		
		List<Integer> player1 = new ArrayList<Integer>();
		List<Integer> player2 = new ArrayList<Integer>();
		List<Integer> player3 = new ArrayList<Integer>();
		List<Integer> bottom = new ArrayList<Integer>();
		
		for(int i = 0 ; i < pokerList.size() ; i++) {
			if(i < 3) {
				bottom.add( pokerList.get(i) );
			}
			else if( i % 3 == 0) {
				player1.add( pokerList.get(i));
			}
			else if (i % 3 == 1) {
				player2.add( pokerList.get(i));
			}
			else if (i % 3 == 2) {
				player3.add( pokerList.get(i));
			}
		}
		Collections.sort(player1);
		Collections.sort(player2);
		Collections.sort(player3);
		Collections.sort(bottom);
		
		look("aaa",pokerMap,player1);
		look("bbb",pokerMap,player2);
		look("ccc",pokerMap,player3);
		look("ddd",pokerMap,bottom);
	}

	public static void look(String name,Map<Integer,String> map, List<Integer> list) {
		System.out.print(name+":");
		for(Integer key : list) {
			String value = map.get(key);
			System.out.print(value+" ");
		}
		System.out.println();
	}
}