集合

1.主要掌握5个接口:

Collection List Set Map Iterator Enumeration

2.不允许重复的子接口:Set

3.Set中常用的两个子类:散列存放的子类:HashSet

  顺序存放的子类TreeSet

4. Map接口

public Set<K> keySet() ,将Map中的所有key以Set集合的方式返回

public Set<Map.Entry><K,V>>entrySet(),将Map集合变为Set集合

Map的输出

package com.ylfeiu.utils;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class HashMapEcho {
	public static void main(String[] args) {
		Map<Integer, String> map = new HashMap<>();
		map.put(1, "hello");
		map.put(2, "txt");
		map.put(2, "txt2");
		map.put(null, "ylfeiu");
		map.put(1, "hello");

		Set<Integer> set = map.keySet();
		Iterator<Integer> car = set.iterator();

		while (car.hasNext()) {
			Integer key = car.next();
			System.err.println(key + "------------>" + map.get(key));
		}

	}
}

  

例子2

package com.ylfeiu.utils;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class HashMapEcho2 {
	public static void main(String[] args) {
		Map<Integer, String> map = new Hashtable<>();
		map.put(1, "hello");
		map.put(2, "txt");
		map.put(2, "txt2");
		// map.put(null, "ylfeiu");//HashTable不能使用null做key
		map.put(1, "hello");

		Set<Map.Entry<Integer, String>> set = map.entrySet();//把Map.Entry当作一个对象
		Iterator<Map.Entry<Integer, String>> car = set.iterator();

		while (car.hasNext()) {
			Map.Entry<Integer, String> me = car.next();
			System.err.println(me.getKey() + "------------>" + me.getValue());

		}

	}
}

  

  

原文地址:https://www.cnblogs.com/ylfeiu/p/3599344.html