首页 > 学院 > 开发设计 > 正文

Map

2019-11-08 01:05:34
字体:
来源:转载
供稿:网友

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


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表