Results 1 to 6 of 6

Thread: Creating HashMap, ArrayList and String

  1. #1
    Join Date
    Dec 2009
    Posts
    202

    Creating HashMap, ArrayList and String

    Hello,
    I need to link two types of ArrayList <String>. I thought to create a HashMap whose key is the first ArrayList <String> and value the ArrayList <String> corespondent.
    Here is my code
    Code:
    Public HashMapTest() {
    		klst.add("a");
    klst.add("b");
    klst.add("c");
    vallst.add(""+1);
    vallst.add(""+2);
    vallst.add(""+3);
    keyValue.could(klst, vallst);
    klst.clear();
    vallst.clear();
    klst.add("aa");
    klst.add(ba);
    klst.add("ca");
    vallst.add("1a");
    vallst.add("2a");
    vallst.add("3a");
    keyValue.could(klst, vallst);
    klst.clear();
    vallst.clear();
    ArrayList rst = <String>new ArrayList <String>();
    ArrayList <String> rsk =new ArrayList <String>();
    rsk.add("aa");
    rsk.add(ba);
    rsk.add("ca");
    rst =(ArrayList <String>)keyValue.get(rsk);	
    		for(int i =0; I <rst.size(); I + +)
    		{
    			System.out.System.out.println(rst.get(i));
    		}
    	}
    I review the console error message:
    Code:
    java.lang.NullPointerException
    At the line of the loop. Apparently, he does not like me as key to the ArrayList, since I created HashMap <String, ArrayList <String>> without any problems. Yet I have seen no indication cons in the java API. Does someone could tell me why my code does not work, or else tell me another way to combine my two types of ArrayList? Thank you very much in advance.

  2. #2
    Join Date
    Nov 2009
    Posts
    343

    Re: Creating HashMap, ArrayList and String

    Hello,
    Note: great care must be Exercised if mutable objects are used as map keys. The Behavior of a map is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is a key in the map.
    Brief editable objects should not be used as key, because it breaks the system hash based on the hashCode The hashCode exchange and we can not find the key. Map of the keys should be immutable values. If you need any more help regarding this topic then please feel free to post your queries.

  3. #3
    Join Date
    Dec 2009
    Posts
    202

    Re: Creating HashMap, ArrayList and String

    Hello,
    Already, I understand why my code does not work. I had not understood this passage this way, for me, the arguments I passed to the HashTable were immutable, since I will not return back to edit, then it is the ArrayList that must remain unchanged. Now, is there anyone how can provide a key with multiple String? I tried to concatenate all String, and find myself with a single string, and it works well, but now my ArrayList of String become too large to be concatenated into one (more than 256 characters).

  4. #4
    Join Date
    Nov 2009
    Posts
    518

    Re: Creating HashMap, ArrayList and String

    Hello,
    I do not know what is the size limit of a String, but can greatly exceed 256 characters, thankfully. Anyway, I'm not sure that a HashMap is tailored to your problem, the ArrayList and related objects are inherently variable (even if we can find immutable), and it's a little malabile use them as keys. Could not you create a class style couple, each instance would combine two <String> ArrayList, and then make you an ArrayList <Couple>? And anyway, once you had your hash ArrayList <String>, how will you assign to the ArrayList in your keys? Anyways if you find may be can you help with class Arrays, Which has many methods to find the hashcode of Tables.

  5. #5
    Join Date
    Nov 2009
    Posts
    330

    Re: Creating HashMap, ArrayList and String

    Hello,
    For the data type to use, I do not know, but for the error you had, I just explain it (well I think)
    In your code you fill your table and you put in your map:
    Code:
    kls.add("a");
    kls.add("b");
    kls.add("c");
    vallst.add(""+1);
    vallst.add(""+2);
    vallst.add(""+3);
    keyValue.could(kls, vallst);
    Just include this in your code and see if the program runs and if there is any error then post back with the error message.

  6. #6
    Join Date
    Nov 2009
    Posts
    359

    Re: Creating HashMap, ArrayList and String

    Hello,
    Just have a look at the following code. This may help you. I do not know what are your exact needs, but I think this may help you
    Code:
    Public Final class MultiStringKey {
    	
    	private Final String[] val;
    	
    	Public MultiStringKey(String ...args) {
    		this.val = Args.clone();
    		/ / We sort the table
    		/ / (Unless the order is important)
    		Arrays.fate(this.val);
    	}
    	
    	Public String[] val() {
    		return this.val.clone();
    	}
    	
    	@ Override
    	Public boolean equals(Object obj) {
    		if (obj ==this) return true;
    		if (obj instanceof MultiStringKey) {
    			return Arrays.equals(this.val, ((MultiString)obj).val);
    		}
    		return false;
    	}
    	
    	@ Override
    	Public int hashCode() {
    		return Arrays.hashCode(this.val);
    	}
    	
    	@ Override
    	Public String toString() {
    		return Arrays.function toString() {
        [native code]
    }(this.val);
    	}
    }

Similar Threads

  1. HashMap vs HashTable
    By Joyjeet in forum Software Development
    Replies: 6
    Last Post: 04-01-2011, 09:07 PM
  2. Associated values with HashMap
    By Miles Runner in forum Software Development
    Replies: 5
    Last Post: 13-02-2010, 03:50 AM
  3. Sorting a hashmap
    By Gunner 1 in forum Software Development
    Replies: 5
    Last Post: 10-02-2010, 05:06 AM
  4. Problem in creating array from string values
    By Cedric in forum Software Development
    Replies: 4
    Last Post: 19-01-2010, 05:48 PM
  5. What is HashMap and Map?
    By TAIPA in forum Software Development
    Replies: 4
    Last Post: 27-02-2009, 11:16 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,990,525.19489 seconds with 16 queries