简体   繁体   中英

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()

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. Try this, it should work:

 mapp.get((long)1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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