Guava: Set + Function = Map?

前端 未结 6 1124
眼角桃花
眼角桃花 2021-02-05 01:40

Is there an idiomatic way to take a Set and a Function, and get a Map live view? (i.e. the Map

6条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-05 02:25

    For the non live view the code exists in lambdaJ with Lambda.map(Set, Converter).

    Set setKs = new Set();
    Converter converterKv = new Converter{
        @Override
        public V convert(K from){
            return null; //Not useful here but you can do whatever you want
        }
    }
    Map mapKvs = Lambda.map(setKs, converterKv);
    

    I tried my own implementation : http://ideone.com/Kkpcn As said in the comments, I have to extends another class so I just implemented Map, that's why there is so much code.

    There is a totally useless (or not ?) feature that allows you to change the converter on the fly.

提交回复
热议问题