I have a multivalued Hashmap (technically a LinkedHashMap):
private LinkedHashMap<String, ArrayList<BodyPart>> bodyParts = new LinkedHashMap<>();
I want to find the number of values associated with a given key. However, bodyParts.get("sample key") returns null if the key isn't present, whereas I want it to return 0 (as there are zero values associated with that key).
I could shield it in an if statement:
int numberOfValues;
if(bodyParts.containsKey("sample"){
numberOfValues = bodyParts.get("sample").size();
}
but I was wondering if there is an easier/better way to do it? I've read the documentation for computeIfPresent but, truthfully, didn't really understand it.
Use Map.getOrDefault(Object key, V defaultValue).
Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.
You can use getOrDefault method of Java Map interface.
It allows you to set default value that is to be returned in case value corresponding to key is not found. So in use case mentioned above you can use :
numberOfValues = bodyParts.getOrDefault("sample", new ArrayList<BodyPart>()).size();
Am new to java and have a query.Please suggest me a solution :
Am doing API level testing and need to pass the request body parameters of a service like below :
Convert your integer value in string and then pass it, as your method expect String parameter, you cannot directly pass integer
sell.requestAdd( "Amount",String.valueOf(Your_Integer_Value));
Java is strongly typed language, so your Map can't store Integer values, as String is defined as both key and value. If you want to store any object - changed map to Map<String, Object>
You can create a class which holds string and int values. In other words, HaspMap<String,Object>.
I would suggest you create some kind of a class which will represent your map key/values so it will be easier to work with it for other developers.
I am trying to create a data structure in Java that is a Vector into which some information about an unknown number of entities will go from a database. When it comes to this information, I only care about 2 fields. Also, it is required that when I iterate through this Vector, I can extract these two fields (say, String) in pairs. Schematically speaking,
String s1 = Vector[1].Field1, String s2 = Vector[1].Field2
Is this even possible? Does anyone know a more efficient way to achieve this?
Note: I would like to keep it in a single Vector because I pass it to another class for processing.
Use
public class Entry {
public String field1;
public String field2;
}
List<Entry> vector = ...;
Why not using Map<String, String>
But map will be unique keys
Also you can create your own Class and pass it to the Vector<yourclass>
Here's how I would do it :
Create an object Pair with 2 attributes : field 1, field 2
Add this object to your vector.
Use a Vector of maps, with keys value1 and value2. Vector<Map<String, String>>.
Or define a new object with two attributes ( I suggest the second option )
I want to store information using data structure.
For example, I have a data similar to:
Code Applicable values
001 A,B,C,D
004 C,D
005 P,Q,R,S
007 S,C
..
..
..
1000 (Code, Applicable values pair)
Straightforward solution I can think of is having HashMap with key type String and value type HashSet.
I was informed before by architect that having Set in Map is not a good idea.
Any suggestion on how to go about implementing this?
A HashMap<String, HashSet<String>> looks just fine to me for holding the type of data you've shown.
That "architect" of yours might be right about too much complexity when we're talking about how that data structure is exposed to the rest of your program. For example:
That type signature above does not make any statement about what kind of strings are used as keys, and what kind of values are in the value sets. Do they represent names? Or ISBN numbers? Or any text, or only particular enumeration values? etc.
If you use such a HashMap everywhere in your code, when adding values into the sets you will have to remember everywhere to do two steps: (1) creating an empty set only if no value is in the dictionary for some given key, and (2) adding the new value to the set of a given key. Having to think about routine stuff like that opens up the door for bugs.
Perhaps it would be better to hide your actual data structure behind a nice, simple and easy-to-use "collection" interface, perhaps similar to the following:
interface ApplicableValuesCollection
{
void Add(int code, String value);
void Remove(int code, String value);
bool Contains(int code, String value);
Iterable<String> GetValuesOfCode(int code);
…
}
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.