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

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.

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

How can I store classes in Array/Arraylists/Lists whatever? [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 2 years ago.
Improve this question
Is it in Java possible to store classes...
Class1 class1 = new Class1();
...in Arrays/ArrayLists, somehow like this:
ArrayList<> classArray = new ArrayList();
classArray.add(class1);
Obviously this doesn't work, but how could it work?
Of course you can. You just specify the type of objects you want to store when you create the array list:
ArrayList<Class1> classArray = ArrayList<Class1>();
classArray.add(class1);
If the class can only be Class1
ArrayList<Class1> list = new ArrayList<Class1>();
Above code won't allow any other class objects it can only store objects of type Class1
If you want to store any class object
ArrayList<Object> list = new ArrayList<Object>();
Object is a parent class of everything in java which is the reason why it can store object of any class

Hashmaps - using booleans [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 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.

Is this a reasonable approach for my Java web app? [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 9 years ago.
Improve this question
Just wondering if there is a better approach to building to the web app that I'm creating? I'm not going to show a lot of code, I'm more interested in following best practices:
Index.html contains a static form
User fills out the form and the POST request is handled by a Java Servlet
The servlet contains 3 methods:
doPost(HttpRequest,
HttpResponse)
doGet(HttpRequest, HttpResponse)
processRequest(Map map)
doPost() constructs a hashmap and assigns all of the variables from the form to it in key:value format. Then it calls the method processRequest(Map map)
processRequest(Map map) has one function: to create a new instance of class formParser with the map variable: i.e. formParser parser = new formParser(map);
The class formParser has a constructor and a method:
the constructor initializes a new HashMap which clones the original and a new LinkedList which is to store the values and calls the method with the map parameter
Map<String, String> paramMap = new HashMap<String, String>(map);
List<String> paramList = getParams(paramMap);
The method public LinkedList<String> getParams(Map paramMap) then checks all of the key:value pairs in the map and only takes those that have no null values
I then use the valid key:value pairs to write certain XML snippets to a pre-existing XML skeleton, which is sent to an external REST-API. I have yet to write this code.
EDIT: I should add that I'm new to Java Web programming, and this is why I'm only trying to validate the efficacy of my approach to the program, rather than the code itself. That will come later.
I think that step 6 can be improved a little by not creating the LinkedList. If I understand correctly the LinkedList is used to remove NULL values. This can be achieved by iterating over the keys of the map, retrieving their value and removing any keys from the map that have NULL values.
Map map = new HashMap<String, String>();
map.put("name1","somevalue");
map.put("name2",null);
for(String s : map.keySet()){
String value = map.get(s);
if ( s == null){
map.remove(s);
}
}
I don't know what you exactly mean by "efficiency", but if you are talking about performance, I don't see anything crazy being done in the steps you describe, and IMO that's enough.
Go with the simplest, most usual design and you can improve performance if there is a problem. In my experience, 99.999% of performance problems in web apps come from doing crazy stuff (i.e. unusual, complex approaches).
Since you are new to web programming you should not consider performance problems.
Just focus on working code.

Passing an Object to a method and returning a list of Objects any example [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
Friends
Im posting my question again as previous question was unclear .
I want to pass an object of an class who's member variables are string , Listof A String and a List of an class member variable to a method that copies the contents of above class objects and returns a Lsit of class object which can be used by an java method.
Any example will be nice
Now I have a
Where is the class for CallFlowResource??
A simple way would be for you to create a list and add the values as you want them to be outputted.
It you would look like this:
public List<CallFlowResource> getCallFlow(CallFlowObject obj)
{
List<CallFlowResource> callFlowRes = new ArrayList<>();
for(int i = 0; i<size; i++)
{
callFlowRes.add(obj.GetterMethodForCFR(i));
}
return callFlowRes;
}

Categories