猿问

在 Java 中懒惰地创建一个颜色映射

我创建了一个函数来在 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);

    }

我想创建一个 java 流,当请求颜色时,它会生成一个新的随机数并延迟返回一个随机颜色。


像惰性列表之类的东西。


编辑:


我读过函数式编程范式支持惰性列表。例如,有一个斐波那契数列的列表,该列表理论上存储列表中的所有元素,但实际上,会在请求时生成第 n 个元素。


我想用一些逻辑来模拟惰性颜色映射。我生成的颜色图只有 13 种颜色的输入。我不想手动输入高达一百左右的值。


我想模拟一个懒惰的彩色地图。因此,当我请求键为 35 的颜色时,colorMap 是用一些最多为 35 的随机颜色制定的,并返回第 35 个值。


我基本上只能返回 35 并存储它,但理想情况下,如果访问 35,在我的扩展程序中,您可以假设存在最大 35 的值。


小唯快跑啊
浏览 106回答 1
1回答

狐的传说

这是一个开始:import java.awt.Color;import java.util.HashMap;import java.util.Random;public class Main {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; ColorMap map = new ColorMap();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(map.get(19));&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(map.get(9));&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(map.get(9));&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(map.get(0));&nbsp; &nbsp; &nbsp; &nbsp; for(int key : map.keySet()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(key);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}class ColorMap extends HashMap<Integer, Color>{&nbsp; &nbsp; Random rand = new Random();&nbsp; &nbsp; @Override&nbsp; &nbsp; public Color get(Object key){&nbsp; &nbsp; &nbsp; &nbsp; if(! (key instanceof Integer)) return null;&nbsp; &nbsp; &nbsp; &nbsp; Color color = super.get(key);&nbsp; &nbsp; &nbsp; &nbsp; if(color == null){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; color = makeRandomColor();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; put((Integer) key,color);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return color;&nbsp; &nbsp; }&nbsp; &nbsp; private Color makeRandomColor(){&nbsp; &nbsp; &nbsp; &nbsp; return new Color( rand.nextFloat(), rand.nextFloat(), rand.nextFloat());&nbsp; &nbsp; }}编辑:要填充缺失值,您需要循环并检查所有先前值:import java.awt.Color;import java.util.HashMap;import java.util.Random;public class Main {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; ColorMap map = new ColorMap();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(map.get(19));&nbsp; &nbsp; &nbsp; &nbsp; for(int key : map.keySet()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(key);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}class ColorMap extends HashMap<Integer, Color>{&nbsp; &nbsp; Random rand = new Random();&nbsp; &nbsp; @Override&nbsp; &nbsp; public Color get(Object key){&nbsp; &nbsp; &nbsp; &nbsp; if(! (key instanceof Integer)) return null;&nbsp; &nbsp; &nbsp; &nbsp; int positiveIntKey = Math.abs((Integer) key);&nbsp; &nbsp; &nbsp; &nbsp; Color color = super.get(positiveIntKey);&nbsp; &nbsp; &nbsp; &nbsp; if(color == null){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fillMapUpTo(positiveIntKey);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return super.get(positiveIntKey);&nbsp; &nbsp; }&nbsp; &nbsp; private void fillMapUpTo(Integer positiveIntKey) {&nbsp; &nbsp; &nbsp; &nbsp; for(int key = 0; key <= positiveIntKey; key++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(super.get(key) == null){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; put(key,makeRandomColor());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private Color makeRandomColor(){&nbsp; &nbsp; &nbsp; &nbsp; return new Color( rand.nextFloat(), rand.nextFloat(), rand.nextFloat());&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答