import java.util.Iterator;
import java.util.TreeMap;public class ContainerTest1 {

TreeMap<Integer,Integer>tmp = new TreeMap<Integer,Integer>();
Iterator<Integer> itr;
public static void main(String[] args) {
new ContainerTest1(); }
public ContainerTest1(){
tmp.put(2, 3);
tmp.put(4, 3);
tmp.put(1, 3);
tmp.put(7, 3);
tmp.put(2, 6);
tmp.put(2, 4);
tmp.put(3, 1);
itr = tmp.keySet().iterator();
while(itr.hasNext()){
int key = itr.next();
int value = tmp.get(key);
System.out.println("key"+key+"value"+value);
}
}

}
为什么我得到的结果是key1value3
key2value4
key3value1
key4value3
key7value3而我想得到所有的键值

解决方案 »

  1.   

    首先,如果加入的键重复,会覆盖掉以前的键值对,其次TreeMap是经过排序的,排序准则基于键。
      

  2.   

    都很有道理,我也知道原因了
                       Iterator<Integer>itr ;
                       IdentityHashMap<Integer,Integer> tmp2 = new IdentityHashMap<Integer,Integer>();
                       int []temp = new int[10];
                      {......tmp2.put();}
                       itr = tmp2.keySet().iterator();
    while(itr.hasNext()){
    Object sos = itr.next();
    value = tmp2.get(sos);
    System.out.println("sos"+sos+"value"+value);
    sos1 = sos.hashCode();
    arr.add(sos1);
    }
    for(int i=0;i<arr.size();i++){
    temp[i] = arr.get(i);

    }
    int temp2;
    for(int i=0;i<temp.length;i++){
    for(int j=0;j<temp.length-i-1;j++){
    if(temp[j]>temp[j+1]){
    temp2 = temp[j+1];
    temp[j+1] = temp[j];
    temp[j] = temp2;
    }
    }
    }


    for(int i=0;i<temp.length;i++){
    System.out.println("temp[i]"+temp[i]);//这里是有值的
    System.out.println("value1"+tmp2.get(temp[i]));//不知道为什么我这里打印出来的值为空

    }
      

  3.   

    import sun.audio.*;
    import java.io.*;public class Sound
    {
    public static void main(String args[])
    {
    try {
    FileInputStream fileau=newFileInputStream("D:\\Javawork\\Study\\bin\\csdn\\sounds\\Sleep.wav");
    AudioStream as=new AudioStream(fileau);
    AudioPlayer.player.start(as);
    }
    catch (Exception e) {}
    }
    }
      

  4.   

    首先treemap是排序的,根据key排序,其次,key是不可以重复的,会覆盖
      

  5.   

    treeset是不允许重复键,而且会自动进行排序,建议楼主用HashMap吧,再用collection类取出里面的键和值输出吧,用过,还行的呢