34

i'm new to Java. How can i store an array of integers values in a HashMap, after that i write this HashMap in a txt file but this isn't important at the moment. I can store single fields but not an array. Any ideas ?

public void salveazaObiectulCreat(String caleSpreFisier) {

    HashMap map = new HashMap();

    map.put ("Autorul",numelePrenumeleAutorului);
    map.put ("Denumirea cartii",denumireaCartii);
    map.put ("Culoarea cartii",culoareaCartii);
    map.put ("Genul cartii",gen);
    map.put ("Limba",limba);
    map.put ("Numarul de copii",numarulDeCopii);
    map.put ("Numarul de pagini",numarulDePagini);
    map.put ("Pretul cartii",pretulCartii);

  try  {

      File file = new File(caleSpreFisier);  

      FileOutputStream f = new FileOutputStream(file);  

      ObjectOutputStream s = new ObjectOutputStream(f);          

      s.writeObject(map);

      s.close();

       } catch(Exception e){

           System.out.println("An exception has occured");     
    }   
}
6
  • 1
    An array of what integers? Are you trying to put multiple arrays into the HashMap? Commented Sep 29, 2011 at 19:55
  • There should be no difference between arrays and any other objects. What did you try? Commented Sep 29, 2011 at 19:56
  • those values in map.put are Strings, now i want to store an array of int values(some digits) Commented Sep 29, 2011 at 19:58
  • We will need to see the rest of the code, for example what numelePrenumeleAutorului is. Commented Sep 29, 2011 at 19:58
  • @biziclop i tried with a for statments , somethig like this : for (int i=0;i<numarulDeCopii;i++) { map.put(i, coeficientUzura[i]); } Commented Sep 29, 2011 at 19:59

7 Answers 7

45
HashMap<String, List<Integer>> map = new HashMap<String, List<Integer>>();
HashMap<String, int[]> map = new HashMap<String, int[]>();

pick one, for example

HashMap<String, List<Integer>> map = new HashMap<String, List<Integer>>();
map.put("Something", new ArrayList<Integer>());
for (int i=0;i<numarulDeCopii; i++) {
    map.get("Something").add(coeficientUzura[i]); 
}

or just

HashMap<String, int[]> map = new HashMap<String, int[]>();
map.put("Something", coeficientUzura);
Sign up to request clarification or add additional context in comments.

1 Comment

if you get the value in recyclerview as like subject .getsubject(); in adapter class then how it should be implemented
36

Not sure of the exact question but is this what you are looking for?

public class TestRun
{
     public static void main(String [] args)
     {
        Map<String, Integer[]> prices = new HashMap<String, Integer[]>();

        prices.put("milk", new Integer[] {1, 3, 2});
        prices.put("eggs", new Integer[] {1, 1, 2});
     }
}

Comments

7

Yes, the Map interface will allow you to store Arrays as values. Here's a very simple example:

int[] val = {1, 2, 3};
Map<String, int[]> map = new HashMap<String, int[]>();
map.put("KEY1", val);

Also, depending on your use case you may want to look at the Multimap support offered by guava.

Comments

0

If you want to store multiple values for a key (if I understand you correctly), you could try a MultiHashMap (available in various libraries, not only commons-collections).

Comments

0

Your life will be much easier if you can save a List as the value instead of an array in that Map.

Comments

0

You can store objects in a HashMap.

HashMap<String, Object> map = new HashMap<String, Object>();

You'll just need to cast it back out correctly.

Comments

0

The issue with your code lies in how you are using arrays (int[]) as keys in a HashMap. In Java, arrays do not override the equals() and hashCode() methods from Object. As a result, when you use an array as a key in a HashMap, the default implementation of hashCode() is based on the memory address, not the content of the array. Therefore, two arrays with the same elements are treated as different keys.

To resolve this issue, you should use a key type that properly implements equals() and hashCode(). A good alternative is to use a List (e.g., ArrayList) or a String representation of the array as the key.

 Map<String, Integer> map = new HashMap<>();
    String dummy ;
    if (map.containsKey(dummy)) {
       map.put(dummy, map.get(dummy) + 1);
     }

only through String or arraylist you can compare and use contains method

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.