Whats the benefit of allowing multiple null keys in hashmap? - java

Just for experimenting, I added multiple null keys in a Hashmap instance. And it didn't complain. What's the benefit of doing that?
The code is,
Map hmap = new HashMap();
hmap.put("sushil","sushil11" );
hmap.put(null,null);
hmap.put(null,"king");
hmap.put(null,"nagasaki");
hmap.put(null,null);
How many keys are there in the map?

I would guess you haven't added multiple null-keys. You just overwrote the same nullkey multiple times.

A normal hashmap will have unique keys, so you're overwriting the entry for the null key repeatedly. You won't have multiple identical keys (for this you need a MultiMap or similar)

It is used to get switch:case:default behavior.
Example:
Problem Definition: Coffee shop in CS Department building. They provide coffee to CS Student for $1.00, to IT department students $1.25 and others for $1.50.
Then Map will be:
Key -> Value
IT -> 1.25
CS -> 1.00
null -> 1.50
if(map.containsKey(dept))
price = map.get(dept);
else
price = map.get(null);
P.S. - I am not "Department-ist" if that's a word. :)

There's an API call for this:
size: Returns the number of key-value mappings in this map.
hmap.size();
As noted you're just overwriting the key/value pair with a new value.

The question has asked about having multiple keys in HashMap which is not possible.
If you pass null again and again the old value is replaced only.
Refer:
http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/HashMap.java#HashMap.putForNullKey%28java.lang.Object%29

HashMap allows multiple null values but only one null key.
If you write multiple null keys like below, then null will be overrides and you'll get the final overridden result. i.e "world"
Map<String, String> valueMap = new HashMap<>();
valueMap.put(null, "hello");
valueMap.put(null, "world");
System.out.println(valueMap.get(null));
Output:
"world"

Related

questions about choosing suitable data structure in Java

My problem is that there are 3 variables, say a,b,c
First, information a,b,c needed to be input and stored.
For example a=Tom, b=school A, c=1402
Then, I can get the above information by entering "Tom", in this case, if there is another Tom in school B,that message would also be printed out.
or by entering "Tom, school A", then only that Tom would be printed out. In this problem, their would be no duplicates if a and b are both locked.
Well, my view is that a must be a key, and a+b can be a composite key. So firstly I thought of a HashMap, however, HashMap has only one key to one value. So I intended to use 2 maps but this could not make use of composite key.
So are my thoughts correct? Or are there any better data collections in Java could be used in this case?
Thank for your time!
There is no off the shelf data structure in Java for your need. There are multi valued map implementations in Apache Commons and Google Guava Collections but not in core Java.
But , In my opinion, you can implement it with a map declaration like - Map<String, List<String>> or Map<String, List<Student>> depending on whether you are keeping your student details as concatenated strings or as a Student class.
While populating the map - you will make two entries in map for first row / student , once for a and another entry for a+b concatenated.
Then while making entries for subsequent rows , you should first check if key exists in map or not, if it exists, you get the value , append new value and store again.
Record a=Tom, b=school A, c=1402 will have two entries then comes record a=Tom, b=school B, c=1403 and you will append record for key = Tom and add a new entry for TomschoolB so in total three entries in map for keys - Tom , TomschoolA and TomschoolB. Key Tom will have two items in value list while rest two will have single items.
Your lookup code should follow key calculation logic as key creation logic i.e. string concatenation or any other things as you wish.
This is just give a rough idea that multiple values can be handled via list.
Edit : From your comments, you look confused so below is code to implement the idea. You can tweak and extend it as you wish.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MultiValuedMap {
private static Map<String,List<String>> storage = new HashMap<>();
public static void main(String[] args) {
store("Tom","Tom,schoolA,1402");
store("TomschoolA","Tom,schoolA,1402");
store("Tom","Tom,schoolB,1402");
store("TomschoolB","Tom,schoolB,1403");
storage.forEach((key,value) -> System.out.println("key:"+key+",value:"+value));
}
private static void store(String key, String value){
if(storage.containsKey(key)) {
List<String> newList = storage.get(key);
newList.add(value);
storage.put(key,newList);
}else{
List<String> values = new ArrayList<String>();
values.add(value);
storage.put(key,values);
}
}
}
Output:
key:Tom,value:[Tom,schoolA,1402, Tom,schoolB,1402]
key:TomschoolB,value:[Tom,schoolB,1403]
key:TomschoolA,value:[Tom,schoolA,1402]
When you feel ambiguous of using any existing datastructure I suggest you to create your own data structure.With that you can create any methods and anything according to your need.

