Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Associativity array in Java

I receive a List<org.apache.avro.generic.GenericRecord> with the data contents as shown below (JSON notation used for clarity). How can I best hold these record types using Java?

Record 1:

   [
    {
      "serial_no" : "x",
      "data1" : "d"
    },
    {
     "serial_no" : "y",
     "data2" : "d2"
    },
    ............................MANY MORE
   ]

Record 2:

   [
    {
      "id":"x",
      "type":"A"
    },
    {
      "id" : "x",
      "type" : "B"
    },
    {
      "id" : "y",
      "type" : "A",
    },
    { 
      "id" : "y",
      "type" : "B"
    } 
   ]

As you see here, each serial number has two records in record2. serial_no in record1 is same as id in record2.

My Goal is: Fatsest way to find these two records.

Solution I think:

Create a map like

      map.put("x", [map.put("A",List), map.put("B",List)]);

But I feel like, its a complex structure. Because map contains list of maps[each map is Map<String,List<Map<String,String>>>].

Any suggestions?

EDIT

Each entries in records are avro GenericRecord

like image 499
Gibbs Avatar asked Aug 10 '15 10:08

Gibbs


People also ask

Does Java have associative array?

Java does not support associative arrays, which are arrays that reference values instead of indexes. Instead, we have the Map class that stores the references and values as objects. There is a key and a value for each entry.

What is associative array with example?

Associative arrays are used to store key value pairs. For example, to store the marks of different subject of a student in an array, a numerically indexed array would not be the best choice.

Is Hashmap an associative array?

A dictionary (also known as a map, hashmap or associative array) is a set of key/value pairs.

What is the difference between array and associative array?

The index data type for a simple array must be an integer value. The index type for an associative array can be one of a set of supported data types. The index values in a simple array must be a contiguous set of integer values. In an associative array the index values can be sparse.


2 Answers

It looks as if you are trying to parse JSON using Java. Why not use a specific library for that? Like the basic http://www.json.org/java/ or Google's https://github.com/google/gson

Otherwise, I do not think that the complex structure you are proposing is especially slow. You might want to design your own object class to hold the data if you think it is more efficient or easier to get to the data.


EDIT

Based on your question I assumed JSON was the format you received it in, sorry.

I would just create a wrapper for GenericRecord, or subclass it. Then add the methods that you need to extract the data, or make it Comparable for sorting.

Something along the lines of

public class MyRecord extends GenericRecord implements Comparable<MyRecord>
{
    // Determine the type
    public int getType()
    {
        if ( this.get( "id") != null )
            return 2;
        return 1;
    }
    // Add methods that allow you to retrieve the serial field from any of the two record types
    public String getId()
    {
        if ( this.get( "id") != null )
            return (String)this.get("id");
        return (String)this.get("serial_no");
    }

    // add comparator methods that will allow you to sort the list, compare based on Id, etc
    @Override
    public int compareTo(MyRecord another) 
    {
        // Just a simple example
        return this.getId().compareTo( another.getId() );
    }
}
like image 128
mvreijn Avatar answered Sep 23 '22 00:09

mvreijn


Define classes for repeated entries:

class SerialNoData {
    String serialNo;
    Object data;
}

and

class IdType {
    String id;
    String type;
}

; once parsed put the instances into arrays or Lists to get the desired format.

like image 42
Binkan Salaryman Avatar answered Sep 22 '22 00:09

Binkan Salaryman