N.Kuno
N.Kuno

Reputation: 13

Is there a Map object with takes index and key and object? Java

I'm trying to emulate a rotor of an enigma machine in Java. I need an object which takes an index, a key and an object, because I unsuccessfully tried HashMaps like this:

private HashMap<Integer,Integer> rotorWiring = new HashMap<Integer, Integer();
private HashMap<Integer,Integer> reverseRotorWiring = new HashMap<Integer, Integer>();

//The "wiring" of the rotor is set from a String,
    public void setRotorWiring(String Wiring) {
    if (Wiring.length()==26) {
        for (int i=0; i<Wiring.length();i++ ) {
            char tempChar = Wiring.charAt(i);
            int valueOfChar = (int)tempChar-64; 
            if (valueOfChar<=26){
                this.rotorWiring.put(i+1,valueOfChar);
                this.reverseRotorWiring.put(valueOfChar,i+1);
            }
        }
    }

}

So far so good, this allows me to translate e.x. an A to an E, however, once I tried to simulate a turn of the rotor like this:

//It should be mentioned that I designing the program to only accept characters a to z inclusive.
public void turn() {
    for (int i=1;i<=rotorWiring.size();i++) {
        if (i!=26) {
            rotorWiring.replace(i, rotorWiring.get(i+1));
        }
        else {
            rotorWiring.replace(i, rotorWiring.get(1));
        }
    }
    for (int i=1;i<=rotorWiring.size();i++) {
        if (i!=26) {
            reverseRotorWiring.replace(i, rotorWiring.get(i+1));
        }
    }
}

However, I noticed that this rather simulates an offset of the internal wiring of the rotor rather than a turn... I'm asking for a "Map"-like solutions with an index, key and object, because that would allow me to offset the index of all the keys and objects by 1, thus simulating a turn.

I am, however, open to suggestions for different solutions to this problem.

It should be mentioned that I'm a bit of a novice, and therefore appreciate rather in-depth explanations.

Many thanks.

Upvotes: 1

Views: 68

Answers (2)

N.Kuno
N.Kuno

Reputation: 13

A map of maps was the solution! It was solved like this:

    private HashMap<Integer,HashMap<Integer,Integer>> rotorWiring = new HashMap<Integer, HashMap<Integer,Integer>>();
    private HashMap<Integer,HashMap<Integer,Integer>> reverseRotorWiring = new HashMap<Integer, HashMap<Integer,Integer>>();

    public void setRotorWiring(String Wiring) {
    if (Wiring.length()==26) {
        for (int i=0; i<Wiring.length();i++ ) {
            HashMap<Integer, Integer> wire = new HashMap<Integer, Integer>();
            HashMap<Integer, Integer> reverseWire = new HashMap<Integer, Integer>();
            char tempChar = Wiring.charAt(i);
            int valueOfChar = (int)tempChar-64; 
            if (valueOfChar<=26){
                wire.put(i+1,valueOfChar);
                reverseWire.put(valueOfChar,i+1);
                rotorWiring.put(i, wire);
                reverseRotorWiring.put(i, reverseWire);
            }
        }
    }

}

Upvotes: 0

Nikolas
Nikolas

Reputation: 44368

Welcome to StackOverflow. There doesn't exist an implementation of what you have described in JDK. However, there are more ways to achieve the storing of Integer-String-Object. Note that both the index and the key are unique by definition. Also, note that the index-key are tightly coupled. You might want to put a Map to another Map:

Map<Integer, Map<String, MyObject>> map;

Or use a collection characteristic for indices:

List<Map<String, MyObject>>

Be careful with removing items which change the index of all the subsequent elements - replace it with null instead to keep the indices. Alternatively, you can create a decorator for your defined object with index/key:

Map<Integer, MyDecoratedObject> map;

Where the MyDecoratedObject would look like:

public class MyDecoratedObject {

     private final String key;      // or int index
     private final MyObject delegate;

     // Full-args constructor, getters
}

Finally, it's up to you to pick a way that satisfied your requirements the most.

Upvotes: 1

Related Questions