2

Maybe that's simple question. But, I wonder to know why we can't populate array or collection with null values? Please look such simple example:

public static void main(String[] args) {

   Map<String, Object> map = new HashMap<>();
   map.put("first",null);
   map.put("first1",new BigDecimal(1.5));
   map.put("first2",new BigDecimal(2.5));
   map.put("first3",new BigDecimal(3.5));


   String[]array1 = new String[map.values().size()];
   Object[]array2 = new Object[map.values().size()];

   int counter = 0;

   for(Map.Entry<String,Object> entry: map.entrySet()){

       String header = entry.getKey();
       Object value = entry.getValue();

       array1[counter] = header;
       array2[counter] = value;

       counter++;

   }
}

I would be glad to listen your purposes.

10
  • 5
    What's the issue with that code? Commented Sep 30, 2013 at 16:42
  • A Map has a size method so no need for map.values().size() just use map.size(). Commented Sep 30, 2013 at 16:43
  • 3
    we can't populate array or collection with null That's not true... Commented Sep 30, 2013 at 16:43
  • Some collections allow null values, some don't. Commented Sep 30, 2013 at 16:44
  • array2 - doesn't populates with null values. Commented Sep 30, 2013 at 16:44

4 Answers 4

4

An object array is populated with null values when it's instantiated. Collections on the other hand are empty at the beginning, so there' nothing that can be "populated" in the first place - well, you could fill them with null values if you wanted, but what would be the point of that? only add elements as needed to a Collection, it makes no sense to fill it with null values (and not all collections will allow it, it depends on the type).

Sign up to request clarification or add additional context in comments.

Comments

1

You can use Arrays.fill to fill an array with values, e.g.:

String[] arr = new String[5]; 
System.out.println(Arrays.deepToString(arr));
Arrays.fill(arr,"initial value");
System.out.println(Arrays.deepToString(arr));
Arrays.fill(arr,null);
System.out.println(Arrays.deepToString(arr));

outputs:

[null, null, null, null, null]
[initial value, initial value, initial value, initial value, initial value]
[null, null, null, null, null]

Comments

0

Add the following code under your for-loop and you'll see that not only are the sizes equal to that of the map, the elements are the same too.

System.out.println("counter = " + counter);
System.out.println("array1.length = " + array1.length);
for(int i=0; i<array1.length; i++) {
   System.out.printf("- array1[%d] = %s\n", i, array1[i]);
}
System.out.println("array2.length = " + array2.length);
for(int i=0; i<array2.length; i++) {
   System.out.printf("- array2[%d] = %s\n", i, array2[i]);
}

The order will probably not be the same order you entered the values in but that's due to the nature of the HashMap. My output was:

counter = 4
array1.length = 4
- array1[0] = first3
- array1[1] = first2
- array1[2] = first
- array1[3] = first1
array2.length = 4
- array2[0] = 3.5
- array2[1] = 2.5
- array2[2] = null
- array2[3] = 1.5

2 Comments

The interesting thing is, that in IDE (Debuger) we can't see elements with null values.
Maybe some settings have to be changed in the debugger then; as you can see above the values are certainly there. Maybe there's a "don't show null values" switch somewhere?
0

When using generics, I think the best practice is to specify something more specific than Object, in order to be more typesafe. So you may want to update the HashMap to:

Map<String, BigDecimal> map = new HashMap<Sting, BigDecimal>();

Then you can create null BigDecimal objects to populate your map.

2 Comments

In my case I should use Object, bacause I need different types to store in map. Like: Date, Integer, String, BigDecimal
Consider creating a common interface for your types since I think this will make better object oriented code.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.