我正在尝试使用在编译时不可用的类动态创建lambda实例,因为它们是在运行时生成的,或者在编译时尚不知道.
这可以使用以下代码
// lambdaClass = class of the lambda interface
// className = class containing the target method
// methodName = name of the target method
private static <T> T lookupLambda(Class<T> lambdaClass, String className, String methodName, Class<?> returnType,
Class<?> argumentType) throws Throwable {
MethodType lambdaType = MethodType.methodType(lambdaClass);
MethodType methodType = MethodType.methodType(returnType, argumentType);
MethodHandles.Lookup lookup = MethodHandles.lookup();
Class<?> targetClass = Class.forName(className);
MethodHandle handle = lookup.findStatic(targetClass, methodName, methodType);
CallSite callSite = LambdaMetafactory
.metafactory(lookup, "call", lambdaType, methodType.unwrap(), handle, methodType);
MethodHandle methodHandle = callSite.getTarget(); …Run Code Online (Sandbox Code Playgroud) 我正在尝试重用相同的HashMap,例如下面的例子来填充列表.首先,我在地图中添加了一些值,将地图添加到列表中,然后清除地图,以便再次添加新值并在列表中添加第二组值,依此类推......
但是,似乎clear()方法还删除了先前在列表中添加的值,如果我不使用clear()方法,则先前在列表中添加的每组值都会被新的值集覆盖,以便最终在这个特殊的例子我将在列表中有4个相同的值集.
我做错了什么?
List<HashMap<String, String>>dataList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put(Answer.ID, "0");
map.put(Answer.IMAGE, "color_icon_awesome");
map.put(Answer.TITLE, firstOption);
dataList.add(map);
map.clear();
map.put(Answer.ID, "1");
map.put(Answer.IMAGE, "color_icon_awesome");
map.put(Answer.TITLE, secondOption);
dataList.add(map);
map.clear();
map.put(Answer.ID, "2");
map.put(Answer.IMAGE, "color_icon_awesome");
map.put(Answer.TITLE, thirdOption);
dataList.add(map);
map.clear();
map.put(Answer.ID, "3");
map.put(Answer.IMAGE, "color_icon_awesome");
map.put(Answer.TITLE, fourthOption);
dataList.add(map);
map.clear();
Run Code Online (Sandbox Code Playgroud) Thread.sleep以long毫秒为参数.但是数字文字被视为整数,除非另外用字母符号表示1000L.那为什么这个有效的代码呢?
Thread.sleep(1000);
Run Code Online (Sandbox Code Playgroud)