Java入門–TreeMapとIteratorを利用するサンプルコード
Javaコード:
package com.startnews24.studyit;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
public class StudyTreemap {
public static void main(String[] args) {
Map<String,String>map =new TreeMap<String,String>(new Comparator<Object>(){
public int compare(Object obj1,Object obj2){
String key1= obj1.toString();
String key2= obj2.toString();
return key1.compareTo(key2);
}
});
map.put(“1", “treemap_AA");
map.put(“2", “treemap_BB");
map.put(“3", “treemap_CC");
for(Iterator iterator = map.values().iterator();iterator.hasNext();){
String name=(String)iterator.next();
System.out.println(“result:"+name);
}
}
}