简体   繁体   English

如何从Java中的HashMap获取值?

[英]How to get values from HashMap in Java?

I want to print the object values.我想打印对象值。 I cant get access to it and dont know how to do it.我无法访问它,也不知道该怎么做。

i cant acess it with value()我无法使用value()访问它

here is my code:这是我的代码:

public class txtdatei {

private String pickerName;
private String language;
private float volumeGain;
private long pickerId;
private static Map<Long,txtdatei> mapp=new HashMap<Long,txtdatei>();

public txtdatei(String username, String language, float volume){

    this.pickerName=username;
    this.language=language;
    this.volumeGain=volume; 
}
public static void main(String[] args){

    File file=new File("test.txt");
    try{
        file.createNewFile();
        FileWriter writer =new FileWriter(file);
        writer.write("username\tbenni\tlanguage\tgerman\n");
        writer.flush();
        writer.close();
        FileReader fr =new FileReader("test.txt");
        BufferedReader reader= new BufferedReader(fr);
        String zeile=reader.readLine();
    String [] data=zeile.split("\t");
    int i=0;
    for(i=0;i<data.length;i++)
    {
        if(data[i].equals("Username"))
                {
                    mapp.put((long)(1),new txtdatei(data[2],data[4],Float.parseFloat(data[6])));
                }
    }       
    System.out.println(mapp.get(1)); //dont know how to read the    
    }catch(IOException ioe){ioe.printStackTrace();}

} }

hope someone can help me,希望可以有人帮帮我,

Thanks.谢谢。

Here is one solution for getting each value of map using the key.这是使用键获取地图的每个值的一种解决方案。

for(Long value: mapp.keySet()){
    System.out.println(mapp.get(value));
}

hope it helps.希望能帮助到你。

First, you set the key being Long, so when you use Map.get((Object)key), u need give a Long to retrive the value.I think 1 is translated to Integer by jvm,thats why you can't get the value.首先,您将键设置为 Long,因此当您使用 Map.get((Object)key) 时,您需要提供一个 Long 来检索该值。我认为 1 被 jvm 转换为 Integer,这就是您无法获取的原因价值。 Try this, it should work:试试这个,它应该可以工作:

 mapp.get((long)1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM