Java Associative-Array

Java associative-array

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

Map<String, String> map = new HashMap<String, String>();
map.put("name", "demo");
map.put("fname", "fdemo");
// etc

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

Even more accurate to your example (since you can replace String with any object that meet your needs) would be to declare:

List<Map<String, String>> data = new ArrayList<>();
data.add(0, map);
data.get(0).get("name");

See the official documentation for more information

Associative arrays in Java

Arrays in PHP are not the same as arrays in Java. Here are the differences:

PHP:

PHP arrays are actually dictionaries. They store a value for each key, where a key can be an integer or a string. If you try to use something else as a key, it will be converted to either an integer or a string.

Java:

Arrays in Java

Java arrays are not associative in the same way as they are in PHP. Let's start with one-dimensional arrays in Java:

A one-dimensional array in Java has a fixed length (that cannot be changed) and each key is an integer in the range of 0 to array.length - 1. So keys, actually called indexes, are always integers. Also, in Java, if you have an array with the keys 2 and 4, you also have (at least) the keys 0, 1 and 3, because the length has to be at least 5 then.

Arrays in Java also have exactly one type and each values in the array can only be of the specified type. Neither size nor type of an array can be changed.

When you create an array in Java, you have two possibilities:

  1. explicitly specify the length when creating the array

    String[] words = new String[4];

    The variable words now holds an array of type String with the length a length of 4. The values of all indexes (0 to 3) are initially set to null.

  2. specify elements when creating the array

    String[] words = new String[] {"apple", "banana", "cranberry"};

    The variable words now holds an array of type String with a length of 3. The elements contained are as specified with the first element bound to index 0, the second element bound to index 1, and so on.

You can think of multi-dimensional arrays as of an array which holds arrays. A 2-dimensional array could look like this:

String[][] twoD = new String[][] {
{"apple", "banana", "cranberry"},
{"car", "ship", "bicycle"}
}

For this twoD[0][2] would be "cranberry" and twoD[1][1] would be "ship". But the number of dimensions of an array does not influence the fact that the keys are integers.

Maps in Java:

Even though Java has no built-in language construct for associative arrays, it offers the interface Map with various implementations, e.g. HashMap. A Map has a type of which the keys are, and a type of which the values are. You can use maps like this:

HashMap<String, String> map = new HashMap<String, String>();
map.put("car", "drive");
map.put("boat", "swim");

System.out.println("You can " + map.get("car") + " a car.");
System.out.println("And a boat can " + map.get("boat") + ".");

This will output:

You can drive a car.
And a boat can swim.

The answer:

The one-to-one way in Java

The answer to your question is that it is not really possible in a reasonable way becasue some of your values are strings, and some are integers. But this would be the most similar code to your PHP array:

//array of HashMaps which have Strings as key and value types
HashMap<String, String>[] cars = new HashMap<String, String>[2];

HashMap<String, String> first = new HashMap<String, String>();
first.put("name", "vauxhall");
first.put("doors", "5");
first.put("color", "black");

HashMap<String, String> second = new HashMap<String, String>();
second.put("name", "peogeot");
second.put("doors", "3");
second.put("color", "red");

//put those two maps into the array of maps
cars[0] = first;
cars[1] = second;

This solution is not very handy, but it is the way that comes closest to your given datastructure.

The cuter way in Java

It seems however, that each of the entries in your PHP array has exactly three properties: name, doors and color. In this case, you may want to create a class Car with these member variables, and store them in an array. This would look like this:

public class Car {

//member variables
public String name;
public int doors;
public String color;

//constructor
public Car(String name, int doors, String color) {
this.name = name;
this.doors = doors;
this.color = color;
}

}

Now, when you have the class Car, you can create an array of all your cars like this:

Car[] cars = new Car[2];
cars[0] = new Car("vauxhall", 5, "black");
cars[1] = new Car("peogeot", 3, "red");

This is the nicer way to do this in Java.

How in Java do I create an associative array structure and sorted, while keeping duplicate keys with different values pairs

You have a multitude of possibilities to realize your requirements. Here are two examples (existing in parallel within the example code):

  1. You can use a Map<Integer, List<Integer>> that holds a key and all values it has in a List
  2. you can create a POJO class that holds exactly one key and one value, but you need to make it sortable/comparable and use a suitable data structure

See this example and pay attention to the code comments:

