Map m = new TreeMap(); m.put("o1", 1); m.put("o2", 2); m.put("o3", 3); m.put("o4", 4); m.put("o5", 5); Set keySet = m.keySet(); for (Iterator it = keySet.iterator(); it.hasNext();) { Object one = m.get(it.next()); Object two; if (it.hasNext()) { two = m.get(it.next()); } }
Map<Integer, Integer> map = new HashMap<Integer, Integer>(); map.put(1, 1); map.put(2, 2); map.put(3, 3); map.put(4, 4); map.put(5, 5); for (Integer i : map.keySet()) { System.out.print(map.get(i)); if(i+1<=map.size()){ System.out.print(","); System.out.println(map.get(i+1)); } }
m.put("o1", 1);
m.put("o2", 2);
m.put("o3", 3);
m.put("o4", 4);
m.put("o5", 5);
Set keySet = m.keySet();
for (Iterator it = keySet.iterator(); it.hasNext();) {
Object one = m.get(it.next());
Object two;
if (it.hasNext()) {
two = m.get(it.next());
}
}
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
map.put(1, 1);
map.put(2, 2);
map.put(3, 3);
map.put(4, 4);
map.put(5, 5);
for (Integer i : map.keySet()) {
System.out.print(map.get(i));
if(i+1<=map.size()){
System.out.print(",");
System.out.println(map.get(i+1));
}
}
map.put(1, 1);
map.put(2, 2);
map.put(3, 3);
map.put(4, 4);
map.put(5, 5);
for (Integer i : map.keySet()) {
System.out.printf("%d, %d\n", map.get(i), map.get(i==5 ? 1 : i+1));
}
由于map是以键值形式存储的,所以如果键是字符串的话要满足你的要求也必须是字符形式的数字
稍加修改就行了:
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("1", 1);
map.put("2", 2);
map.put("3", 3);
map.put("4", 4);
map.put("5", 5);
for (String i : map.keySet()) {
System.out.print(map.get(i));
if(Integer.parseInt(i)+1<=map.size()){
System.out.print(",");
System.out.println(map.get(Integer.parseInt(i)+1+""));
}else{
System.out.print(",");
System.out.println(map.get("1"));
}
}