-1

I have always worked with PHP and I am new to Java. I have read now a few tutorials about hashmaps, arraylist etc., but I am confused now which one is the right choice for me. In PHP I used to create arrays like this:

$test['google']['color'] = "red";
$test['google']['value'] = 67;
$test['IBM']['color'] = "blue";
$test['apple']['value'] = 23;
.
.

I also want to search for the key and value. What would be a good choice in Java?

rghome
  • 8,529
  • 8
  • 43
  • 62
johnboy
  • 3
  • 2
  • Possible duplicate of [Initialising a multidimensional array in Java](http://stackoverflow.com/questions/1067073/initialising-a-multidimensional-array-in-java) – B001ᛦ Feb 09 '17 at 09:17
  • It's not a type-mixed array. What you are showing is a two-dimensional array of integers, whose keys are strings. So it's quite easy to represent in Java as a Map of Maps. But in general you should keep in mind that you are not supposed to find exact equivalents between two languages. Rather, you should find the correct data structure that helps you solve the problem properly in your current language, and doesn't force it into looking like the other language. – RealSkeptic Feb 09 '17 at 09:19
  • Sorry, my bad! I need a type-mixed array. Thanks for your advise. Thats why i am asking. I am not sure which one is the correct data structure :) – johnboy Feb 09 '17 at 09:24
  • In this case, ask yourself - why are the types mixed? In Java, you usually collect together objects that have something in common. Using a mixed-type array is usually a lazy way of representing an object. – RealSkeptic Feb 09 '17 at 09:30
  • Thx but how else should i save the data? How can i seperate "color" and "value" but still know that they are for the same company? I could create 2 Hashmaps like "company,color" and "company, value" but that seems to me pretty bad code? :) – johnboy Feb 09 '17 at 09:39
  • Ok, i finally found this: http://stackoverflow.com/questions/5809486/java-easiest-way-to-store-mixed-data-types-in-a-multidimensional-array that seems to me a good solution :) – johnboy Feb 09 '17 at 10:03

1 Answers1

0

This is not a type mixed array, but associative array with associative arrays inside. At the end you've got just one type - integer.

Java doesn't support associative arrays, however this could easily be achieved using a Map.

Map<String, Int> map = new HashMap<String, Integer>();
map.put("name", 1);
map.put("fname", 2);

map.get("name"); // returns 1

And then

Map<String, Map<String, Integer>> mapAssociated = new HashMap<String, Map<String, Integer>>;
mapAssociated,put('example', map);

But to be honest. I don't like associated arrays in php, I prefer to use objects and collections. Maybe you're thinking about the problem from wrong perspective.

Or, second solution

Object[][] data = {
    {"mykey1", "myval1"},
    {"mykey2", "myval2"},
    {new Date(), new Integer(1)},
};
Piotr Pasich
  • 2,639
  • 2
  • 12
  • 14
  • Thanks, i ll try this. But i did a mistake above. I need a type mixed array but it seems that your solution would be still good choice? – johnboy Feb 09 '17 at 09:34
  • I'm not so sure now. Probably you need to reconsider what you exactly need and maybe create new class with good properties. – Piotr Pasich Feb 09 '17 at 09:36
  • @johnboy, you can replace `Integer` with `Object` in map declaration. Then you will be allowed to put objects of any type. – ilya Feb 09 '17 at 09:39