public class StackoverflowMain {

public static void main(String[] args) {
String a = "3, 4, 1, 2, 3";
String b = "19, 24, 32, 68, 50";

// Map-approach: use a map that maps a key to a list of values
Map<Integer, List<Integer>> ab = new TreeMap<>();
// Pair-approach: make a sortable POJO that holds a key and a value only
// and create a data structure that holds them sorted
SortedSet<Pair> pairList = new TreeSet<Pair>();

// split both Strings by comma
String[] aSplit = a.split(",");
String[] bSplit = b.split(",");

// check if the length of the resulting arrays is the same
if (aSplit.length == bSplit.length) {
// if yes, go through the arrays of numbers
for (int i = 0; i < aSplit.length; i++) {
int key = Integer.parseInt(aSplit[i].trim());
int value = Integer.parseInt(bSplit[i].trim());

// in the Pair-approach, you just have to create a Pair with the value found
Pair pair = new Pair(key, value);
// and add it to the set of pairs
pairList.add(pair);

// the following check is only needed for the Map-solution
if (ab.containsKey(key)) {
// if the key is already present,
// just add the new value to its value list
ab.get(key).add(value);
// sort the value list each time a new value has been added
ab.get(key).sort(Comparator.naturalOrder());
} else {
// if the key is not present in the Map so far,
// create a new List for the value
List<Integer> valueList = new ArrayList<>();
// add the value to that list
valueList.add(value);
// and put both into the Map
ab.put(key, valueList);
}
}
} else {
System.err.println("The Strings have different amounts of elements!");
}

// print what's in the Map
System.out.println("Map-approach:");
for (int key : ab.keySet()) {
List<Integer> value = ab.get(key);
for (int val : value) {
System.out.println(key + " : " + val);
}
}

System.out.println("————————————————");

System.out.println("Pairs-approach:");
for (Pair pair : pairList) {
System.out.println(pair.key + " : " + pair.val);
}
}

/**
* This class is needed for the Pair-approach.
* It is comparable (and by that, sortable) and will be sorted by key
* and if the keys are equal, it will sort by value.
*/
static class Pair implements Comparable<Pair> {
int key;
int val;

Pair(int key, int value) {
this.key = key;
this.val = value;
}

@Override
public int compareTo(Pair otherPair) {
if (key == otherPair.key) {
if (val == otherPair.val) {
return 0;
} else if (val < otherPair.key) {
return -1;
} else {
return 1;
}
} else if (key < otherPair.key) {
return -1;
} else {
return 1;
}
}
}
}

This code produces the following output:

Map-approach:
1 : [32]
2 : [68]
3 : [19, 50]
4 : [24]
————————————————
Pairs-approach:
1 : 32
2 : 68
3 : 19
3 : 50
4 : 24

EDIT

Since the Pair-approach does not sort correctly, I came up with this Map-approach:

public class StackoverflowMain {

public static void main(String[] args) {
String a = "3, 4, 1, 3, 3, 2, 3";
String b = "5, 24, 35, 99, 32, 68, 19";

// Map-approach: use a map that maps a key to a list of values
Map<Integer, List<Integer>> ab = new TreeMap<>();

// split both Strings by comma
String[] aSplit = a.split(",");
String[] bSplit = b.split(",");

// check if the length of the resulting arrays is the same
if (aSplit.length == bSplit.length) {
// if yes, go through the arrays of numbers
for (int i = 0; i < aSplit.length; i++) {
int key = Integer.parseInt(aSplit[i].trim());
int value = Integer.parseInt(bSplit[i].trim());

// the following check is only needed for the Map-solution
if (ab.containsKey(key)) {
// if the key is already present, just add the new value to its value list
ab.get(key).add(value);
// sort the value list each time a new value has been added
ab.get(key).sort(Comparator.naturalOrder());
} else {
// if the key is not present in the Map so far, create a new List for the value
List<Integer> valueList = new ArrayList<>();
// add the value to that list
valueList.add(value);
// and put both into the Map
ab.put(key, valueList);
}
}
} else {
System.err.println("The Strings have different amounts of elements!");
}

// print what's in the Map
System.out.println("Map-approach:");
for (int key : ab.keySet()) {
List<Integer> value = ab.get(key);
for (int val : value) {
System.out.println(key + " : " + val);
}
}
}
}

