集合----map
package map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/***
* 一、Map
* 1、public interface Map<k,value>。
* 2、
*
*/
public class MapDemo {
public static void main(String[] args) {
Map hashmap=new HashMap();
hashmap.put(2, "abc");
hashmap.put(3, 33);
hashmap.put("p1", new Person(23,"李平"));
Set set=hashmap.entrySet();
/***
* 查,方法一:增强for循环。
*/
Object[] objs=set.toArray();
for(Object obj:objs){
System.out.println(obj);
}
/***
* 查,方法二:entrySet
* 1、先变成entrySet
* 2、从获得entrySet实例中获得迭代器
* 3、在while中用迭代器实例获得entry集合
* 4、再从entry集合中获得key,value
*/
System.out.println("---");
Set sets=hashmap.entrySet();
Iterator it=sets.iterator();
while(it.hasNext()){
Entry en= (Entry) it.next();
Object key=en.getKey();
Object value=en.getValue();
System.out.println(key+","+value);
}
}
}