I have a table consisting 10 columns in database. One of the columns have alphaneumaric values which was set as a key for the hashmap which will have all the values as objects for each row in the hashmap.
example : A10 (key) , rowobj(Class). Now in that same column another value is present as A10. So now if we are trying to load the values to hashmap from the tableas the key name is same will there be multiple values enrolled to that same key name?
Also can we combine the values of 2 columns to create a unique key for the hash map? How to do that?
Thanks
Two options:
create a wrapper object that contains all values as fields, set them, and then map.put(id, rowObject)
use Multimap (guava)
Can't you use primary key of the table as the key of your HashMap ?
You could use a HashMap where the key is still A10 but now the value is a List.
So you could have multiple values for the same key. You only have to pay attention in the insertion that new List is created when the first element is inserted. May be something like (in pseudocode):
HashMap<String, List> myMap = new HashMap<String,List>;
for (elements to insert){
if (!myMap.containsKey(element.key()))
ArrayList myList = new ArrayList();
myList.add(element);
myMap.put(element.key(), myList);
}else{
ArrayList myList = myMap.get(element.key());
myList.add(element);
myMap.put(element.key(), myList);
}
}
If you mean that a Key x should map to a list vals (which are the values that x represents), then that is easily doable like this (didnt check the syntax, so dont expect it to compile):
//assuming that the keys are of type int and values are of type String
Map<int,List<String>> myMap = new HashMap()<int, new ArrayList()<String>>;
Related
I have two string value ID and Value coming from my code, I need to store this in a map without iterating over the map but by checking by the key value if it exists in the map.
The ID can come many times with different value and in that case I have add it to the already existing entry of the ID and add the value along with the existing value
I created a Map with String and List to add the values but I am facing difficulties,
Map < String, List< String >> accessMap = new HashMap < String, List< String>>();
If the key is not present, add a new entry with ID and Value (as List).
How to find the key in the map and get the entrySet without iterating over the map and add the value alone, if the ID is already present in the map.
Example,
while(accessList.hasNext()){
....
....
String id = accessEntry.get(ID);
String value = accessEntry.get(Value);
/*Add the id and value to the map if not present, if ID is present add the *value alone to the entrySet.Value (which is a list)
*/
}
The id and value has to be added into a map checking if the ID is already present in the map, if not create a new entry.
The accessList might have many ID references with different value, in that case the value should be added to the entrySet of the already existing entry, the value would be a list with single value or multiple value.
public static void add(Map<String, Set<String>> map, String key, String value) {
map.compute(key, (id, values) -> {
(values = Optional.ofNullable(values).orElseGet(LinkedHashSet::new)).add(value);
return values;
});
}
For values it is better to use Set to exclude duplications.
Use computeIfAbsent() to initialise a List for the id if one doesn't exist already, then just add the value to that:
entitlementMap.computeIfAbsent(id, s -> new ArrayList<>()).add(value);
I'm checking to see if a key in my HashMap exists, if it does, I also want to check to see if any other keys have a value with the same name as that of the original key I checked for or not.
For example I have this.
System.out.println("What course do you want to search?");
String searchcourse = input.nextLine();
boolean coursefound = false;
if(hashmap.containsKey(searchcourse) == true){
coursefound = true;
}
This checks to see if the key exists in my hashmap, but now I need to check every single key's values for a specific value, in this case the string searchcourse.
Usually I would use a basic for loop to iterate through something like this, but it doesn't work with HashMaps. My values are also stored in a String ArrayList, if that helps.
You will want to look at each entry in the HashMap. This loop should check the contents of the ArrayList for your searchcourse and print out the key that contained the value.
for (Map.Entry<String,ArrayList> entries : hashmap.entrySet()) {
if (entries.getValue().contains(searchcourse)) {
System.out.println(entries.getKey() + " contains " + searchcourse);
}
}
Here are the relevant javadocs:
Map.Entry
HashMap entrySet method
ArrayList contains method
You can have a bi-directional map. E.g. you can have a Map<Value, Set<Key>> or MultiMap for the values to keys or you can use a bi-directional map which is planned to be added to Guava.
As I understand your question, the values in your Map are List<String>. That is, your Map is declares as Map<String, List<String>>. If so:
for (List<String> listOfStrings : myMap.values()) [
if (listOfStrings .contains(searchcourse) {
// do something
}
}
If the values are just Strings, i.e. the Map is a Map<String, String>, then #Matt has the simple answer.
I have this hashmap of students which stores the id, name and last name.
So I created this :
Map<Interger, HashMap<String,String>> students = new HashMap<>();
where the second hashmap stores the name and lastname.
My goal is to look for a student in a swing application, I succeed in searching with id because it's the key of the first hashmap, but i'd like to use all of them like this:
So my question is : If I want to search by name or last name, how can i get the value of the first hashmap and put it in a new hashmap ?
You can iterate on the hashmap like this :
private int searchByName(String s) {
for(Map.Entry<Integer, HashMap<String, String>> entry : students.entrySet()) {
HashMap student = entry.getValue(); // Hashmap containing first and lastname
if (student.containsKey(s)) // If name match
return entry.getKey(); // Return the student ID
}
return 0; // Student not found
}
For the lastname just use containsValue(s) instead of containsKey(s)
You can use the Google Collections API Guava for that specifically a BiMap
A bimap (or "bidirectional map") is a map that preserves the
uniqueness of its values as well as that of its keys. This constraint
enables bimaps to support an "inverse view", which is another bimap
containing the same entries as this bimap but with reversed keys and
values.
With this you'will be able to search using first name and last name. This BiMap will be value to your first parent hashmap.
I am not sure if the data structure is the best for your use-case but answering to your question, you should try using values() method which would return you the collection of values of the Map
http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html#values()
Collection res = studensts.values();
Iterator<HashMap<String, String>> i = res.iterator();
Map<String,String> resM = null;
while(i.hasNext()){
resM = i.next();
}
I want to write a program that prints out entries "0" and "4" of the HashMap (i.e. entry.getKey(0) and entry.getKey(4) but it won't let me do this) What would be another way using what I already have?
Basically I have this:
HashMap<String, Integer> hm = new HashMap<String, Integer>();
I can iterate over each entry using this code:
for (Map.Entry<String,Integer> entry : hm.entrySet())
{
System.out.println(entry.getKey() + "/" + entry.getValue());
}
Since people have asked for more contextual information, I am storing a set of strings in the HashMap. For example the 0th entry is "Bob", the 1st entry is "Mindy", the 2nd is "Yasser", the 3rd is "Greg" and the 4th is "Jacky." I want the program to print out the 0th and 4th entries of the populated HashMap.
If you are specific about the keys in the Map, you can directly use get() method.Like this,
Integer value = hm.get("0");
If you want to iterate then use something like the code below :
HashMap<String, Integer> hm = new HashMap<String, Integer>();
for (Entry<String, Integer> entry : hm.entrySet())
{
String key = entry.getKey();
if(key.equals("0") || key.equals("1"))
System.out.println(key + "/" + entry.getValue());
}
You cannot pass index to the getKey() method like getKey(0) etc. Refer the documentation.
HashMap class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time. So, if you are looking to fetch values from a HashMap based on index, probably it is not possible. Closest to your requirement will be something like LinkedHashMap, which maintains the order of keys for insertion/access order.
HashMap works on principle of hashing, we have put(key, value) and get(key) method for storing and retrieving Objects from HashMap. When we pass Key and Value object to put() method on Java HashMap, HashMap implementation calls hashCode() method on Key object and applies returned hashcode() into its own hashing function to find a bucket location for storing Entry object, important point to mention is that HashMap in Java stores both key and value object as Map.Entry in bucket which is essential to understand the retrieving logic.
The HashMap has no defined ordering of keys. You may use LinkedHashMap instead of HashMap It will always return keys in same order (as insertion) when calling keySet(). And then you pick the 0th or 4th key.Later you can retrieve the value for the keys you fetched at 0th and 4th location.
I would recommend simply using the get() method with the provided key. Iteration is not necessary in this case.
Since people have asked for more contextual information, I am storing a set of strings in the HashMap. For example the 0th entry is "Bob", the 1st entry is "Mindy", the 2nd is "Yasser", the 3rd is "Greg" and the 4th is "Jacky." I want the program to print out the 0th and 4th entries of the populated HashMap.
With how it is being used, an HashMap makes no sense. Use a String[] instead!
String[] names = {"Bob", "Mindy", "Yasser", "Greg", "Jacky" };
System.out.println("When " + names[0] + " met " + names[4]);
Why not a targeted approach:
String[] targets = {"0", "4"};
for (String target : targets) {
System.out.println(target + "/" + hm.get(target));
}
Way more efficient than the "big hammer" full iteration approach, and you get output order for free.
If you have a lot of entries in your Map you may considerate to use one of the navigable collections, like TreeMap.
NavigableMap<String,String> map = new TreeMap<>()
map.subMap("0", true, "4", true);
Visit: http://docs.oracle.com/javase/6/docs/api/java/util/NavigableMap.html
I have the following list:
List<ArrayList> list;
list.get(i) contains the ArrayList object with the following values {p_name=set1, number=777002}.
I have to create a
Map<key,value>
where the key contains the p_name, and values are the numbers.
How to do it easily and fast as there can be hundreds of entries in the initial list and each number can be present in multiple p_name entries.
Update: Here is my current solution
List<Row> list; //here is my data
Map<String,String> map = new TreeMap<String,String>();
for (Row l : list) {
if (l.hasValues()) {
Map<String, String> values = l.getResult(); // internal method of Row interface that returns a map
String key = values.get( "number");
map.put(key, values.get( "p_name" ));
}
}
The method works, but maybe it could be done better?
PS : There is an obvious error in my design. I wonder if you find it :)
Sine the key can have more then one values, what you are looking for is a MultiMap. Multimap
Or a simple map in the form
Map<Key,ArrayList<Values>>
There is no "fast" way here to me. You still need to iterate through all the elements and check all the values.
And actually hundreds to Java is not much at all