假设我想声明Spring RowMapper,但不是创建动态类,而是实现一个实现RowMapper的抽象类.这是我的方法签名:
SqlProcedure#declareRowMapper(RowMapper<?> rowMapper);
Run Code Online (Sandbox Code Playgroud)
CustomRowMapper.java:
public abstract class CustomRowMapper<T> implements RowMapper<T> {
protected A a = new A();
}
Run Code Online (Sandbox Code Playgroud)
旧的Java方式是写:
sqlProc.declareRowMapper(new CustomRowMapper<Object>() {
@Override
public Object mapRow(ResultSet rs, int rowNum) {
a.doSomething(rs, rowNum);
return new Object();
}
});
Run Code Online (Sandbox Code Playgroud)
是否有可能用lambda表达式实现相同的功能?我想做这样的事情:
sqlProc.declareRowMapper((rs, rowNum) -> {
a.doSomething(rs, rowNum);
return new Object();
});
Run Code Online (Sandbox Code Playgroud)
但后来我会得到一个编译错误说a cannot be resolved.这是因为Java将此视为该RowMapper#mapRow方法的实现,而不是CustomRowMapper#mapRow.
如何通过lambda表达式告诉Java使用CustomRowMapper而不是RowMapper?这甚至可能吗?
大家好我有一个List<User>,我想添加一个方法,返回使用Id找到的特定用户.我想使用lambda表达式,所以我尝试了这个,但它不起作用.
...
List<User> user = users.stream().filter(x -> x.id == id).collect(Collectors.toList());
return user[0];
Run Code Online (Sandbox Code Playgroud)
这段代码不编译并给我这些错误:
The method stream() is undefined for the type List<User>
Lambda expressions are allowed only at source level 1.8 or above *
Collectors cannot be resolved
Run Code Online (Sandbox Code Playgroud)
我目前遇到Java的泛型类型擦除和运行时注释的问题,我不确定我是做错了什么,或者它是Java编译器中的错误.考虑以下最小的工作示例:
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface MyAnnotation {
}
public interface MyGenericInterface<T> {
void hello(T there);
}
public class MyObject {
}
public class MyClass implements MyGenericInterface<MyObject> {
@Override
@MyAnnotation
public void hello(final MyObject there) {
}
}
Run Code Online (Sandbox Code Playgroud)
现在,当我使用反射查询有关MyClass.hello的信息时,我希望hello方法仍然具有注释,但它不会:
public class MyTest {
@Test
public void testName() throws Exception {
Method[] declaredMethods = MyClass.class.getDeclaredMethods();
for (Method method : declaredMethods) {
Assert.assertNotNull(String.format("Method '%s' is not annotated.", method), method
.getAnnotation(MyAnnotation.class));
}
}
}
Run Code Online (Sandbox Code Playgroud)
(意外)错误消息如下所示:
java.lang.AssertionError:方法'public void test.MyClass.hello(java.lang.Object)'未注释.
用Java 1.7.60测试.
class OuterA {
class InnerA {
}
}
class SubclassC extends OuterA.InnerA {
SubclassC(OuterA outerRef) {
outerRef.super();
}
}
class XYZ {
public static void main(String[] args) {
new SubclassC(new OuterA());
}
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中
OuterA对象引用传递给SubclassC.java文件的构造函数进行编译?InnerA不需要将对象引用传递给SubclassC构造函数?我试图根据这个链接试验项目Jigsaw http://openjdk.java.net/projects/jigsaw/doc/quickstart.html
但是,我无法jmod在Java 9早期访问构建中找到命令.同样,javac发出错误消息说
javac:无效标志:-modulepath".
任何的想法?在Java 9中试用Jigsaw的最佳方法是什么?
何时使用System.identityhashcode()和hashcode()方法?*
我在我的应用程序的web.xml中定义了一个context-param,如下所示
<context-param>
<param-name>baseUrl</param-name>
<param-value>http://www.abc.com/</param-value>
</context-param>
Run Code Online (Sandbox Code Playgroud)
现在我想在我的Controller中使用baseUrl的值,所以我怎么能访问这个.....?
如果有人知道,请告诉我.
提前致谢 !
我在想,如果有一个简单的方法(一个在线,而无需创建一个功能)转换String到BooleanJava中,但在某种程度上说Boolean是null如果String是null.
如果我看到它正确,如果输入字符串是,则Boolean类中的所有方法都将返回.这是为什么?falsenull
为什么在存在而不是返回的情况下Boolean.valueOf(String s)返回更好? falsesnullnull
我有这些陈述:
int \u65549 = 9;
System.out.println(\u65549);
Run Code Online (Sandbox Code Playgroud)
这完全编译.和输出
9
Run Code Online (Sandbox Code Playgroud)
但是:
System.out.println(Character.isJavaIdentifierStart(\u65549));
Run Code Online (Sandbox Code Playgroud)
输出
false
Run Code Online (Sandbox Code Playgroud)
此方法无法处理补充字符.要支持所有Unicode字符(包括增补字符),请使用该
isJavaIdentifierStart(int)方法.
然后我这样做了:
int x = \u65549;
System.out.println(Character.isJavaIdentifierStart(x));
Run Code Online (Sandbox Code Playgroud)
但即使这样打印:
false
Run Code Online (Sandbox Code Playgroud)
那么,这是否意味着,Java是\u65549一个标识符?
在春季3规划环境地政司,#this并#root进行了介绍.
始终定义变量#root并引用根上下文对象.虽然#this可能会随着表达式的组件的评估而变化,但#root始终引用根.
我已经阅读了文档,但我仍然不明白是什么#root意思(没有例子).有人可以举个例子吗?
java ×9
spring ×3
java-8 ×2
lambda ×2
annotations ×1
boolean ×1
eclipse ×1
generics ×1
hashcode ×1
identifier ×1
inheritance ×1
java-9 ×1
openjdk ×1
reflection ×1
spring-el ×1