![]() Now, when you want to put a new ArrayList object in the map, assigned to a specific FileDetail key, you assign the ArrayList object to the FileDetail's ArrayList member variable instead, and then place the FileDetail object in the map. Eg: Here My query is key1(i.e key can be 1,10,15), and output should be list1,list2,list3(list of lists). For the private map variable, it would be defined thusly: private HashMap map = new HashMap() Q2) Again I want to apply a filter condition on the key in hashmap and retrieve the corresponding list of lists. Repeat this process for all the entries in the map. ![]() If the current entry’s value is greater than the reference entry’s value, then store the current entry as the reference entry. Assuming such control, the FileDetail class would be modified to contain a member variable of type ArrayList, which for my sample code below I'll call list, with associated setter and getter methods. Method 1: Using iterative approach for each loop Approach: Iterate the map entry by entry Store the first entry in a reference variable to compare to initially. In the OP's example, this would be control of the FileDetail class and of the private map variable. My solution depends on having control of the class used for the key, and control of the map, with the ability to redefine them, which appears to be the case for the OP. The issue then, is how to get what you originally intended to be the value. You would need to iterate over every key / value pair in the map, searching for the given value and storing it into a collection. That being the case, the answer is to use the key also as the value, so that when you perform a lookup using an arbitrary key, which equals your original key but is not necessarily = to it, you get back the original key. If you want to get all the values in a map that are associated to a given key you are doing a reverse lookup. I've been using SO for years, with no ability to comment or vote on answers.)Īs has been stated already, maps are designed so that when you have a key, you look up a value. Moreover, every question I've had to date has already been asked on SO. ![]() It's tough finding questions I can answer that don't already have answers. (Please be gentle this is my first posting on SO. I thought I'd share the solution I decided upon using, which is not described in any of the existing answers and avoids iterating over all the keys in the map. This question is five years old, but I had the same question just today, and found this page.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |