Results 1 to 16 of 16
Thread: getting strings from arraylist
- 05-05-2009, 09:17 PM #1
Member
- Join Date
- Jun 2008
- Posts
- 85
- Rep Power
- 0
getting strings from arraylist
I have hashmap<string,ArrayList<string>>.I am able to loop through the map
for (Iterator it = map.keySet().iterator(); it.hasNext();) {
String name = "xxxx";
if(name==(String) it.next())
{
for(Object value : (ArrayList<String>)map.get(name)) {
System.out.println(name+"-->"+value);
}
}
so what i get is
xxxx --> [val1,val2]
but i need string from the arraylist like
xxxx --> val1
xxxx --> val2
any idea of how i get this??and also i need to check if the arraylist is empty
xxxx --> []
Thanks
- 05-05-2009, 09:24 PM #2
you can't compare strings using ==. Instead try
Java Code:if(name.equalsIgnoreCase((String)it.next())){ //do stuff }Liberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.
- 05-05-2009, 10:34 PM #3
First of all you have no need for all the casting since you are already using generics within the map.
map.get(String) will return an ArrayList<String>.
Also next time use the code tags and make sure your brackets are in the correct location.
I think you want something along the lines of this:
Mr. BeansJava Code:HashMap< String, ArrayList<String> > myMap = new HashMap< String, ArrayList<String> >(); String name = "xxx"; for(Iterator<String> it = myMap.keySet().iterator(); it.hasNext();) { String next = it.next(); if(next.equalsIgnoreCase(name)) { if(myMap.get(name).size() >=0) { for(String value : myMap.get(name)) { System.out.println(name + " ---> " + value); } } else { System.out.println(name + " ---> NO_VALUES" ); } } }Last edited by Mr.Beans; 05-05-2009 at 10:37 PM. Reason: Addes a check to see if the ArrayList was empty
- 05-05-2009, 11:00 PM #4
Member
- Join Date
- Jun 2008
- Posts
- 85
- Rep Power
- 0
Hi
Thanks for your reply
The method size() is undefined for the type ObjectJava Code:if(myMap.get(name).[B]size() [/B]>=0) {
Can only iterate over an array or an instance of java.lang.IterableJava Code:for(String value : [B]myMap.get(name)) [/B]{
these are the error that i get
Thanks
- 05-05-2009, 11:10 PM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
If the compiler regards myMap.get(name) as an Object, and not something that has a size() method or implements Iterable, that's because you have the declaration of it wrong.
- 05-05-2009, 11:41 PM #6
Member
- Join Date
- Jun 2008
- Posts
- 85
- Rep Power
- 0
Thankyou..yes my declaration was wrong....But what if i have this hashmap inside a multimap something like this
now this give meJava Code:HashMap<String, ArrayList<String>> map =new HashMap<String, ArrayList<String>> (); MultiMap m = MultiValueMap.decorate(map); ArrayList arr1=new ArrayList(); arr1.add(val1); arr1.add(val2); ArrayList arr2=new ArrayList(); m.put(key1,arr1); m.put(key1,arr2); m.put(key2,arr1); m.put(key2,arr2); for (Iterator it = m.keySet().iterator(); it.hasNext();) { String name=key1; String n = (String) it.next(); //System.out.println(name); if(name.equalsIgnoreCase(n)) { for(Object value : (ArrayList)m.get(name)) { System.out.println(name"---> "+value); } }
key1-->[val1,val2]
and now how can i get individual strings from this arraylist
- 05-05-2009, 11:57 PM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
> how can i get individual strings from this arraylist
By iterating over the array list and printing the contents.
I think you will stand the best chance of getting specific help if you post a SSCCE. What you posted won't compile because of the MultiMap. (and the typo near the end).
- 05-06-2009, 02:31 AM #8
Member
- Join Date
- Jun 2008
- Posts
- 85
- Rep Power
- 0
I am sorry if it was not of SSCCE type,but didnt understand why you said it wont compile because of Multimap???
Here is the codeJava Code:import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import org.apache.commons.collections.MultiMap; import org.apache.commons.collections.map.MultiValueMap; public class Multitest { public static void main(String args[]) { HashMap<String, ArrayList<String>> map =new HashMap<String, ArrayList<String>> (); MultiMap m = MultiValueMap.decorate(map); ArrayList<String> arr1=new ArrayList<String>(); arr1.add("val1"); arr1.add("val2"); ArrayList<String> arr2=new ArrayList<String>(); m.put("key1",arr1); m.put("key1",arr2); m.put("key2",arr1); m.put("key2",arr2); for (Iterator it = m.keySet().iterator(); it.hasNext();) { String name="key1"; String n = (String) it.next(); //System.out.println(name); if(name.equalsIgnoreCase(n)) { for(Object value : (ArrayList)m.get(name)) { System.out.println(name+"---> "+value); } } } } }
and its output is
key1---> [val1, val2]
key1---> []
can you now help me on how to get the arraylist as strings??
- 05-06-2009, 03:13 AM #9
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
No need to be sorry. Just don't be surprised. People can offer the most specific help if they know what a problem is. A SSCCE is effective in describing a problem: "I need to do this, How do I do that?" and variants are not problem descriptions.I am sorry if it was not of SSCCE type,
It won't compile, because there is no MultiMap class in the "plain" JRE.but didnt understand why you said it wont compile because of Multimap???
From MultiValueMap API documentation: "A MultiValueMap decorates another map, allowing it to have more than one value for a key.
"A MultiMap is a Map with slightly different semantics. Putting a value into the map will add the value to a Collection at that key. Getting a value will return a Collection, holding all the values put to that key.
"This implementation is a decorator, allowing any Map implementation to be used as the base.
"In addition, this implementation allows the type of collection used for the values to be controlled. By default, an ArrayList is used, however a Class to instantiate may be specified, or a factory that returns a Collection instance."
In your case you put exactly two values into the map m with key "key1":
The two values are both ArrayList<String>. When you later call m.get(name) you are going to get back an ArrayList which will contain exactly the same two arrays that you associated with the key "key1". Be clear about this: the returned ArrayList is an array list of ArrayLists: it is not an array list of Strings.Java Code:m.put("key1",arr1); m.put("key1",arr2);
To print something like
where the values val1, val2 etc are the union, or concatenation, of the array lists returned by m.get(), take the array lists (you are currently calling them value and iterate over their contents.Java Code:key1--->val1 key1--->val2
I think I might have said this before, but you may not have realised that value is not merely an Object, it is an ArrayList<String>.
Checking for value being empty is not really needed here as empty array lists contribute nothing to the output. But if you need to know that use the size() method.
Duplicates could be a problem. For instance if you had
then the output would list "val1" twice. If you really want a union rather than a concatenation, form a Set<String>, add the contents of each of the lists returned by m.get() and then print the output.Java Code:ArrayList<String> arr1=new ArrayList<String>(); arr1.add("val1"); arr1.add("val2"); ArrayList<String> arr2=new ArrayList<String>(); arr2.add("val1"); m.put("key1",arr1); m.put("key1",arr2);Last edited by pbrockway2; 05-06-2009 at 03:17 AM.
- 05-06-2009, 04:02 AM #10
Member
- Join Date
- Jun 2008
- Posts
- 85
- Rep Power
- 0
since value is not merely an object,its an ArrayList<String>,i tried to iterate like this why is that i get "Can only iterate over an array or an instance of java.lang.Iterable" and value is underlined
Java Code:for(Object value : (ArrayList)m.get(name)) { for(String val:[B]value[/B]) { System.out.println(name+"---> "+val); } }
- 05-06-2009, 04:24 AM #11
Again, you're declaring it wrong and don't need casts.
Java Code:for (ArrayList<String> value : m.get(name)) { for (String val : value) { System.out.println(name + "---> " + val); } }Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-06-2009, 03:28 PM #12
Member
- Join Date
- Jun 2008
- Posts
- 85
- Rep Power
- 0
(ArrayList<String> value : m.get(name)) is again causing the same error,meaning again my declaration is wrong..
it tried parameterizing MultiMap with <String,HashMap>,it saysJava Code:MultiMap m = MultiValueMap.decorate(map);
The type MultiMap is not generic; it cannot be parameterized with arguments..
so how do i solve the problem???
- 05-06-2009, 04:12 PM #13
Member
- Join Date
- Mar 2009
- Posts
- 25
- Rep Power
- 0
You are making this a lot more difficult then what it needs to be.
Java Code:public static void main(String[] args) { HashMap<String, ArrayList<String>> myMap = new HashMap<String, ArrayList<String>>(); ArrayList<String> e1 = new ArrayList<String>(); e1.add("val1"); e1.add("val2"); ArrayList<String> e2 = new ArrayList<String>(); e2.add("val1"); e2.add("val2"); e2.add("val3"); e2.add("val3"); myMap.put("E1", e1); myMap.put("E2", e2); for (String key : myMap.keySet()) { for (String value : myMap.get(key)) { System.out.println(key + " = " + value); } } }
- 05-06-2009, 07:42 PM #14
Member
- Join Date
- Jun 2008
- Posts
- 85
- Rep Power
- 0
the problem is I need duplicate values
Java Code:ArrayList<String> e1 = new ArrayList<String>(); e1.add("val1"); e1.add("val2"); ArrayList<String> e2 = new ArrayList<String>(); e2.add("val1"); e2.add("val2"); e2.add("val3"); e2.add("val3"); myMap.put("E1", e1); myMap.put("E1", e2);is there anyway i can have the duplicate values too???Java Code:will fetch me E1 = val1 E1 = val2 E1 = val3 E1 = val3 but i need the duplicate values too.. E1 = val1 E1 = val2 E1 = val1 E1 = val2 E1 = val3 E1 = val3
- 05-06-2009, 09:57 PM #15
Member
- Join Date
- Jun 2008
- Posts
- 85
- Rep Power
- 0
Special thanks to "pbrockway2" for you patience in explaning the problem and helping me..and thanks to all of them who helped me solve the problem...
finally it was arraylist of arraylist that i was having as value,so two loops one for the outer arraylist and the other for the inner one,iterating over them helped me to get the string and had to cast to specific type since it was stored as a object.
Thanks guys...
- 05-06-2009, 10:40 PM #16
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
You're welcome - with collections going "2 deep" like that you have to work slowly and think what sort of thing is being returned by each method.
I'm not entirely sure you have to cast a variable declared as Object as something else, rather than just declaring it as what it is. But that's your call...
Similar Threads
-
Subtracting Strings
By ravian in forum New To JavaReplies: 7Last Post: 10-08-2009, 06:26 PM -
Using Merge Sort to sort an ArrayList of Strings
By coldfire in forum New To JavaReplies: 3Last Post: 03-13-2009, 01:03 AM -
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM -
i have a bunch of strings in an arraylist and...
By newtojava7 in forum New To JavaReplies: 1Last Post: 03-11-2008, 12:51 AM -
reversing Strings
By Java Tip in forum Java TipReplies: 0Last Post: 11-11-2007, 08:24 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks