简体   繁体   中英

print specific record from Hashmap

I have a Hashmap and i am struggling on how to print a single key and value. i am able to print all of them but would like to know how to just print one of them thanks

import java.util.HashMap;


public class Coordinate {

static class Coords {
    int x;
    int y;

    public boolean equals(Object o) {
        Coords c = (Coords) o;
        return c.x == x && c.y == y;
    }

    public Coords(int x, int y) {
        super();
        this.x = x;
        this.y = y;
    }

    public int hashCode() {
        return new Integer(x + "0" + y);
    }

    public String toString()
    {
        return x + ";" + y;
    }


}

public static void main(String args[]) {

    HashMap<Coords, String> map = new HashMap<Coords, String>();

    map.put(new Coords(65, 72), "Dan");


    map.put(new Coords(68, 78), "Amn");
    map.put(new Coords(675, 89), "Ann");

    System.out.println(map.size());
    System.out.println(map.toString());

}
}

At the moment it shows

3
{65;72=Dan, 68;78=Amn, 675;89=Ann}

but would like it to just show

65;72=Dan

thanks for looking

The Map.get(K) method allows you to retrieve the value of a desired key. So you can do this:

Coords c = new Coords(65,72);
System.out.println(c + " -> " + map.get(c));

This works for any sort of Map, including HashMap and TreeMap. You can also obtain a set of all keys in the map using Map.keySet() .

只是派生自HashMap并覆盖其toString方法

You want a specic behavior on the invocation of hash map. The default and generic behavior for hash map is to print all the elements. If you want the specific behavior you are better off wrapping it in your own class and providing the custom toString implementation. Also, why don't you consider just printing down the particular element after retrieving it from the map.

i think you must have it like, looks more systematic (key will be unique) :

HashMap<String, Coords> map = new HashMap<String, Coords>();    
map.put("Dan", new Coords(65, 72));
map.put("Amn", new Coords(68, 78));
map.put("Ann", new Coords(675, 89));

Then for specific value you have to do System.out.println(map.get("Dan").toString()); it will return the coordinates

UPDATE: As per the your code it will be :

System.out.println(new Coords(x, y) + "=" + map.get(new Coords(x, y)));

The Map has a method called get() which can accept a key. For a given co-ordinate the equals and hashcode method will be called to find a matching value. Use this method.

PS: Your equals method always assumes that the object to compare with is a Coords, which may not be the case.

i think here you can use just map.get(key) method to extract the value. if you need a fancy look other than the formal one, override the toString() method inside the class.

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