简体   繁体   中英

LinkedHashMap or two separate arrays in java

I have two arrays of predefined length, let us say 8, which contains related data. I can arrange them in such a way that array1[0] has corresonding mapping at array2[0]. That is key value pairs are at the same index in both arrays. In such a case, for iterating the array, I can either use a for loop like below

for(int i=0;i<array1.length;i++){
  int array1Val = array1[i];
  String array2Val = array2[i];
    //some code
 }  

Is this approach gud? Or is it better to use linked hashmap instead and loop using

 map.entrySet()

Complexity wise and efficiency wise which is a better approach?

Rule of thumb: When you are considering creating corresponding arrays, then it's time to create a class for that corresponding data, and create an array of that class. This is the clearest, most readable, object-oriented way to store your corresponding data in objects in Java. (It doesn't have to be called "Data".)

class Data {
   private int value1;
   private String value2;
   // Any other arrays?  Make another value here.
   // Constructor, getters, setters
}

Then declare an array of that class, and store objects in it here.

Data[] mydata = new Data[length];
for (int i = 0; i < length; i++) {
   mydata[i] = new Data();  // and you can then initialize the Data object too.
}

Then one for loop can access each Data object.

for (int i = 0; i < length; i++) {
   Data data = mydata[i];
   // Extract values here.
}

(Or you can use the enhanced "foreach" loop also.)

for (Data data : mydata) {
   // Extract values here.
}

Use a map.

In terms of performance, you are iterating over the length of the data structure.

so its only o(n)

reading from a map is o(1), reading from an array is also o(1)

so really no difference in performance.

But in terms of readability and maintainability its better to have a single "map" data structure then two array data structures that need to be consumed/used in a "special way" (in this case you need to remember that array1[0] maps to array2[0]. That makes the code less readable and more complicated than it has to be.

Keep your code clean, easy to read, and simple, use a map:

Here is are ways to loop thru a map in java:

for (Map.Entry<String, Object> entry : map.entrySet()) {
    String key = entry.getKey();
    Object value = entry.getValue();
    // ...
}

for (Object value : map.values()) {
    // ...
}

[UPDATE]

In response to people who suggest "hey create a Class"...

Please ask yourself "who needs to consume this data"?

If the data is only relevant to an internal method, then I suggest using an anonymous class like here:

public void myMethod(String param1, String param2) {
    class MyAnonymousClass {
        String data1;
        String data2;
    }
    MyAnonymousClass anonymous = new MyAnonymousClass();
    anonymous.data1 = param1;
    anonymous.data2 = param2;
}

If the data is only relevant to the internals of another Class then create a private static inner class.

 pubic class SomeClass {
    private static class MyDataContainer {

    }
 }

Just be aware of these choices when creating classes.

Cheers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM