---
# System prepended metadata

title: HashMap 用物件當 Key
tags: [實作相關]

---

HashMap 用物件當 Key
===

```java=
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Object objectOne = new Object();
        Object objectTwo = new Object();
        Object objectThree = new Object();

        // Key 是 Object 型態，Value 是 String 型態。
        HashMap<Object, String> mapOne = new HashMap<>();
        mapOne.put(objectOne, "ValueOne");
        mapOne.put(objectTwo, "ValueTwo");
        mapOne.put(objectThree, "ValueThree");

        String result = mapOne.get(objectOne) + "\n" +
                mapOne.get(objectTwo) + "\n" +
                mapOne.get(objectThree) + "\n";

        TextView v = new TextView(this);
        v.setText(result);
        setContentView(v);
    }
}
```

## Ref.
[https://chris930921.gitbooks.io/android-traveling-gitbook/content/listview/hashmap.html](https://chris930921.gitbooks.io/android-traveling-gitbook/content/listview/hashmap.html)

###### tags: `實作相關`