0

I have several lists of entity names which compose a dataset that is organized, for example:

  • ENT_01
  • ENT_02
  • ...
  • ENT_nn

Being any number of lists of different entity names (as Strings) always numbered from 01 to nn.

What I intend to do is read them, and create some data structure that utilizes the entity root name (ex. ENT) and associate each instance of it (ex. ENT_01, ENT_02) to itself. Meaning that when I call ENT I could use it (or its position in a List, ArrayList, etc) as a parameter to retriveve de whole numbered structure. Problem being, I also need a way to easily access each numbered entity instances.

I thought about and tried based on this question to implement an "ArrayList of ArrayLists", with the first being the rootname and the second containing the instances, but it looked inefficient and improvised way to do it.

Is there any structure or class, maybe under Collections and/or Sets, in Java which would do it in a more elegant and effcient way?

1 Answers1

0

It's hard to tell without knowing how exactly your data looks like and it might be a little too much for SO.

However, assuming "ENT" would be some arbitrary key (I'll assume it is a string) you could use a Map<String, Entity> where Entity would be a class that contains your inner structure(s).

Example:

Map<String, Entity> entities = ...;

Further assuming that the inner elements 0 to n are densely packed (i.e. there are no or only small holes) you could use an ArrayList (assigned to a List property) and use the get(index) method to access the element at that index.

Example:

class Entity {
  List<Element> elements = new ArrayList<>();

  Element getElement( int index ) {
    if( index < 0 || index >= elements.size() ) {
      return null; //out of range, i.e. no such element
    }

    return elements.get(index);
  } 
}

Hence a lookup for element "ENT_02" could look like this:

//note that you'd need to handle "ENT" not being present, i.e. the get() returning null
entities.get( "ENT" ).getElement( 2 ); 

If your structure is as simple you could actually just make it a Map<String, List<Element>> but I'd assume that it isn't that simple or won't stay that way (i.e. Entity will probably contain more than just the list of elements).

Thomas
  • 87,414
  • 12
  • 119
  • 157