I have 4 separate hashmaps all of the same type. I would like to merge the values of them all into a single list. I know how to set a List to hashMapOne.values(), but this doesn't help me here since I need to add all values from all 4 lists. Can I do this without looping and individually adding each one?
HashMap<String, MyEntity> hashMapOne = new HashMap<String, MyEntity>();
HashMap<String, MyEntity> hashMapTwo = new HashMap<String, MyEntity>();
HashMap<String, MyEntity> hashMapThree = new HashMap<String, MyEntity>();
HashMap<String, MyEntity> hashMapFour = new HashMap<String, MyEntity>();
List<MyEntity> finalList = new ArrayList<MyEntity>();
List<MyEntity> finalList = new ArrayList<MyEntity>();
finalList.addAll(hashMapOne.values());
finalList.addAll(hashMapTwo.values());
finalList.addAll(hashMapThree.values());
finalList.addAll(hashMapFour.values());
If I were you, I'd just use Stream#of for all Map#values, and then call Stream#flatMap and Stream#collect to transform it to a List:
List<MyEntity> finalList = Stream.of(hashMapOne.values(), hashMapTwo.values(),
hashMapThree.values(), hashMapFour.values())
.flatMap(Collection::stream)
.collect(Collectors.toList());
Related
Hi i have a ArrayList of HashMap and i need the HashMap to be sorted by its key,Value.
ArrayList<HashMap> newList = new ArrayList();
loop start:
HashMap hashData = new HashMap();
hashData.put("name", "string-studentname");
hashData.put("mark", "int-studentmark");
newList.add(hashData);
loop end:
I need the newList to be sorted by the key-mark.
How do i get it?
If you have multiple entries in your ArrayList and the key is the same, you can just sort via:
newList.sort(Comparator.comparingInt(o -> o.get("mark")));
Assuming you're typing the map properly:
HashMap<String, Integer> hashData = new HashMap<>();
hashData.put("mark", 1);
I have getSemiFileInfos() in a list and I wanted to use stream and then loop each of element inside. Each element can still use getItem() or getItem2(). I will first create a map for item and item2 in itemMap, then I will store each loop of getSemiFileInfos() as a map into items list.
What I want is to have it in a single line. I am wondering whether is still possible.
private List<Map<String, String>> items;
items = new ArrayList<Map<String, String>>();
Map<String, String> itemMap = new HashMap<String, String>();
file.getFileInfo().getSemiFileInfos().stream().forEach(m->
itemMap.put("item", m.getItem());
itemMap.put("item2", m.getItem2().split(":",1))
);
items.add(mrphMap);
You can map each element of your Stream into a Map with map() and then collect into a List:
List<Map<String, String>> items =
file.getFileInfo()
.getSemiFileInfos()
.stream()
.map(m-> {
Map<String, String> itemMap = new HashMap<String, String>();
itemMap.put("item", m.getItem());
itemMap.put("item2", m.getItem2().split(":",1)); // this doesn't produce a String
// value so perhaps you are
// missing some additional logic
// that would extract one
// String from the String[]
return itemMap;
})
.collect(Collectors.toList());
I have a collection:
Collection<Map<String, Object>> items = new ArrayList();
Map<String, Object> item1 = new HashMap();
Map<String, Object> item2 = new HashMap();
item1.put("first_name", "john");
item1.put("last_name", "doe");
item2.put("first_name", "jane");
item2.put("last_name", "doe");
items.add(item1);
items.add(item2);
I would like to filter out using stream all maps in collection that have "first_name" set as "jane". And it should return a collection of maps (same type).
If it could be done by not using stream I don't mind but I'd prefer stream.
Not too hard to do. Just add this to your code.
Collection<Map<String, Object>> items2 = items.stream().filter(a -> !"jane".equals(a.get("first_name"))).collect(Collectors.toList());
You do need to specify what happens if the map doesn't contain the key "first_name" at all, I'm assuming that it needs to have a first_name key (that isn't jane) to be valid post-filter.
Additionally, just a heads up. Your existing code isn't using generics correctly; my IDE at least gave me a warning of unchecked casting on your first three lines. After Java 1.7 you can have it infer generic type arguments, but you still need to supply the symbols <> to make that happen. Here's a fixed version:
Collection<Map<String, Object>> items = new ArrayList<>();
Map<String, Object> item1 = new HashMap<>();
Map<String, Object> item2 = new HashMap<>();
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.
How can I put a hashmap inside an array?
HashMap<String, Object>[] config= null;
config[0] = new HashMap<String, Object>();
config[0].put("Name", "Jon");
config[0].put("valueA", 0);
config[1] = new HashMap<String, Object>();
config[1].put("valueA", 2323);
List<Map<String, String>[]> listOfMaps = new ArrayList<Map<String, String>[]>();
should be sufficient. You don't instantiate it with the () since it is an array, you need to provide it a size or a series of HashMaps as part of the array constructor.
You can find some explanation here What's the reason I can't create generic array types in Java?
I propose to use next construction:
ArrayList<HashMap<String, Object>> config= new ArrayList<HashMap<String,Object>>();
HashMap<String, Object> map;
map = new HashMap<String, Object>();
map.put("Name", "Jon");
map.put("valueA", 0);
config.add(map);
map = new HashMap<String, Object>();
map.put("valueA", 2323);
config.add(map);
Whilst you can declare an array of generics, you cannot instantiate them using the normal syntax.
Map<String,Object>[] config = new HashMap<String,Object>[10]; would give the compile error:
Cannot create a generic array of HashMap
You can however use Array.newInstance see answers to this SO question
use ArrayList instead of a simple array.
ArrayList<HashMap<String, Object>> arrayOfMap=new ArrayList<HashMap<String,Object>>();
Map<String,Object>[] config = new HashMap[10];
just leave the generic parameters away.