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 have a HashMap HashMap<Integer, ArrayList<Bundle>> hashMap I am able to put values in the HashMap . And the hashmap is passed to the next class . Now how to get the ArrayList of Bundle from HashMap. Any how I am using Iterator Set . Still I am not able to list the values of HashMap which is an arraylist. This is actually am Message picker . And I have multiselection mode on using callback for listview. When I click on a sms list , I need to get some details and store it in a hashmap . There might be chance that a sms can have multiple recepinets. The key will be position . I need to take all the values of the HashMap that is ArrayList into one single ArrayList. Say int the 0th position we have [0,[A,B]] ,[1,[C,D]] , i want to create a new arraylist and store A , B, C, D in it.
Here you have a complete example:
public class Test {
private final String value;
public Test(String value) {
this.value = value;
}
public static void main(String[] args) {
HashMap<Integer, ArrayList<Test>> example = new HashMap<Integer, ArrayList<Test>>();
ArrayList<Test> test1 = new ArrayList<Test>();
test1.add(new Test("HELLO"));
ArrayList<Test> test2 = new ArrayList<Test>();
test2.add(new Test("HELLO2"));
example.put(1, test1);
example.put(2, test2);
// We get the second arraylist
// Where 2 is the Integer we added in example.put(2, test2);
ArrayList<Test> testExtracted = example.get(2);
System.out.println(testExtracted.get(0).value); // Prints HELLO2
}
}
you can try like that
ArrayList<Bundle> value = hashMap.get(yourkey);
Related
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 am trying to search for a specific value in a HashMap, using an iterator, currently I have this method. I am very new to Java so your help would be greatly appreciated. helper.readAMap is hashmap which stores responses, which are generated when a user types in a certain word.
public String generateResponse(String words)
{
HashMap<String, String> map = new HashMap();
map = helper.readAMap("replies.txt");
Iterator<String> it = map.keySet();
while(it.hasNext()) {
String word = it.next();
String response = map.get(word);
if(response != null) {
return response;
}
}
return pickDefaultResponse();
}
Here:
if(key.equals(words)) {
You compare a String to a HashSet of Strings. That is like comparing an apple to a pear; it will always be false.
So you either want a single String as argument to your method, or you want to generate responses to all of the words.
I expect you want to do this:
if(words.contains(key)) { // Your input contains the key
return map.get(key); // Retrieve the response to the key from the map
}
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 was trying to create an ArrayList of ArrayLits. Then, whenever I added an ArrayList to my initial ArrayList, I get My 2nd ArrayList in the initial ArrayList copied to the first ArrayList in the initial ArrayList. Here is my Code. Thank you for any help.
ArrayList<ArrayList> msebi = new ArrayList<ArrayList>();
ArrayList<Pair> temp = new ArrayList<Pair>();
Stack<ArrayList> arrayListStack = new Stack<ArrayList>();
Pair pair = null;
for(int i=1;i<finalList.size()-1;i++){
pair = finalList.get(i);
if(finalList.get(i+1).getDepth() > finalList.get(i).getDepth()){
temp.add(pair);
if(i == finalList.size()-2){
temp.add(finalList.get(i+1));
msebi.add(temp);
}
else {
temp.add(pair);
msebi.add(temp);
temp.clear();
}
}
Kindly Note that I have created another Class that is Named Pair. This Pair consist of a String and depth, and thus we have getDepth(); Also, I have in the main class ArrayList finalList = new ArrayList();
Thus, Whenever I am trying to copy the content of the ArrayLists within the msebi ArrayList, I got the values of the second arraylist the same as the first one.
When you add your ArrayList called temp to your outer ArrayList called msebi, msebi contains a reference to temp, not a copy. Then you immediately clear() temp and build it up again. You have multiple references to the same temp list in msebi.
Create a new ArrayList instead of clearing the old one. This way, when a list is added, it's left alone, because you will be operating on another list object.
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 8 years ago.
Improve this question
I've tried to search the WWW but failed to find an answer. Couldn't find one here either.
Here's my question:
How do I get a specific name(element?) from a Customer in an ArrayList?
I'm imagining it looks something like this:
ArrayList<Customer> list = new ArrayList();
String name = list.get(2) // which would return the Customer at 2's place.
But what if I want to search for a customer by name, lets say a customer named Alex? How do I do that?
Bonus question: How do I then delete that customer?
As others have said this isn't all that efficient and a HashMap will give you fast lookup. But if you must iterate over the list you would do it like this:
String targetName = "Jane";
Customer result = null;
for (Customer c : list) {
if (targetName.equals(c.getName())) {
result = c;
break;
}
}
If you need to remove an item from a list while iterating over it you need to use an iterator.
String targetName = "Jane";
List<Customer> list = new ArrayList<Customer>();
Iterator<Customer> iter = list.iterator();
while (iter.hasNext()) {
Customer c = iter.next();
if (targetName.equals(c.getName())) {
iter.remove();
break;
}
}
Your are going to have to iterate through your array using something like this in a function call.
void int HasName(string name){
for(int i=0; i < list.size(); i++) {
String s = list.get(i).getName();
//search the string
if(name.equals(s)) {
return i
}
}
return -1
}
If you really need to search by name consider looking into HashMap.
With an ArrayList, you have to loop... If you can, use a Map (HashMap, TreeMap) to quickly find an element.
This works if you always seek by name, for example. (use name as key of the map)
There isn't a way to explicitly do what you want, unless you want to iterate through the whole collection, comparing the desired name to the current one. If you want this type of functionality, you could try a Map such as HashMap.
Implement equals and hashcode for Customer object. Use customer name attribute for this.
Use ArrayList.indexof to find the index of the element. use the remove method in Arraylist to remove the object by index.
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 8 years ago.
Improve this question
I want to store two integer values using an integer key, also please tell how to store and retrieve data using the key.
i already tried this :
static HashMap<Integer, User> nodes = new HashMap<Integer, User>();
public User( int b, int c) {
xco = b;
yco = c;
}
and i stored values into it as
User.nodes.put(User.key,new User((int)upx,(int)upy));
but I can't retrieve the data using the key
First of all your question isn't much describe your requirement and what have you tried so far.
But assuming your question you can try as follows.
you can create a Map<Key,Value> Integer value as a Key and Integer List<Integer> as Value
Map<Integer,List<Integer>> map=new HashMap<>();
List<Integer> list=new ArrayList<>();
list.add(1);
list.add(2);
map.put(1,list);// key is 1, values 1 and 2
you can retrieve values as follows
map.get(1) // will return list which contains values 1 and 2
Edit:
HashMap<Integer, User> nodes = new HashMap<Integer, User>();
User user=new User(1,2);// creating a user
nodes.put(1,user);
Now you can get values
nodes.get(1)// will return user
If I understand the question.
If your values are observably large, you can use the method: v1 * 10^n + v2
where v2 < 10^n
use SparseIntArray it is efficient way to store int key value pair
SparseIntArray sparseIntArray new SparseIntArray();
sparseIntArray.put(int_key,int_value);
while for geting value
sparseIntArray.get(int_key);
use The Map Interface
A map has the form Map <K, V> where:
K: specifies the type of keys maintained in this map.*
V: defines the type of mapped values.
you need to have an object for your 2 integer values:
class Object{
Integer int1;
Integer int2;
}
Map<Integer,Object> map;
To get value
Object result = map.get(1) // will return the first object which contain 2 integers
You can get your 2 integers using Object.int1 and Object.int2
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'm working on a game and I'm implementing objects where you can define in "TiledMap editor" what items a certain object holds.
So I've got to an idea where I can enter the Item ID's right there like {22:4, id:amount}. When I parse the map, I retrieve that array as a string, is there a way to convert it to an array?
Thanks in advance!
Firstly, you probably want a Map, not an array or a List.
Map<String,String> processParams(String list) {
Map<String,String> = new HashMap<String,String>();
int openBracket = list.indexOf("{");
int closeBracket = list.lastIndexOf("}");
String params = list.substring(openBracket+1,closeBracket);
String paramList = params.split(",");
for(String param: paramList) {
String pData = param.trim().split(":");
map.put(param[0].trim(),param[1].trim());
}
return map;
}
processParams("{22:4, id:amount}");
Of course, it's actually a JSON-like structure so there's probably pre-existing parsers.