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.
Related
With the last improvement in matrices, you can add, update and delete an element but only in matrix.
But for:
Map<String, Object> comentarios = new HashMap<>();
I probe it and it transforms me a Map to array does not matiene the same type.
Someone know how I should do it?
My code is:
final Map<String, Object> nuevoComentario = new HashMap<>();
nuevoComentario.put("comentario", FieldValue.arrayUnion("id5", "Msj add"));
db.collection("Hospedaje").document(documentGetID)
.update(nuevoComentario);
From what I understand FieldValue.arrayUnion and FieldValue.arrayRemove are just for array fields, which HashMap isn't really.
Try using db.collection("Hospedaje").document(documentGetID).update("myHashMapField", "key" to /value/);
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);
HashMap<String, String> foo = new HashMap<String, String>();
HashMap<String, String> baar = new HashMap<String, String>();
How to remove items found in baar from foo?
You can try:
foo.keySet().removeAll(baar.keySet())
Changes to a Map's keySet() are reflected in the map itself.
If you want to remove exact mappings (not just based on keys), you can use the same approach with the entrySet() instead:
foo.entrySet().removeAll(baar.entrySet());
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<>();
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?)