How to memory map a hashmap to a file [closed] - java

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.

Related

How to get a list from nested maps [closed]

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());

Why should initialize list in Java [closed]

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 6 years ago.
Improve this question
If I use the below code
List<String> listOfStrings=new ArrayList<>();
listOfStrings.add("first string");
or the following code
List<String> listOfStrings;
listOfStrings.add("first string");
to create a Java list, both the codes get compiled successfully and give same output on iterating the list. So what is the relevance of initializing the list
If listOfStrings is a local variable, the second example won't compile: you have to definitely assign a value to a variable before you can use it.
It listOfStrings is a member or static variable, the second example would yield a NullPointerException because you're invoking the add method on a null reference.
In the first example, you are not initialising the list, you are instantiating a class or creating the new object, the list. In the second example you will receive a runtime error, because the variable listOfStrings is null.

Convert a List to List<Iterator<>> in java [closed]

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 7 years ago.
Improve this question
I have a list<something> which passed to a method but the method expects like List<Iterator<something>>.
How could i achieve this?
Thanks
Your question is like asking
My client wants apples but I only have oranges. How can I turn my oranges into apples?
If the method requires a list of Iterator then give it a list of iterators! Don't give it a list of objects!
If you have this problem, you must be using the method in the wrong way. But you didn't tell me what the method is. So I can't tell you how to use the method in the correct way.
But anyway, if you really want to do this, here's how
//Assume your list is called "list"
List<Iterator> newList = new ArrayList<>();
for (Object item : list) {
newList.add((Iterator)item);
}
Note: this might result in ClassCastException.

HashMap<String, ArrayList<Objects>> [closed]

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.

Iterating over ConcurrentHashMap [closed]

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
I referred many links regarding Iterator of ConcurrentHashMap,
like Is iterating ConcurrentHashMap values thread safe? or any other on Google, even java doc,yet i am not much getting what kind of behavior i might face while iterating the Concurrenthashmap and simultaneously modifying it
Read the JavaDoc for ConcurrentHashMap.values():
The view's iterator [..] guarantees to traverse elements as they existed upon construction of the iterator, and may (but is not guaranteed to) reflect any modifications subsequent to construction.
If you're interested in the contract of the other iterators, they have documentation as well.
Yes, unlike regular maps you can iterate over ConcurrentHashMap and remove elements using Map.remove(key). Try this test
ConcurrentHashMap<Integer, Integer> m = new ConcurrentHashMap<>();
m.put(1, 1);
m.put(2, 2);
m.put(3, 3);
for (int i : m.keySet()) {
if (i == 2) {
m.remove(i);
}
}
System.out.println(m);
it prints
{1=1, 3=3}

Categories