Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have the following map:
Map<String, Map<String, Map<String, Map<String, List<Person>>>>>
I want to get a List<Person> that is the value in the fourth map.
How can I do it in Java 8, i.e., how can I retrieve the value of the innermost map?
You have several nested maps, so each call to get will return the respective value (which is a deeper map) and will eventually get you to the list. In order to call methods in empty maps, use Map.getOrDefault() instead of get().
import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
List myList = myMap
.getOrDefault("key1", emptyMap())
.getOrDefault("key2", emptyMap())
.getOrDefault("key3", emptyMap())
.getOrDefault("key4", emptyList());
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 months ago.
Improve this question
Why is the following code a bad practice and what is the solution for it?
TreeSet<Map.Entry<Integer, Map<String, String>>> sortedtable = new TreeSet<>(new ComparatorByDueDate());
public void sortTable(Map<Integer, Map<String, String>> table){
sortedtable.addAll(table.entrySet());
}
Update: As per Sonar, it is a bad practice. I am asking the question here as the sonar explanation seems confusing.
Java Map.Entry objects are not intended for long term storage. From the docs (emphasis mine),
A map entry (key-value pair). The Map.entrySet method returns a collection-view of the map, whose elements are of this class. The only way to obtain a reference to a map entry is from the iterator of this collection-view. These Map.Entry objects are valid only for the duration of the iteration; more formally, the behavior of a map entry is undefined if the backing map has been modified after the entry was returned by the iterator, except through the setValue operation on the map entry.
If anyone, for any reason, adds, removes, or modifies any part of the map after the fact, then your TreeSet entries now have undefined behavior. They might still be good, they might get nulled out, they might exhibit some random behavior.
If you want to store a pair of elements, then write a class that has two instance variables. If you're on a new enough Java version, then records are great for this sort of thing.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I was implementing a Java program containing hash maps, I wanted to use two different hash-maps and a boolean function. So for every key, depending on the boolean outcome, it would select one of the hash-maps. What would be some disadvantages/advantage of this?
There are two ways to do this (as far as I can think of at the moment):
Method 1: (from #WJS in the comments)
Have a HashMap with a boolean key and the corresponding HashMap as the value. Like so:
HashMap<Boolean, Map<Key, Value>> outer = new HashMap<>();
Method 2:
Since there can only be two HashMaps corresponding to your boolean true or false value, I don't think you need to have another HashMap and can simply use a boolean variable.
For Example:
// or similar depending on your implementation and needs; can be
// extended for choosing get(), set(), etc.
boolean flag;
HashMap<Key, Value> trueMap = new HashMap<>();
HashMap<Key, Value> falseMap = new HashMap<>();
HashMap<Key, Value> map = flag ? trueMap : falseMap;
There is no downside to having HashMaps chosen like this and is actually fairly common.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a class like the one below:
public class Company {
private List<Team> teams;
}
public class Team {
String name;
}
I have a list of Compony objects like this:
List<Company> companies = ...
Teams can belong to and appear in the list of more than one company.
I am looking to use Java streams to group the companies into a map, with team name as the map key. The value of each map entry is a list of companies that the team belongs to. Like this.
Map<String, List<Company>> companiesGroupedByTeam;
So the same company may appear in lists for different teams.
Does anyone know how to use java streams to do this grouping? Struggling to do the group by part.
Thanks!
something like:
Map<String, List<Company>> collect = companies.stream()
.flatMap(c -> c.getTeams().stream()
.map(t -> new AbstractMap.SimpleEntry<>(t.getName(), c)))
.collect(Collectors.groupingBy(Map.Entry::getKey,
Collectors.mapping(Map.Entry::getValue, Collectors.toList())));
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a HashMap<String, ArrayList<Object>> how do I add elements into it form a GUI and how can I retrieve them and store them in a JTable in the GUI.
Getting a class cast exception . See code snipped below
public ArrayList<Vendor> getVendors(){
ArrayList<Member> vendorList = new ArrayList(vendors.values());
return new ArrayList(vendorList);
}
This is how Vendor class is defined and vendors object used as well below
public class Vendor {
private String vendorName, vendorDescription;
vendors is a HashMap
private HashMap<String, ArrayList<Vendor>> vendors;
I have a HashMap<String, ArrayList<Object>> how do I add elements into it form a GUI
You write UI code that creates the keys and objects, and calls Map.put(...).
.... and how can I retrieve them and store them in a JTable in the GUI.
You write code that iterates the Map, and populates a TableModel.
And before you ask, I'm not going to provide you with "example" code to copy. Sorry.
Re your followup question:
private HashMap<String, ArrayList<Vendor>> vendors;
...
ArrayList<Member> vendorList = new ArrayList(vendors.values());
The type of the value returned by vendors.values() will be Collection<ArrayList<Vendor>>, but you are attempting to use it in a context that requires a Collection<? extends Member>. Obviously ArrayList<Vendor> and Member are not related types!
I don't understand why that would give you a class cast exception, but it is definitely wrong.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
How to memory map a hashmap to a file in JAVA.
HashMap<Long, ArrayList<String>> myMap = new HashMap<Long, ArrayList<String>>();
I want to memory map it to a file so that if there is any exception during the get() method of myMap, I can retrieve the data again.
Thanks
You should not need to back up the data in this case - if an exception is thrown during a call to get the data in the map should be preserved.
If the issue is that your program is crashing when an exception is thrown (which causes you to lose the data) then you should look to catch and handle the exception instead.