1

I'm searching the best way to store values in data structure where the values came from querying three columns xxxxxx GROUP BY status, height; (i.e., two columns). the result looks like.

status |  height | count |
--------------------------
InUse  |  90     |   5   |
InUSe  |  80     |   3   |
stock  |  80     |   1   |
stock  |  120    |   3   |
scrap  |  90     |   1   |

Now I wanted to store in some data structure or MultiMap or whatever the best way so that I can get the value of count.

Or

whatever the best way I can manipulate with this values.

One thing I figured is for every unique set of (status, height)--> count I'll get the value of count so how I have to store them.

Can I do something like Map< Map<someENUM, List<Long>>, Long> Will this help me? or any other way to store and use this values with less confusion.

status of type ENUM
height of type Long
count of type Long

EDIT: Thanks for your answers @Andy Turner, @OAD and @burhancerit

these answers are working well in java. But I'm sorry for not being specific about my context I use.

The Context where I'm using this is I want to populate a HTML table with this Guava Table suggested by @Andy Turner or ArrayList<myObject> suggested by @OAD and @ burhancerit in jstl/EL.

something like this

status |  height | count |                  Height | stock | Scrap | InUSe  
--------------------------                 ---------------------------------
InUse  |  90     |   5   |          HTML      90    |  0    |  1    |   5 
InUSe  |  80     |   3   |  ------> Table     80    |  1    |  0    |   3
stock  |  80     |   1   |      using EL      120   |  3    |  0    |   0
stock  |  120    |   3   |
scrap  |  90     |   1   |

So, Now which is the best way in this context and how to use them in EL.

  • 1
    create a class, put these three fields , while getting from query set these values to each instance and add that object to the collection. – Sindhoo Oad Jan 29 '16 at 12:23
  • 1
    Note that you'd probably want `Map>`, rather than `Map< Map, Long>`, since you'd need to a `Map>` to look up values with the former. The type I suggest is how [Guava HashBasedTable](http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/HashBasedTable.html) is implemented internally. – Andy Turner Jan 29 '16 at 12:32
  • Alternatively, you could use `Map, Long>`. – Andy Turner Jan 29 '16 at 12:38
  • 1
    @AndyTurner You suggested to use Map> or Map> But the thing is Map K must be unique right. In my case they are duplicated. – Durgaprasad Kusuma Jan 29 '16 at 12:59
  • @DurgaprasadKusuma the key in the first map is the status; the corresponding value is a map of height->count. So, to look up, say `(stock, 120)`, you would first get the value for `stock`, which is `{80 -> 1, 120 -> 3}`; from this you look up the value for `120`, which is `3`. – Andy Turner Jan 29 '16 at 14:40

2 Answers2

3

Since you tagged Guava: store it in a Guava Table, where the row is the status and column is the height:

Table<String, Long, Long> table;

For example:

// Construction:
ImmutableTable.Builder<String, Long, Long> builder =
    ImmutableTable.builder();
for (RowType row : rows) {
  builder.put(row.getStatus(), row.getHeight(), row.getCount());
} 
ImmutableTable<StatusType, HeightType, CountType> table = builder.build();

// Retrieval:
Long count = table.get("InUse", 90L);

To build the table you describe in your question, you can use the table structure suggested in this answer, or you can transpose the table, so that it is Table (swap the rows and columns). Then (example given as plain console output, since I am not familiar with el):

Set<String> statuses = table.columnKeySet();
System.out.print("Height");
for (String status : statuses) {
  System.out.print("|" + status);
}
System.out.println();
for (Long height : table.rowKeySet()) {
  System.out.print(height);
  for (String status : statuses) {
    Long count = Objects.firstNotNull(table.get(height, status), 0L);
    System.out.print("|" + count);
  }
  System.out.println();
}
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
2

well it depends on you how you want to store. Such as arrayLists, maps, etc..

if you have a such Object to that values below

class YourObject{
 status
 height
 count

//getters-setters etc some other stuff

}

you can use such a class to store your values in ArrayList<YourObject> list ;

add,
remove,
getItem,
size,
.
.

Also list is very flexible according to using your own data structure implementations, And another is using Queue, Linklist, all these are already implemented and ready to be used.

for small and quickly changing data i prefer Arraylist it has many methods, accessing data easily. etc..

Also please take a look at this linkhttps://stackoverflow.com/questions/322715/when-to-use-linkedlist-over-arraylist

More detailed explanation of arraylist.

This is just a simple entry explanation.Hope it helps.

Edit: OAD already mentions this .

Community
  • 1
  • 1