javaperformancelistdictionaryset

List vs. Map: Which takes less space and more efficient?


I have two classes SensorNetwork and Sensor.

class SensorNetwork
{
    Set<Integer> sensorIDs; // Sensor networks have a collection of sensors.
    Set<Integer> adjacents; // Adjacency list of SensorNetwork objects.
}

class Sensor
{
    int networkID; // ID of the network of which this sensor belongs to
    Map<Integer, Float> adjacents; // Adjacency list of sensors in form of <ID, distance>
}

Number of SensorNetworks are predefined (up to 1000). Hence, I may use an array. But number of Sensors are undefined (at most number of sensor networks / 4).

When you consider addition, deletion and get(), I need the one which is faster and takes less space (because I'm going to use serialization).

Here are my options (as far as I have thought)

Option 1: Don't define a class for SensorNetwork. Instead, use List<Set<Integer>> sensorNetwork; and another map for Map<Integer, Set> networkAdjacencies;
Option 2: Use Map<Integer, Set<Integer>> sensorNetwork. In case I want to get sensors of network i, I simply write sensorNetwork.get(i).
Option 3: Don't define classes. Instead, use option 2 and for Sensor class:

Map<Integer, Map<Integer, Float>> sensorAdjacencies;

Which option should I choose in terms of space and time efficiency?


Solution

  • This sounds like it'd be very helpful for you (specifically the Data Structures section): http://bigocheatsheet.com/

    You say

    I need my structure to be efficient while adding, removing and finding elements. No other behavior.

    The problem is that Lists and Maps are usually used in totally different cases. Their names describe their use cases fairly well -- you use a List if you need to list something (probably in some sequential order), while a Map would be used if you need to map an input to an output. You can use a Map as a List by mapping Integers to your elements, but that's overcomplicating things a bit. However, even within List and Map you can have different implementations that differ wildly in asymptotic performance.

    With few exceptions, data structures will take O(n) space, which makes sense. If memory serves, anything other than an ArrayList (or other collections backed only by a primitive array) will have a decent amount of space overhead as they use other objects (e.g. Nodes for LinkedLists and Entry objects for Maps) to organize the underlying structure. I wouldn't worry too much about this overhead though unless space really is at a premium.

    For best-performance addition, deletion, and search, you want to look at how the data structure is implemented.

    The following two structures are usually Maps, and so usually require you to implement equals() (and hashCode() for HashMaps):

    Hope that rundown helped. If you clear up how your program is structured, I might be able to give a recommendation. Until then, the best I can do is show you the options and let you pick.