在尝试使用方法引用时,遇到了 concat 方法可以用作 BiFunction 的情况,据我了解 BiFunction apply 方法需要 2 个输入参数并产生结果。而 concat 方法接受 1 个输入参数并返回与该值连接的字符串。
示例代码:
public class Test {
public static void main(String[] args) {
Test t = new Test();
String str1 = "Hello";
String str2 = "Workld";
System.out.println(t.stringManipulator(str1, str2, String::concat));
System.out.println(str1);
System.out.println(str2);
}
private String stringManipulator(String inputStr, String inputStr2, BiFunction<String, String, String> function) {
return function.apply(inputStr, inputStr2);
}
}
Run Code Online (Sandbox Code Playgroud)
输出
HelloWorkld
Hello
Workld
Run Code Online (Sandbox Code Playgroud)
有人可以帮助我理解这里发生了什么吗?
我在这里有一个问题:
场景: 我有一个JSF-2,Spring(Beans wiring)应用程序.我写了一个自定义验证器,我想执行它.
@FacesValidator("com.test.vali")
@Named("com.test.vali")
public class TestValidator implements Validator {
@Override
public void validate(FacesContext arg0, UIComponent arg1, Object arg2) throws ValidatorException {
System.out.println("dhkdfkbhdfbkdfksdfdfk");
}
}
Run Code Online (Sandbox Code Playgroud)
我试图使用以下方式注入验证器:
方法1:
<h:inputText value="#{helloWorld.name}">
<f:validator binding="#{com.test.vali}" />
</h:inputText>
Run Code Online (Sandbox Code Playgroud)
产量
当试图渲染页面时,它抛出异常.
javax.servlet.ServletException: /testRichFaces.xhtml @17,48 <f:validator> A validator id was not specified. Typically the validator id is set in the constructor ValidateHandler(ValidatorConfig)
Run Code Online (Sandbox Code Playgroud)
在此搜索了很多,并验证了几种方式:
路#2
<f:validator validatorId="com.test.vali" />
Run Code Online (Sandbox Code Playgroud)
产量
javax.servlet.ServletException: Expression Error: Named Object: com.test.vali not found.
Run Code Online (Sandbox Code Playgroud)
所以从方式#1和方式#2,我可以解释没有任何注释对我有用.
然后,我试图转到最后一种方法:
方式#3:在faces-config.xml中添加验证器,只是为了表明我使用2.0兼容性:
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd" …Run Code Online (Sandbox Code Playgroud) 我对 Spring Boot 很陌生,模型中有一个 Id(主键),它是 String,我需要在保存新实体时自动生成它。
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private String id;
private String name;
private String description;
Run Code Online (Sandbox Code Playgroud)
但是,保存新实体时出现此错误。
"message": "Unknown integral data type for ids : java.lang.String; nested exception is org.hibernate.id.IdentifierGenerationException:
Run Code Online (Sandbox Code Playgroud)
如何避免此错误并在id保存新实体时自动生成。
我试图理解CompletableFuture,并遇到了2个方法,然后是ApplyAsync然后是Make.我试图了解这两者之间的区别.
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
System.out.println(Thread.currentThread().getName() + " Printing hello");
return "Hello";
}).thenCompose((String s) -> {
return CompletableFuture.supplyAsync(() -> {
System.out.println(Thread.currentThread().getName() + " Adding abc");
return "abc "+s;});
}).thenApplyAsync((String s) -> {
System.out.println(Thread.currentThread().getName() + " Adding world");
return s + " World";
}).thenApplyAsync((String s) -> {
System.out.println(Thread.currentThread().getName() + " Adding name");
if (false) {
throw new RuntimeException("Oh no exception");
}
return s + " player!";
}).handle((String s, Throwable t) -> {
System.out.println(s != null ? s : "BLANK"); …Run Code Online (Sandbox Code Playgroud) 假设我有线程池,并且我正在从该线程池中执行一个带有名称的线程的任务thread-a
现在在thread-a我开始一个新线程中让我们调用它thread-child(可能是池线程,也可能不是)这是否意味着thread-a要回到线程池什么时候thread-child运行?还是thread-a会死?
您好我正在编写一个简单的java程序来从套接字读取数据,但遇到了一个问题,在每次输入后我都会有很多空格.
目的:编写一个简单的服务器套接字,它可以从客户端套接字读取CORRECT数据.
到目前为止:我已经能够编写从socket读取的代码,甚至能够读取数据,但事情是我最后得到了很多空格.
所以我不得不使用trim()来管理空间,但我仍然不知道这是对还是错.
会对这方面的意见表示赞赏.
注意:我使用windows7 telnet服务连接到套接字.
readSocket() {
System.out.println(client_socket);
try {
System.out.println("reading socket");
/*BufferedReader brIn = new BufferedReader(new InputStreamReader(client_socket.getInputStream()));
String inputLine = null;
while ((inputLine = brIn.readLine()) != null) {
System.out.println(inputLine);
}*/
InputStream is = client_socket.getInputStream();
byte[] byteArr = new byte[1024];
int inputsize = -1;
int currentPos = 0;
StringBuffer sb = new StringBuffer(11111);
/*while((inputsize = is.read(byteArr)) != -1) {
String processed = new String(byteArr);
sb.append(processed);
}*/
int BUFFER_SIZE = 1024;
int read;
byte[] buffer = new byte[BUFFER_SIZE];
String …Run Code Online (Sandbox Code Playgroud) 我知道这个问题已经回答了一段时间.但是参数一直是一个对象.多数民众赞成我要替换的东西:
if (s.equals("A")) { add (obj = new A(x,y)); }
if (s.equals("B")) { add (obj = new B(x,y)); }
if (s.equals("C")) { add (obj = new C(x,y)); }
Run Code Online (Sandbox Code Playgroud)
我有这个:
try
{
Class cl = Class.forName(s);
Constructor con = cl.getConstructor (x, y);
obj = con.newInstance(x, y);
}
Run Code Online (Sandbox Code Playgroud)
但是它期望x,y作为一个类,但它是一个int.怎么做>
我有一份员工名单,有不同的经历,如
5.0,3.3,5.5,5.6,4.5等..
当我试图通过使用Math.round它给出错误的结果来排序最大到最小经验时,例如:
5.6,5.0,5.5,5.3,4.5等..
我想要的结果如下:
5.6,5.5,5.3,5.0,4.5等..
我在这里用过Collections.sort:
Collections.sort(employeeList, new Comparator<Emp>() {
@Override
public int compare(Emp t, Emp t1) {
return Math.round(t.getExperience() - t1.getExperience()); // which giving wrong results
// return Float.compare(t.getExperience() - t1.getExperience()); // which is not working
}
});
Run Code Online (Sandbox Code Playgroud)
这里t1.getExperience()将给你浮动结果.
java ×8
java-8 ×3
cdi ×1
collections ×1
hibernate ×1
jsf-2 ×1
serversocket ×1
sockets ×1
sorting ×1