It is shorter and uses a Map<Integer, List<Integer>> and sorts the List<Integer> every time a new value gets added (apart from the first value, which doesn't need a sort). That needed another loop in the output code, but you don't have to create a new class.

It produces the following output:

Map-approach:
1 : 35
2 : 68
3 : 5
3 : 19
3 : 32
3 : 99
4 : 24

Does Java support associative arrays?

I don't know a thing about C++, but you are probably looking for a Class implementing the Map interface.

Associative arrays and Java

You can store arrays as values of a HashMap:

HashMap<Integer,String[]> hm = new HashMap<Integer,String[]>();
hm.put( 1, new String[]{ "a", "b" } );

As for as having "multi-dimensional" keys, you can always wrap them together with a class. Another, albeit ugly, solution would be to have HashMaps of HashMaps.

How to use keys/values from regular array to create associative array in Java 8?

It is possible to use Stream API to convert array into map in a functional way:

  • use IntStream.range to create a stream of integers
  • collected into map using Collectors.toMap:

For a new map, filter operation can be applied to select values less than 5.

// these imports to be added
import java.util.*;
import java.util.stream.*;

int [] array = {8, 3, 2, 9, 1, 15};
Map<Integer, Integer> map = IntStream.range(0, array.length) // stream of int indexes
.boxed() // stream of Integer
.collect(Collectors.toMap(
i -> i, // use index as key
i -> array[i] // get array element as value
));

Map<Integer, Integer> mapNew = IntStream.range(0, array.length)
.boxed()
.filter(i -> array[i] < 5)
.collect(Collectors.toMap(i -> i, i -> array[i] + 5));

System.out.println("old map:" + map);
// printing each entry in new map using lambda
mapNew.forEach((k, v) -> System.out.println(k + " -> " + v));

// use common for loop by entries
System.out.println("----");
for(Map.Entry<Integer, Integer> entry : mapNew.entrySet()) {
System.out.println(entry.getKey() + " => " + entry.getValue());
}

Output:

old map:{0=8, 1=3, 2=2, 3=9, 4=1, 5=15}
1 -> 8
2 -> 7
4 -> 6
----
1 => 8
2 => 7
4 => 6

pass associative array to java in pl/sql

You cannot use a PL/SQL associative array like that - instead use a collection and modify your Java code to take two String arrays: one of keys; and one of values:

CREATE TYPE StringList IS TABLE OF VARCHAR2(255)
/

CREATE PACKAGE SOLICITUDES_HTTP AS
FUNCTION Solicitud_Http_get(
header_keys IN StringList,
header_values IN StringList,
url IN VARCHAR2
) return VARCHAR2;
END SOLICITUDES_HTTP;
/

CREATE PACKAGE BODY SOLICITUDES_HTTP AS
FUNCTION Solicitud_Http_get(
header_keys IN StringList,
header_values IN StringList,
url IN VARCHAR2
) return VARCHAR2
IS LANGUAGE JAVA
NAME 'HTTP_Request.sendGet(java.lang.String[],java.lang.String[],java.Lang.String) return java.lang.String';
END SOLICITUDES_HTTP;
/

Java: Help reading associative arrays in YAML using Jackson

Perhaps you can better understand the YAML structure if you paste your YAML into an Online YAML Parser, e.g. https://yaml-online-parser.appspot.com/

It shows:

Output


{
"innings": [
{
"1st innings": {
"deliveries": [
{
"0.1": {
"batsman": "ME Trescothick",
"bowler": "DT Johnston",
"runs": {
"batsman": 0,
"total": 0,
"extras": 0
},
"non_striker": "EC Joyce"
}
}
],
"team": "England"
}
}
]
}

As you can see, the root is a Map<String, List<Map<String, Inning>>>, where

  • Outer map has one entry with key "innings", and value:

    • Array with one element:

      • Inner map has one entry with key "1st innings", and value:

        • An Inning object

Since Inning has fields team and deliveries, that does map to the object starting at the third {.

So there are 2 strings outside that: "innings" and "1st innings". Neither of your attempts allow for two names, so they cannot possibly work.

I'll leave it to you to decide if you want classes for that extra map. I'd suggest creating a Game class with an innings field being a List<?>. Whether that ? is a Map<String, Inning> or some class with a "1st innings" field is up to you.



Related Topics



Leave a reply



Submit