Okay, so I'm fairly new to programming so apologies if this problem is really simple, I've created an ArrayList inside my Hash Map so that I can add more than one value into my hash map.
My code looks fine (to me) but theres an error on this line "mymap.add(module, new ArrayList>());" saying ( or [ expected.
Wasted way too much time trying to figure this out so thought I'd post here. Thanks in advance for any help. Also: yes I need to do it this way and no I can't use guava MultiMap.
public class hashArray {
HashMap<String, ArrayList<Integer>> mymap = new HashMap<String, ArrayList<Integer>>();
public hashArray() {}
public void addEntryHR( String module, Integer result ) {
mymap.add(module, new ArrayList<Integer>>());
There is a typo and a bug in your line:
// The typo is right here v
mymap.add(mod, new ArrayList<Integer>>());
Remove one of the > and change add to put:
mymap.put(mod, new ArrayList<Integer>());
The error you get, is about the typo. Fixing that typo will give you an error about add to be an unknown method.
You need to first get the list out from map object like below-
ArrayList<Integer> list = mymap.get(mod);
if (list==null){
//then create new ArrayList object and assign to list
list=new ArrayList<Integer>();
}
list.add(number); //adding new number to the list
mymap.put(mod,list); //storing in map
The problems
You create a new ArrayList in new ArrayList<Integer>>() (with contains a syntax error as well - a > too many), but never add number to it.
You are calling add on a HashMap, which doesn't have this method. For maps, it is called put.
Proposed solution
Please see the code below
Map<String, List<Integer>> myMap = new HashMap<>();
public void addEntryHR(String mod, Integer number) {
List<Integer> numbers = new ArrayList<>();
numbers.add(number);
myMap.put(mod, numbers);
}
Other remarks
Use interfaces
It is advised to use the interfaces of the collections rather than the implementations. This means Map<String, String> myMap instead of HashMap<String, String> myMap and List<Integer> instead of ArrayList<Integer>.
Why do this? Because this allows you to be flexible in the collections you use and reduce maintenance effort. Say you change your mind and want to use LinkedList instead of ArrayList, then you just have to change one new ArrayList<>() to new LinkedList<>() instead of all the ArrayList variables used throughout the code.
Related
I tried and search answer for this question as this seems trivial but I really couldn't find it. (Maybe I don't know how to ask the right question).
Is there a way to do the following?
private void createAList(String a) {
List<String> a = new ArrayList<String>();
}
Because I want to have a list created with every change of data and I want a specific name for it that will be provided in String a.
I hope I've explained it clearly enough. :)
Edit: Ok, I see I didn't explained it clear enough.......
Although I don't know how can I make it even simpler it is quite straight forward.
I want to create this method and then use it like that:
String name = ben;
createAList(name);
And it will create:
List<String> ben = new ArrayList<String>();
You can't change the variable's name.
You could create a Hashmap:
HashMap <String, ArrayList> valuesMap;
And then
private void createAList(String a) {
List<String> valueList = new ArrayList<String>();
valuesMap.put(a, valueList);
}
Then you can use the list search ing the variable "a" value in HashMap's keys
I need to create a list with values of type - (long,String)
like -
ArrayList a = new ArrayList();
a.add(1L,branchName);
How can I do this because if I use list It will accept only int,String.
You should note that ArrayList's add(int,String) adds the String element in the given int index (if the index is valid). The int parameter is not part of the contents of the ArrayList.
Perhaps an ArrayList is not the correct choice for you. If you wish to map Long keys to String values, use Map<Long,String>.
Map<Long,String> a = new HashMap<> ();
a.put(1L,branchName);
You can define a custom class, e.g.
class IndexAndBranchName {
long index;
String branchName;
}
and then add instances of this to the ArrayList:
ArrayList<IndexAndBranchName> a = new ArrayList<>();
a.add(new IndexAndBranchName(index, branchName));
Whether you use this approach or something like Eran's depends upon what you need to use the list for subsequently:
If you want to look "branches" up by index, use a Map; however, you can only store a single value per key; you could use a Guava Multimap or similar if you want multiple values per key.
If you simply want all of the index/branch name pairs, you can use this approach.
You can use the below code for your question.
HashMap is also a better option , but if you want only ArrayList then use it.
List<Map<Object, Object>> mylist = new ArrayList<Map<Object, Object>>();
Map map = new HashMap<>();
map.put(1L, "BranchName");
mylist.add(map);
I want to add a trail to a player on a grid, similar to that of the game "Snake." However, when I go to make an array list, I get an error. Here is the code for creating it.
ArrayList<Integer, Integer> trail = new ArrayList<Integer, Integer>();
I get the error "Incorrect number of arguments."
How am I supposed to make this to keep track of which coordinates have been gone over by the player?
Make a class Pair which will look like something like this:
class Pair {
public int x, y;
}
or you can use predefined java.awt.Point class. Then in your code use
ArrayList<Pair>
instead of
ArrayList<Integer, Integer>.
Array list only takes one arguement.
You can refer to this link here.
Solution to your problem is you can use hash map or double dimension array as per your requirement.
ArrayList takes only one argument, like this:
ArrayList<Integer> trail = new ArrayList<Integer>();
If you need to store a key and a value, use HashMap
HashMap<Integer, Integer> trail = new HashMap<Integer, Integer>();
You can also make a Arraylist of integer array like
new Arraylist;
I'm facing some known solvable problem but struck out here. In my code, the values are getting stored in ArrayList on the first for loop execution. However, on the 2nd loop and further, the values are overwritten by the final values in the ArrayList. Finally, the lastly entered values are alone getting stored with the number of times of size of the list.
lstall = (ListView)findViewById(R.id.lvall);
sampleArrayList = new ArrayList<HashMap<String, String>>();
ListAdapter sampleListAdapter;
dh = new DBHelper(this);
HashMap<String, String> sampleObjectMap;
List<String> lvall = dh.selectAll();
sampleObjectMap= new HashMap<String, String>();
for(int i=0;i<lvall.size();i++)
{
for (#SuppressWarnings("unused") String sampleObj : lvall)
{
sampleObjectMap.put("title", dh.val1(i));
sampleObjectMap.put("person", dh.pers(i));
sampleObjectMap.put("priorty", setpriority(String.valueOf(dh.prioirty(i))));
sampleObjectMap.put("dat", getDate(Long.valueOf(dh.time(i)),"dd/MM/yyyy"));
}
sampleArrayList.add(sampleObjectMap);
}
I need to store all the values in a arraylist and display within a list view. Any help is highly appreciated and thanks in advance.
You're creating a single HashMap<String, String> and repeatedly overwriting the entries within it.
In fact, you're doing that twice as you have a nested for loop for no obvious reason. I believe you want:
for (int i = 0; i < lvall.size(); i++)
{
HashMap<String, String> sampleObjectMap = new HashMap<String, String>();
sampleObjectMap.put("title", dh.val1(i));
sampleObjectMap.put("person", dh.pers(i));
sampleObjectMap.put("priorty", setpriority(String.valueOf(dh.prioirty(i))));
sampleObjectMap.put("dat", getDate(Long.valueOf(dh.time(i)),"dd/MM/yyyy"));
sampleArrayList.add(sampleObjectMap);
}
It also seems odd to use a list's size but not actually use the values within the list... you might want to consider restructuring your code...
Try it out:
for(int i=0;i<lvall.size();i++)
{
sampleObjectMap= new HashMap<String, String>();
sampleObjectMap.put("title", dh.val1(i));
sampleObjectMap.put("person", dh.pers(i));
sampleObjectMap.put("priorty", setpriority(String.valueOf(dh.prioirty(i))));
sampleObjectMap.put("dat", getDate(Long.valueOf(dh.time(i)),"dd/MM/yyyy"));
sampleArrayList.add(sampleObjectMap);
}
All the elements in the list have the reference of the same Map instance and you are overriding the same Map entries for each iteration.
for(int i=0;i<lvall.size();i++) {
// move it inside the loop
sampleObjectMap= new HashMap<String, String>();
But i don't think that you need a Map here. Just create a class with all the required fields as member variables.
I have not tested it, but try to create new Map
sampleObjectMap = new HashMap<String, String>();
in your inner loop, before you do put. Test it. Later try to use 1 map object and calling clear() after the map is recorded in the sampleArrayList.
For a project, I have to create a map where the keys are Lists of String and the value is two integers. So, I made it like this:
private Map<LinkedList<String>, int[]> playerProfile;
private List<String> previousChoices;
Then later I have to iterate through the map and write all the key-value combinations to a data file. So I am setting up an iterator like this:
Set<Entry<LinkedList<String>, int[]>> profileSet;
profileSet = playerProfile.entrySet();
//iterate through the Set
List<String> curList; //current list of choices
int[] curHeadTail; //current list of heads/tails
Entry<LinkedList<String>, int[]> curEntry;
Iterator<Entry<LinkedList<String>, int[]>> i =
profileSet.iterator();
What I want to know is: is there a simpler way to do this that takes less lines of code? And at one point I have triply-nested generics. Is that too much?
Sure, you can use a for-each loop:
for(Entry<LinkedList<String>, int[]> curEntry : playerProfile.entrySet()){
// now you can use curEntry
}
And no, nested generics here (and in general) are not a problem.