java 1>>2_Java中(key1, key2) -> key2怎么理解?

  • Post author:
  • Post category:java


问题一

Collectors.toMap(PositionContent::getName, Function.identity(), (key1, key2) -> key2)

这个怎么理解?

Map map = list.stream().collect(Collectors.toMap(PositionContent::getName, Function.identity(), (key1, key2) -> key2));

问题二

.newBuilder()

.maximumSize(1000)

.expireAfterAccess(10, TimeUnit.MINUTES)

这种点点是什么意思?这段是什么意思?

static LoadingCache screenSource = CacheBuilder.newBuilder()

.maximumSize(1000)

.expireAfterAccess(10, TimeUnit.MINUTES)

.build(

new CacheLoader() {

public Object load(String key) {

return getValue(key);

}

});

源码

public class PurposeFileUtil {

public static final String Positions =”positions”;

static LoadingCache screenSource = CacheBuilder.newBuilder()

.maximumSize(1000)

.expireAfterAccess(10, TimeUnit.MINUTES)

.build(

new CacheLoader() {

public Object load(String key) {

return getValue(key);

}

});

private static Object getValue(String key){

Config conf = ConfigFactory.parseFile(new File(“perception-purpose.conf”));

List list = JSON.parseArray(JSON.toJSONString(conf.getAnyRefList(“screen.positions”)),PositionContent.class);

Map map = list.stream().collect(Collectors.toMap(PositionContent::getName, Function.identity(), (key1, key2) -> key2));

switch(key){

case Positions:

return map;

default :

return conf.getAnyRefList(“screen.responce.time.line”);

}

}

public static Object getSourceByKey(String key)throws Exception{

return screenSource.get(key);

}

public static void initSource(){

try {

JNotify.addWatch(“conf”, JNotify.FILE_MODIFIED, false, new JNotifyListener(){

@Override

public void fileRenamed(int wd, String rootPath, String oldName, String newName) {

}

@Override

public void fileModified(int wd, String rootPath, String name) {

if(name.equals(“source.conf”)){

loadSource(rootPath,name);

}

}

public void fileDeleted(int wd, String rootPath, String name) {

}

public void fileCreated(int wd, String rootPath, String name) {

}

});

} catch (JNotifyException e) {

log.error(“error watch file”);

}

while (true) {

try {

Thread.sleep(60000);

} catch (InterruptedException e) {

}

}

}

private static void loadSource(String rootPath , String name){

Config conf = ConfigFactory.parseFile(new File(rootPath+”/”+name));

List list = JSON.parseArray(JSON.toJSONString(conf.getAnyRefList(“screen.monitor”)),PositionContent.class);

Map map = list.stream().collect(Collectors.toMap(PositionContent::getName, Function.identity(), (key1, key2) -> key2));

screenSource.put(Positions, map);

}

}



版权声明:本文为weixin_42357256原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。