Skip to content

Instantly share code, notes, and snippets.

@Tikitoo
Created January 6, 2015 09:18
Show Gist options
  • Save Tikitoo/ffef10e2d37503e940a2 to your computer and use it in GitHub Desktop.
Save Tikitoo/ffef10e2d37503e940a2 to your computer and use it in GitHub Desktop.
package com.tikitoo.demo.app2;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* 参考:
* http://javahowto.blogspot.com/2006/06/4-ways-to-traverse-map.html
* Created by tikitoo on 1/6/15.
*/
public class ListRemoveDuplicates {
private static final Integer HTTP_NOT_FOUND = 404;
private static final Integer HTTP_OK= 200;
private static final Integer HTTP_SERVER_ERROR = 505;
public static void main(String[] args) {
Map<Integer, String> data = new HashMap<Integer, String>();
data.put(HTTP_NOT_FOUND, "HTTP_NOT_FOUND");
data.put(HTTP_OK, "HTTP_OK");
data.put(HTTP_SERVER_ERROR, "SERVER_ERROR");
// 第一种
Set<Map.Entry<Integer, String>> entrySet = data.entrySet();
for (Map.Entry<Integer, String> entry : entrySet) {
Integer key = entry.getKey();
String value = entry.getValue();
System.out.println(key + ": " + value);
}
// 第二种
for (Integer key : data.keySet()) {
String value = data.get(key);
System.out.println(key + ": " + value);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment