Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Map in Java

Tags:

java

map

I'd like to create a map that contains entries consisting of (int, Point2D)

How can I do this in Java?

I tried the following unsuccessfully.

HashMap hm = new HashMap();  hm.put(1, new Point2D.Double(50, 50)); 
like image 267
Kevin Meredith Avatar asked Feb 07 '13 04:02

Kevin Meredith


People also ask

Can you make a Map in Java?

Add Items to make a Map To make a map, place 8 papers and 1 compass on Java Edition (PC/Mac), Xbox and PS in the 3x3 crafting grid. In PE and Windows 10, you need 9 papers to make a map.

What is a HashMap Java?

Java HashMap is a class which is used to perform operations such as inserting, deleting and locating elements in a map. We create a map, where we pass two kinds of values which are 'key' and 'value'.

Why we use HashMap in Java?

The HashMap class of the Java collections framework provides the functionality of the hash table data structure. It stores elements in key/value pairs. Here, keys are unique identifiers used to associate each value on a map. The HashMap class implements the Map interface.


2 Answers

Map <Integer, Point2D.Double> hm = new HashMap<Integer, Point2D>(); hm.put(1, new Point2D.Double(50, 50)); 
like image 76
hd1 Avatar answered Oct 13 '22 00:10

hd1


Java 9

public static void main(String[] args) {     Map<Integer,String> map = Map.ofEntries(entry(1,"A"), entry(2,"B"), entry(3,"C")); } 
like image 30
Durgpal Singh Avatar answered Oct 13 '22 00:10

Durgpal Singh