User代码请参考:http://bbs.itheima.com/thread-169162-1-1.html直接上码:
- package io.shaoshuai.colletion;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map;
- /**
- * MapOperation.java
- * @author shaoshuai
- * created 2015年2月3日 下午2:22:26
- * @version 1.0
- */
- public class MapOperation {
- /**
- * main
- * @param args
- * void
- * @exception
- * @since 1.0.0
- */
- public static void main(String[] args) {
- Map<String, User> map = new HashMap<String ,User>();
- User user = new User.UserBuilder(1l, "lanmotbzy", "123456")
- .email("1025276114@qq.com").address("北京黑马").build();
- User user2 = new User.UserBuilder(2l, "shaoshuai", "123456")
- .email("shaoshuai@qq.com").address("中关村").build();
- map.put("1", user);
- map.put("2", user2);
-
- // 迭代map第一种方式:轮询所有的key
- System.out.println("迭代map第一种方式:轮询所有的key");
- for(Iterator<String> keys = map.keySet().iterator();keys.hasNext();) {
- user = map.get(keys.next());
- System.out.println("user.getId():" + user.getId());
- System.out.println("user.getUsername():" + user.getUsername());
- System.out.println("user.getPassword():" + user.getPassword());
- System.out.println("user.getEmail():" + user.getEmail());
- System.out.println("user.getAddress():" + user.getAddress());
- }
-
- // 迭代map第二种方式 轮询所有的Map.Entry<K,V>
- Map.Entry<String, User> entry;
- System.out.println("迭代map第二种方式 轮询所有的Map.Entry<K,V>");
- for(Iterator<Map.Entry<String, User>> entrys = map.entrySet().iterator();entrys.hasNext();) {
- entry = entrys.next();
- user = entry.getValue();
- System.out.println("user.getId():" + user.getId());
- System.out.println("user.getUsername():" + user.getUsername());
- System.out.println("user.getPassword():" + user.getPassword());
- System.out.println("user.getEmail():" + user.getEmail());
- System.out.println("user.getAddress():" + user.getAddress());
- }
-
- System.out.println("System.nanoTime():" + System.nanoTime());
-
- }
-
-
- }
复制代码 代码是不是很精简,很优雅
|
|