Check if Set of Object contain an Object with this attribute

Consider i have a List Of Objects, and i convert it to a Set to make some actions like this :
List<User> listUser = new ArrayList<>();
listUser.add(new User(1, "user1"));
listUser.add(new User(2, "user2"));
listUser.add(new User(3, "user3"));
Set<User> myset = new HashSet<>(listUser);
I know there are a contains(Object o) but i want to check with the attributes of my Object.
My Question is What is the best way to check if the Set contain an Object with ID = 1 or user = "user1" or any attribute
Thank you.
Does your User class override equalsusing just the ID? If so, you could use:
if (mySet.contains(new User(1, "irrelevant"));
Note that it's quite odd to have something called mySet which is actually a List rather than a Set... I'd consider either changing the name or the type. If you use HashSet you'll need to override hashCode as well as equals, but you should do that anyway to obey the normal contracts from Object.
If you want to be able to check by multiple different attributes - or if you don't want to override equality in User - then you could consider creating maps instead, e.g.
Map<Integer, User> usersById = new HashMap<>();
Map<String, User> usersByName = new HashMap<>();
...
Then you need to keep all the maps in sync, of course - and you need to consider what happens if you've got multiple users with the same name, etc.
You are correct that you cannot use Java streams with Java 7 (they were introduced in Java 8). For Java 7 I would suggest an enhanced for loop in which you check your search condition (if (u.getId() == 1)) and act appropriately.
If you are ever getting a problem with the linear search that the for loop would do, you may build maps: HashMap<Integer, User> for allowing lookup by ID and HashMap<String, User> for lookup by user name. Map.containsKey() will tell you whether the map contains a specific user. You will need to take care that the maps contain all your users, and also that users are removed from the maps when deleted from your set/list, of course (oops, only saw now that Jon Skeet has already said all of this, sorry).
For anyone using Java 8 and reading along here, use of streams needs not be very fancy. A couple of examples:
boolean containsUser1 = myset.stream().anyMatch(u -> u.getId() == 1);
This yieds true.
User[] u1Array = myset.stream().filter(u -> u.getId() == 1).toArray(User[]::new);
Set<User> u1Set = myset.stream().filter(u -> u.getUserName().equals("user1")).collect(Collectors.toSet());
Each of these two yield [1 user1] (the first one as an array, the second as s Set, obviously). Each use of a stream does a linear search behind the scenes.
That said, you can do fancy things with streams. I’ve found it a pleasure learning them and using them, look forward to that.
Simpliest and fanciest way i think is to override equals method in your User class by just considering the id.
Then you can use the contains method from the List interface

Select every row with exact map key and value

Here is a part of my Person class:
#Entity
public class Person {
#ElementCollection(fetch = FetchType.EAGER)
private Map<String, String> externalPropertyMap = new HashMap<>();
}
So i need to select every Person with exact key and value from this map. For example select every Person with key="prefferedNumber" and value="123456"
How can i do this?
Actually this is not accurated:
So i need to select every Person with exact key and value from this map
You have a HashMap, a Map implementation which don't allow repeated keys, so, you just will get one single value for each key from the HashMap.
To achieve this you need to use another Map implementation, but don't worry, someone has your problem already:
Try this implementation
MultiMap from ApacheCommons
A MultiMap is a Map with slightly different semantics. Putting a value into the Map will add the value to a Collection at that key. Getting a value will return a Collection, holding all the values put to that key.

How can I properly relate multiple BiMap collections using shared keys?

I have the following BiMap collections:
BiMap<String,String> accessIds = HashBiMap.create();
accessIds.put("FOO","accessId 1"); //This access Id is common to both FOO and BAR
BiMap<String,String> merchants = HashBiMap.create();
merchants.put("FOO", "merchant 1"); //Both FOO and BAR each have unique merchants
merchants.put("BAR", "merchant 2");
These are 2 of the 4 total collections I currently have. All 4 collections share the same keys, but different values.
The question I have is: How can I ensure that I can get merchant 2 when I have an accessIds key of FOO?
Before someone points out that these two collections do not, in fact, share the same keys, please remember that a BiMap enforces unique values so I am unable to list "BAR","accessId 1" in the collection.
I'm not convinced that BiMap is the right collection, but I do make use of its inverse() method. If there is a collection better suited ( or some other method that I am overlooking ) please let me know.
FYI: I use Guava-14.0-rc1 for the BiMap collection.
Based on your comment, in your workflow, the Access ID is a key, not a value, that in at least one case has several associated values instead of one.
You could use a Multimap for your Access IDS, assuming you can then select which value to retain as the key for accessing the other Maps (or BiMaps, though it's unclear through your example why they are BiMaps, but I guess that's unrelated).
ImmutableMultimap.Builder<String, String> builder = ImmutableMultimap.builder();
builder.put("FOO", "accessId 1");
builder.put("BAR", "accessId 1");
ImmutableMultimap<String, String> accessIds = builder.build();
ImmutableMultimap<String, String> byAccessIds = accessIds.inverse();
Collection<String> keys = byAccessIds.get("accessId 1"); // ["FOO", "BAR"]
String key = doSomething(keys); // "BAR" is chosen
String merchant = merchants.get(key); // "merchant 2"
If you cannot use immutable structures, you can also build a regular Multimap for accessIds (for example using a HashMultimap) and inverse it using Multimaps.invertFrom().

Is it possible to have a hashmap with 4 objects?

Can I have an hashMap with say ID as my key and info, name, quantity as my values?
ok, say I have a class (Products) already that sets my variables, getters and setters. In my Invoice class, which is where the hashMap would be. Would I put like:
private HashMap<String, Products> keys = new HashMap<String, Products>
I'm not quite sure how to access the HashMap though. Say I implement a class that allows me to add and remove invoices from the HashMap, I do not know what the values would be:
keys.put(??value of id??,??not sure what goes here??);
Sure. Make another class that contains your info, name and quantity and put that as the value of your HashMap.
No, but the best way is to wrap the information you want to keep in the map in a class:
public class Info {
private String info;
private String name;
private int quantity;
...
public Info(String info, String name, int quantity) {
...
}
}
Then do this to put something in the map:
Info info = new Info("info", "name", 2);
Map map = new HashMap<Integer, Info>();
map.put(22, info);
And do this to get something out:
Info info = map.get(22)
How about HashMap<Integer, ArrayList<String>> ?
UPDATE: Please try to avoid this, this is a better approach.
Not exactly.
A Map defines a strictly 1 to 1 relationship between keys and values. One key in the map has one value.
If you want to associate multiple values with one key you need to do one of the following:
Define a Values class to represent the values as a single object; e.g. as per #Starkey's and #Javed's answers. Then the map becomes a Map<String, Values> (assuming that the key type is String).
Define the map as a Map<String,List<Object>> or Map<String,Object[]> and represent the values as an untyped list / array
Define the map as a Map<String,Properties> or Map<String,Map<String,Object>> and represent the values as the Java equivalent of an associative array.
Of these, the first option is both the safest (smallest chance of runtime errors), the most efficient and the best style.
(Aside: an Apache commons MultiMap might be considered as another possibility, but the conceptual model and APIs don't really match this use-case.)
Sure. Depending on how flexible your datastructe is you can use a Hashmap a la:
HashMap<IdType, List<String>>, with IdType String or Integer, depending on the Keys you like to use.
HashMap<IdType, String[]>
HashMap<IdType, YourObjectType>, with YourObjectType beeing a Object you defined yourself, holding the values you like
YourObjectType can of course be anything you can define as an Object. Also another HashMap if you like.
One of the concerns while using a Map would be use of hardcoded keys. If the key is a string, and the key changes. Can consider using a constant instead of a hardcoded string.
Having a dedicated class has the benefit of compiler to check for name changes. However, as mentioned in the earlier comments.. It can become a concern...
In my opinion both are feasible. We need to weigh which option is better depending on the situation
Create an object that encapsulates the four together. Something like:
public class Foo {
private String s1;
private String s2;
private int v3;
private MyObject obj1
// constructors, getters, helper functions.
}
I think MultiMap from google library could serve the purpose
https://google.github.io/guava/releases/19.0/api/docs/com/google/common/collect/Multimap.html
Multimap<String, String> map = ArrayListMultimap.create();
String key = "uniqueKey";
map.put(key, "value1");
map.put(key, "value2");
map.put(key, "value3");
System.out.println(map);//{uniqueKey=[value1, value2, value3]}
Of course, you could for example declare it like this: HashMap<Integer, HashMap<String,Object>> You use the outer hashmap to link your id with your inner HashMap, and in the inner one, you create keys "info", "name", "quantity" and associate values with them.
Of course, you could also use an ArrayList as the outer collection (it could be a better match for your ID: ArrayList<HashMap<String,Object>> that way you have indexed (id based) access to each of your "info", "name", "quantity" hashmap "records"
You could have ID as key and a List or Set (Collection in general) of objects as value.

Categories

Resources