Results 1 to 16 of 16
  1. #1
    Ms.Ranjan is offline Member
    Join Date
    Jun 2008
    Posts
    85
    Rep Power
    0

    Default 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

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    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.

  3. #3
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    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:
    Java 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" );
            }
        }
    }
    Mr. Beans
    Last edited by Mr.Beans; 05-05-2009 at 10:37 PM. Reason: Addes a check to see if the ArrayList was empty

  4. #4
    Ms.Ranjan is offline Member
    Join Date
    Jun 2008
    Posts
    85
    Rep Power
    0

    Default

    Hi
    Thanks for your reply

    Java Code:
    if(myMap.get(name).[B]size() [/B]>=0) {
    The method size() is undefined for the type Object

    Java Code:
    for(String value : [B]myMap.get(name)) [/B]{
    Can only iterate over an array or an instance of java.lang.Iterable

    these are the error that i get

    Thanks

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

  6. #6
    Ms.Ranjan is offline Member
    Join Date
    Jun 2008
    Posts
    85
    Rep Power
    0

    Default

    Thankyou..yes my declaration was wrong....But what if i have this hashmap inside a multimap something like this

    Java 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);
    }
    }
    now this give me
    key1-->[val1,val2]

    and now how can i get individual strings from this arraylist

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    > 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).

  8. #8
    Ms.Ranjan is offline Member
    Join Date
    Jun 2008
    Posts
    85
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    >
    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).
    I am sorry if it was not of SSCCE type,but didnt understand why you said it wont compile because of Multimap???

    Java 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);
    	}
    	}
    	}
    }
    }
    Here is the code
    and its output is
    key1---> [val1, val2]
    key1---> []

    can you now help me on how to get the arraylist as strings??

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I am sorry if it was not of SSCCE type,
    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.

    but didnt understand why you said it wont compile because of Multimap???
    It won't compile, because there is no MultiMap class in the "plain" JRE.

    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":

    Java Code:
    m.put("key1",arr1);
    m.put("key1",arr2);
    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.

    To print something like

    Java Code:
    key1--->val1
    key1--->val2
    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.
    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

    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);
    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.
    Last edited by pbrockway2; 05-06-2009 at 03:17 AM.

  10. #10
    Ms.Ranjan is offline Member
    Join Date
    Jun 2008
    Posts
    85
    Rep Power
    0

    Default

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

  11. #11
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    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

  12. #12
    Ms.Ranjan is offline Member
    Join Date
    Jun 2008
    Posts
    85
    Rep Power
    0

    Default

    (ArrayList<String> value : m.get(name)) is again causing the same error,meaning again my declaration is wrong..

    Java Code:
    MultiMap m = MultiValueMap.decorate(map);
    it tried parameterizing MultiMap with <String,HashMap>,it says
    The type MultiMap is not generic; it cannot be parameterized with arguments..
    so how do i solve the problem???

  13. #13
    Join Date
    Mar 2009
    Posts
    25
    Rep Power
    0

    Default

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

  14. #14
    Ms.Ranjan is offline Member
    Join Date
    Jun 2008
    Posts
    85
    Rep Power
    0

    Default

    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);
    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
    is there anyway i can have the duplicate values too???

  15. #15
    Ms.Ranjan is offline Member
    Join Date
    Jun 2008
    Posts
    85
    Rep Power
    0

    Default

    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...

  16. #16
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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

  1. Subtracting Strings
    By ravian in forum New To Java
    Replies: 7
    Last Post: 10-08-2009, 06:26 PM
  2. Using Merge Sort to sort an ArrayList of Strings
    By coldfire in forum New To Java
    Replies: 3
    Last Post: 03-13-2009, 01:03 AM
  3. Replies: 2
    Last Post: 04-21-2008, 11:43 AM
  4. i have a bunch of strings in an arraylist and...
    By newtojava7 in forum New To Java
    Replies: 1
    Last Post: 03-11-2008, 12:51 AM
  5. reversing Strings
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-11-2007, 08:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •