Make A Hashmap with its keys being Strings values being HashSets - java

Trying to make a Hash Map with keys of strings, and values of HashSets. I want the hash sets to be all integers
Set<String> numbersSet = new HashSet<Integer>();
// won't work:
HashMap<String, numberSet> database = new HashMap<String, numberSet>();//error - "( or [ expected"
//Also won't work. If it did, how can even I add to the set inside this hashMap?
HashMap<String, HashSet<Integer>> database = new HashMap<String, HashSet<Integer>>(); //error-incompatible types

HashMap<String, HashSet<Integer>> database = new HashMap<String, HashSet<Integer>>();
Works for me.
BTW, if you are using JDK 1.7 you can use:
HashMap<String, HashSet<Integer>> mymap = new HashMap<>();

Related

Create HashMap from HashSet value using loop

I'm trying to create a HashMap from values of a HashSet (which itself is stored as values in a HashMap).
I'm not sure whether to Iterate or use a for loop.
Example:
newMap = new HashMap<String, HashSet<String>>();
HashSet<String> boxing = new HashSet<String>();
newMap.put("fighting", boxing);
boxing.add("jab");
boxing.add("hook");
boxing.add("uppercut");
Now I need to iterate or loop through so that each value in the 'boxing' HashSet creates a new HashMap with the value as the key for the new map.
So newMap1 would have 'jab' as the key, newMap2 would have 'hook' as the key and so on.
Any help is appreciated.
Since Java 9 you can do this:
Map<String, Set<String>> newMap = Map.of("fighting", Set.of("jab","hook","uppercut"));

java how to combine two hashmap without duplicate entries

I am trying to combine two different hashmaps. But in those two maps, there are some duplicate entries, so I have to remove thoese entries after combine.
For example:
HashMap 1:
100 hello
101 nice
HashMap 2:
100 hello
102 good
After combine, the hashmap should looks like:
100 hello
101 nice
102 good
I have tried putall,but seems it does not remove duplicate entries.
Could someone help me on a fast way to do that?
I don't see a problem in your approach.
HashMap<String, Object> first = new HashMap<String, Object>();
HashMap<String, Object> second = new HashMap<String, Object>();
first.put("100", "hello");
first.put("101", "nice");
second.put("100", "hello");
second.put("102", "good");
first.putAll(second);
System.out.println(first);
outputs
{102=good, 101=nice, 100=hello}
Try to check, if your handling of HashMaps is correct.
The keys of a HashMap can't be duplicates, because they are represented in a Set, so merging the two maps should do the work.
you can add two hashmap map1 and map2 to third hashmap map3 just declare the input type for value as Object as all the data type have their super class as Object class.
HashMap<String, Integer> map1 = new HashMap<String, Integer>();
HashMap<String, String> map2 = new HashMap<String, String>();
HashMap<String, Object> map3;
map3 = new HashMap<>();
map3.putAll(map1);
map3.putAll(map2);

Best design/coding to accomplish the below requirement?

I am using Java 1.7. I have below map.
Map<String, String> keyValues = new HashMap<>();
But map can contain values as below.
keyValues.put("one", "value1");
keyValues.put("two", "value2");
OR
keyValues.put("four", "value1");
keyValues.put("two", "value2");
keyValues.put("seven", "value3");
OR
keyValues.put("one", "value1");
keyValues.put("two", "value2");
keyValues.put("three", "value3");
Basically the map can contain N values where is N is generic it can contain any number of values.
I have one more value for group of keys as below.
keys one, two belongs to 12345SRT
keys one, two, three, four belongs to 12345SRTSSS
keys four, two, seven belongs to 764RTYL87
Now map can contain any one of above key sets.
In that case based on the key set in the map it has to return corresponding value.
Ex:
If map contains one and two then it should return 12345SRT.
If map contains four,two and seven then logic should return 764RTYL87.
What is the best place to keep above key sets and corresponding values?
Shall i consider enum?
The logic has to take map and return value.
What is the best way to do that?
You can use Apache MultiKeyMap
Example
MultiKeyMap multiKeyMap = new MultiKeyMap();
multiKeyMap.put("New York","IBM","Sam");
multiKeyMap.put("Sydney","Infosys","Honey");
multiKeyMap.put("Prague","JP Morgan","Peter");
multiKeyMap.put("Scotland","RBS","Deny");
multiKeyMap.put("Paris","Nomura","Lily");
multiKeyMap.put("Melbourne","Citi Bank","Sandy");
multiKeyMap.put("Aukland","Bank of America","Tommy");
Resultant map
Similar question: How to implement a Map with multiple keys?
EDIT :
You can have a custom key class which can have N number of keys.
Something like
Class MyKey{
List<String> keys;
}
Map<MyKey, String> keyValues = new HashMap<>();
Also override appropriate methods of Map like equals,hashcode,get etc.
Maybe this?
HashMap<Set<String>, String> multiMap = new HashMap<Set<String>, String>();
Set<String> mk1 = new HashSet<String>();
mk1.add("one");
mk1.add("two");
mk1.add("three");
multiMap.put(mk1, "derp");
Set<String> checker = new HashSet<String>();
checker.add("two");
checker.add("three");
checker.add("one");
if(multiMap.containsKey(checker))
System.out.println(multiMap.get(checker));
Try running this and see if it's the behavior you're wanting.
You can use as below :
Map<String, ArrayList<String>> keyValues = new HashMap<String, ArrayList<String>();
keyValues.put("12345SRT",["one","two"]);
keyValues.put("12345SRTSSS",["one", "two", "three", "four"]);
keyValues.put("764RTYL87",["four", "two", "seven"]);
Ex:if you have a map object with keys as "one" and "two"
String getKey(map){
for (Map.Entry<String, Object> e : keyValues.entrySet()) {
String key = e.getKey();
ArrayList<string> value = e.getValue();
if(value.containsAll(map.keySet())){
return key;
}
}
}

HashMap from List of Files

I'd like to explore the option of using a HashMap to keep track of changes between files. I'm using a few config/text files to give a set of documents of status:
The config file looks like:
STATUS1 = "Doc1.pdf, Doc2.xls, Doc5.doc"
STATUS2 = "Doc8.pdf, Doc6.doc"
STATUS3 = "Doc10.pdf"
...
Instead of having to create a separate HashMap for each instance like so:
Map<String, String> map1 = new HashMap<String, String>();
Map<String, String> map2 = new HashMap<String, String>();
Map<String, String> map3 = new HashMap<String, String>();
map1.put("STATUS1", "Doc1.pdf");
map2.put("STATUS1", "Doc2.xls");
map3.put("STATUS1", "Doc5.doc");
I'd like to have only a single Map with the key of the status and the values mapped to that key.
I don't need help in parsing the file, I just need assistance in implementing the HashMap or Map so I can add this functionality. If there are other datatypes or methods of organizing this data, I'd like to hear your opinions on that.
Any help would be much appreciated.
You can use a MultiMap, which stores multiple values for the same key.
Multimap
Multimap<String, String> myMultimap = ArrayListMultimap.create();
// Adding some key/value
myMultimap.put("STATUS1", "somePDF");
myMultimap.put("STATUS1", "someDOC");
myMultimap.put("STATUS1", "someXCL");
myMultimap.put("STATUS2","someFormat");
// Getting the size
int size = myMultimap.size();
System.out.println(size); // 4
// Getting values
Collection<string> stats1 = myMultimap.get("STATUS1");
System.out.println(stats1); // [somePDF, someDOC, someXCL]
2 . HashMap
With HashMap you can have something like,
List<String> listOfDocs = new ArrayList<String>();
listOfDocs.add("somePDF");
listOfDocs.add("someDOC");
listOfDocs.add("someFormat");
HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
// key would be your STATUS
// Values would be ListOfDocs you need.
map.put("STATUS1", listOfDocs);
map.put("STATUS2", listOfDocs2);
map.put("STATUS3", listOfDocs3);
Hope this helps.
Let me know if you have questions.

What is the best way to create a copy of a Map using pass-by-value?

If I have a Java map with 100s of values in it, and I wanted to create another copy of it using this code :
LinkedHashMap<String, Vector<String>> map1 = new LinkedHashMap<String, Vector<String>>();
LinkedHashMap<String, Vector<String>> map2 = new LinkedHashMap<String, Vector<String>>( map1 );
Then if I change any value in any Vector entry for map1 it will be affected in map2 also. I do not want that. I want map2 to be totally independent on map1.
What is the best way to do that ?
Basically, you'll need to clone each vector:
LinkedHashMap<String, Vector<String>> map2 = new LinkedHashMap<String, Vector<String>>();
for (Map.Entry<String, Vector<String>> entry : map1.entrySet()) {
Vector<String> clone = new Vector<String>(entry.getValue());
map2.put(entry.getKey(), clone);
}
You don't have to go any deeper than that though, of course - because String is immutable.
(Any reason you're using Vector rather than ArrayList, by the way?)

Categories