-1

I created a function to create a colour map in java.

final void setDefaultColourMap() {
        colourMap = new HashMap<>();
        colourMap.put(0, Color.BLACK);
        colourMap.put(1, Color.RED);
        colourMap.put(2, Color.GREEN);
        colourMap.put(3, Color.BLUE);
        colourMap.put(4, Color.PINK);
        colourMap.put(5, Color.YELLOW);
        colourMap.put(6, Color.CYAN);
        colourMap.put(7, Color.ORANGE);
        colourMap.put(8, Color.MAGENTA);
        colourMap.put(9, Color.DARK_GRAY);
        colourMap.put(10, Color.WHITE);
        colourMap.put(11, Color.GRAY);
        colourMap.put(12, Color.LIGHT_GRAY);
    }

I want to create a java stream, which when requested for a colour generates a new random number and returns a random color lazily.

Something like lazy lists.

Edit:

I have read that functional programming paradigm supports lazy lists. For example, there is a list for Fibonacci series, the list theoretically stores all the elements in the list, but practically, will generate the nth element when requested.

I want to simulate lazy color map with some logic. The color map I generated has input only for 13 colors. I don't want to manually enter values upto hundred or so.

I want to simulate a lazy color map. So, when I request for a color with key 35, the colorMap is formulated with some random colors upto 35 and the 35th value is returned.

I could basically return only 35 and store it but, ideally, if a 35 is access, in my extended program, you can assume that values upto 35 exist.

Tarun Maganti
  • 3,076
  • 2
  • 35
  • 64
  • What you are trying to achieve is not clear to me. On one hand : "I don't want to access colors from my color map" on the other hand you do: "map.get(15)" . I recommend you [edit] and redefine your need. – c0der Jul 29 '19 at 06:25
  • Is [this](https://ide.geeksforgeeks.org/qz9mfCT9Ig) what you are trying to achieve ? – c0der Jul 29 '19 at 06:55
  • @c0der yes. It is good. But, if the color doesn't exist, can I just populate it upto the requested value. – Tarun Maganti Jul 29 '19 at 06:57
  • @c0der It is beautiful though. – Tarun Maganti Jul 29 '19 at 06:58

1 Answers1

1

Here is a start:

import java.awt.Color;
import java.util.HashMap;
import java.util.Random;

public class Main {

    public static void main(String[] args) {
        ColorMap map = new ColorMap();
        System.out.println(map.get(19));
        System.out.println(map.get(9));
        System.out.println(map.get(9));
        System.out.println(map.get(0));

        for(int key : map.keySet()){
            System.out.println(key);
        }
    }
}

class ColorMap extends HashMap<Integer, Color>{

    Random rand = new Random();

    @Override
    public Color get(Object key){

        if(! (key instanceof Integer)) return null;

        Color color = super.get(key);
        if(color == null){
            color = makeRandomColor();
            put((Integer) key,color);
        }
        return color;
    }

    private Color makeRandomColor(){
        return new Color( rand.nextFloat(), rand.nextFloat(), rand.nextFloat());
    }
}

Edit: To populate up to the missing value you need to loop and check all prior values:

import java.awt.Color;
import java.util.HashMap;
import java.util.Random;

public class Main {

    public static void main(String[] args) {
        ColorMap map = new ColorMap();
        System.out.println(map.get(19));

        for(int key : map.keySet()){
            System.out.println(key);
        }
    }
}

class ColorMap extends HashMap<Integer, Color>{

    Random rand = new Random();

    @Override
    public Color get(Object key){

        if(! (key instanceof Integer)) return null;
        int positiveIntKey = Math.abs((Integer) key);

        Color color = super.get(positiveIntKey);
        if(color == null){
            fillMapUpTo(positiveIntKey);
        }
        return super.get(positiveIntKey);
    }

    private void fillMapUpTo(Integer positiveIntKey) {

        for(int key = 0; key <= positiveIntKey; key++){
            if(super.get(key) == null){
                put(key,makeRandomColor());
            }
        }
    }

    private Color makeRandomColor(){
        return new Color( rand.nextFloat(), rand.nextFloat(), rand.nextFloat());
    }
}
c0der
  • 18,467
  • 6
  • 33
  • 65