Reputation: 11777
Is it possible to create a HashMap whose key is an array of integers?
I´m accustomed to use Python and just recently I began working with Java. In my job I need to create a HashMap with keys like:
map.put([5,2], 1);
map.put([3,2], 0);
and so on. I´m using it to later on test if a pair of those numbers are present in the map, if yes, then do something, if not, continue.
To do so I tried the following:
Map<Array, Boolean> test = new HashMap<Array, Boolean>();
int[] a1 = {5,2};
test.put(a1, true);
Eclipse gives the message ("The arguments are not applicable to int[]..."). But any configuration I´ve done I get some error.
I tried using ArrayList, Objects inside the map, nested HashMap and so on but none worked (in python it´s very easy, I´d just write dict[(5,2)] = 1, so I imagine in Java there´s something simple like that). I was suggested to transform the numbers into Strings and add a colon between then, like:
map.put("5:2", 1);
and later on I break the string again but if this is the solution I´ll go back to Python ;)!!
Probably this is a very simple question but I couldn´t find the answer, hope you can help me.
Thanks in advance!
Upvotes: 2
Views: 2710
Reputation: 16545
If you want to check for the existance of your entry, you can use a Set
(a useful concrete implementation is HashSet
.
final Set<List<Integer>> population;
population = new HashSet<List<Integer>>();
population.add(Arrays.asList(1, 2));
if (population.contains(Arrays.asList(1, 2)))
{
...
}
You can use an List
as I have done above - but that doesn't guarantee that all your lists are exactly two elements long (if that is indeed a constraint). To make it a bit more robust, you could create your own class to represent the tuple. If you do, make sure you implement equals()
and hashCode()
(here's an article explaining good practice).
Arrays.asList()
is a useful way of creating a list in-line in the code. A more general list is an ArrayList
.
Upvotes: 2
Reputation: 43391
The simplest thing would be a Map<List<Integer>, Boolean>
-- or even just a Set<List<Integer>>
, since you don't care about the value as much as whether the key is there.
The more java-y solution would be some class that represents the two ints:
public class Coordinate { // or whatever
private final int x;
private final int y;
// constructor and overrides for equals, hashCode and toString
}
Then have a Set<Coordinate>
.
This is considered more idiomatically Java because the class name tells you exactly what this set is for -- and enforces that it's used that way. A Set<List<Integer>>
, on the other hand, could be lots of things: coordinates, lottery pickings, SSNs for people in a given department, credit cards for a payment... you the programmer have no way of knowing just by looking at the type, and it's easy for a set of numbers that means on thing to be accidentally used in another context. A Set<Coordinate>
can't be anything other than a set of coordinates.
Upvotes: 2
Reputation: 23455
This works:
Map<List<Integer>,Boolean> map = new HashMap<>();
map.put( Arrays.asList( new Integer(1), new Integer(2) ), false );
map.put( Arrays.asList( 4, 5 ), true ); // Integer type is inferred and ints are autoboxed
map.get( Arrays.asList( 1, 2 ) ); // gets the Boolean object for "false"
Upvotes: 1
Reputation: 4704
Actually java is a lot less expressive than python so you'll have to write a bit more code.
I think you need to use a map with an ArrayList
in the following way:
ArrayList al = map.get("key");
if (al == null) {
al = new ArrayList();
map.put("key", al)
}
al.add(some_object);
You also may use the array as the key (as you asked for), probably you want an immutable array. The key for the hash map to work is use some object that has a good implementation of equals
and hashCode
.
This is the way that it is done in java, you may use any kind of collection though, Sets are more common.
Cheers!
Upvotes: 0