1

This is a simplified example of how it will be used!

A map with n-elements (in this example there are three; One, Two and Three). The map is defined as <String, DecrementableInteger> were the later is a wrapper for a mutable integer.

class DecrementableInteger
{
    private int timeLeftMax;
    private int timeLeft;

    public DecrementableInteger (int v) {
        timeLeft = v;
        timeLeftMax = v;
    }

    public void update (int v) {
        timeLeft -= v;
    }

    public int get () {
        return timeLeft;
    }

    public void reset () {
        timeLeft = timeLeftMax;
    }
}

Then there is the usage of it.

class Test
{
    private final Map<String, DecrementableInteger> integers = new CircleHashMap<>(); //?
    private String activeKey = null;

    Test () {
        integers.put("One", new DecrementableInteger(4));
        integers.put("Two", new DecrementableInteger(1));
        integers.put("Three", new DecrementableInteger(2));
    }

    void update () {
        if (activeKey == null) {
            for (Entry<String, DecrementableInteger> e : integers.entrySet()) {
                e.getValue().update (1);
                if (e.getValue().get () <= 0) {
                    System.out.println(e.getKey() + " just finished, resetting.");
                    e.getValue().reset();
                    activeKey = e.getKey();
                    break;
                }
            }
        } else {
            System.out.println(activeKey + " did their thing, continuing!");
            activeKey = null;
        }
    }
}

The update method is called many times every second.

The first iteration will give the following result.

  1. One => 3
  2. Two => 0, Two just finished, resetting. Two => 1

Iteration broke, continue at Three next time.

  1. Three => 1

Iteration done, start over at One next time.

  1. One => 2,
  2. Two => 0, Two just finished, resetting. Two => 1

Iteration broke, continue at Three next time.

  1. Three => 0, Three just finished, resetting. Three => 2

Iteration done, start over at One next time.

  1. One => 1
  2. ...

Meaning the code will need have the following qualities:

  • The Map needs to remember its order, it can not shuffle them around.
  • The code needs to remember were it left off; currently in activeKey
  • The iteration needs to be able to start from activeKey

Is there a efficient and neat solution to a CircleList? If so, what would that solution look like.

Emz
  • 1,280
  • 1
  • 14
  • 29
  • Why are you using map? Do you select value based on its key or not? – libik Jul 28 '15 at 15:19
  • In this simplified example I do not. However selection is used on the full code. Which I can not really post here due to its healthy length. – Emz Jul 28 '15 at 15:51

1 Answers1

1

Do you need to use a Map, or could you use a List?

If it needs to be a Map, you should use TreeMap, as TreeMap has defined ordering. Then you need to create a special Iterator, which essentially traverses the tree, and goes back to element 1 when it reaches the end.

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • More information can be found [here](http://stackoverflow.com/questions/5849154/can-we-write-our-own-iterator-in-java). – Emz Jul 28 '15 at 15:52