集合的几种遍历方式

package com.great.start;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class Run {

	public static void main(String[] args) {
		System.out.println("第一题:");
		ArrayList<String> arraylist = new ArrayList<>();
		arraylist.add("一号");
		arraylist.add("二号");
		arraylist.add("三号");
		System.out.println("ArrayList:第一种");
		for(int i=0;i<arraylist.size();i++) {
			System.out.println(arraylist.get(i));
		}
		System.out.println("---------------");
		System.out.println("ArrayList:第二种");
		for(String str:arraylist) {
			System.out.println(str);
		}
		System.out.println("---------------");
		System.out.println("ArrayList:第三种");
		Iterator<String> it = arraylist.iterator();
		while(it.hasNext()) {
			String str = it.next();
			System.out.println(str);
		}
		HashMap<String, String> map = new HashMap<>();
		map.put("1", "一号");
		map.put("2", "二号");
		map.put("3", "三号");
		map.put("4", "四号");
		map.put("5", "五号");
		System.out.println("---------------------");
		System.out.println("第二题:");
		System.out.println("HashMap:第一种");
		Collection<String> c  = map.values();
		Iterator<String> it1 = c.iterator();
		while(it1.hasNext()) {
			String str = it1.next();
			System.out.println(str);
		}
		System.out.println("---------------------");
		System.out.println("HashMap:第二种");
		Set<String> set = map.keySet();
		for(String str:set) {
			System.out.println(str+"---"+map.get(str));
		}
		System.out.println("---------------------");
		System.out.println("HashMap:第三种");
		Set<Map.Entry<String, String>> set1 = map.entrySet();
		for(Map.Entry<String, String> map1:set1) {
			System.out.println(map1.getKey()+"---"+map1.getValue());
		}
		HashMap<String,String[]> map1 = new HashMap<>();
		ArrayList<String[]> list = new ArrayList<>();
		String[] string = {"一号","男","17"};
		list.add(string);
		String[] string1 = {"二号","女","18"};
		list.add(string1);
		String[] string2 = {"三号","男","19"};
		list.add(string2);
		String[] string3 = {"四号","女","20"};
		list.add(string3);
		String[] string4 = {"五号","男","21"};
		list.add(string4);
		
		map1.put("10000", list.get(0));
		map1.put("10001", list.get(1));
		map1.put("10002", list.get(2));
		map1.put("10003", list.get(3));
		map1.put("10004", list.get(4));
		System.out.println("---------------------");
		System.out.println("第三题:");
		System.out.println("HashMap:第一种");
		Collection<String[]> c1 = map1.values();
		for(String[] str:c1) {
			for(int i=0;i<str.length;i++) {
				System.out.print(str[i]+"-");
			}
			System.out.println();
		}
		System.out.println("HashMap:第二种");
		Set<String> set2 = map1.keySet();
		int index = 0;
		for(String str:set2) {
			String[] s = list.get(index);
			System.out.print(str+"-");
			for(int i=0;i<s.length;i++) {
				System.out.print(s[i]+"-");
			}
			System.out.println();
			index++;
		}
		System.out.println("HashMap:第三种");
		Set<Map.Entry<String, String[]>> set3 = map1.entrySet();
		for(Map.Entry<String, String[]> map2:set3) {
			String [] str = map2.getValue();
			System.out.print(map2.getKey()+"-");
			for(int i=0;i<str.length;i++) {
				System.out.print(str[i]);
			}
			System.out.println();
		}
		System.out.println("---------------");
		System.out.println("第五题");
		HashMap<String, String> map3 = new HashMap<>();
		map3.put("10000", "JF1711");
		map3.put("10001", "JF1711");
		map3.put("10002", "JF1711");
		map3.put("10003", "JF1712");
		map3.put("10004", "JF1712");
		Set<Map.Entry<String, String>> set4 = map3.entrySet();

		for(Map.Entry<String, String> map4:set4) {
			for(Map.Entry<String, String[]> map2:set3) {
				if(map4.getKey().equals(map2.getKey())) {
					String [] str = map2.getValue();
					System.out.print(map2.getKey()+"-");
					for(int i=0;i<str.length;i++) {
						System.out.print(str[i]+"-");
					}
					System.out.println(map4.getValue());
					System.out.println();
				}
			}
		}


	}
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值