我想有一个正则表达式匹配字符串与开头没有空格.但是包含中间空格的字符串可以匹配.到目前为止,我在下面尝试过
[^-\s][a-zA-Z0-9-_\\s]+$
Run Code Online (Sandbox Code Playgroud)
上面不允许在开头使用空格,但也不允许在字符串的中间/末尾.请帮我.
虽然我正在做一些运行来测试这个线程中的一些代码,但我发现了一个奇怪的事情,如果你考虑以下程序
import java.util.ArrayList;
import java.util.List;
public class OverloadTest {
public String test1(List l){
return "abc";
}
public int test1(List<Integer> l){
return 1;
}
public static void main(String [] args) {
List l = new ArrayList();
System.out.println(new OverloadTest().test1(l));
}
}
Run Code Online (Sandbox Code Playgroud)
我期待Java编译器由于字节码Erasure属性而显示模糊错误,但事实并非如此.现在,当我尝试运行此代码时,我期待test1(List)
将被调用并且输出将是"abc"
令我惊讶的它被调用test1(List<Integer>)
(输出是1
)
我甚至尝试过如下
List l = new ArrayList();
l.add("a");
System.out.println(new OverloadTest().test1(l));
Run Code Online (Sandbox Code Playgroud)
但仍然发现Java调用test1(List<Integer> param)
方法,当我检查param
它时,它有String
"a"(Java如何投射List<String>
到List<Integer>
?)
我试图了解java.util.concurrent
包中的实用程序,并了解到我们可以在方法中成功完成任务之后向callable
对象提交ExecutorService
,该对象返回Future
,其中填充了返回的值.callable
call()
我理解所有的callables都是使用多个线程并发执行的.
当我想看看ExecutorService
批量任务执行有多少改进时,我想到了捕获时间.
以下是我试图执行的代码 -
package concurrency;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class ExecutorExample {
private static Callable<String> callable = new Callable<String>() {
@Override
public String call() throws Exception {
StringBuilder builder = new StringBuilder();
for(int i=0; i<5; i++) {
builder.append(i);
}
return builder.toString();
}
};
public static void main(String [] args) {
long start = System.currentTimeMillis();
ExecutorService …
Run Code Online (Sandbox Code Playgroud) 我是spring框架的新手今天我遇到了web.xml文件中的调度程序servlet配置,我想出了一个关于url模式的问题,比如这个语法/.那么,如果我在tomcat服务器中部署web应用程序,实际上"/"符号适用的是:host:port /或host:port/myWeb /
我注意到构造函数和类的简单方法做同样的工作.创建类的构造的确切原因是什么?如果我创建MyClass(){}
构造函数和MyClassMethod(){}
方法,它将执行与编写这些方法和构造函数的正文部分相同的工作.那么构建的需要是什么?它有什么特殊用途吗?
当我正想通过这个文章,下部分私人会员超类中,我看到了这条线
嵌套类可以访问其封闭类的所有私有成员,包括字段和方法。因此,子类继承的公共或受保护的嵌套类可以间接访问超类的所有私有成员。
我的问题是我们如何才能直接访问Nested
类Base
中Derived
(就像我们可以访问任何public
,protected
字段)?
和
如果有一种方法,怎么能Derived
访问p
它的私有字段Base
通过Nested
?
public class Base {
protected int f;
private int p;
public class Nested {
public int getP() {
return p;
}
}
}
class Derived extends Base {
public void newMethod() {
System.out.println(f); // i understand inheriting protected field
// how to access the inherited Nested class here? and if accessed how to …
Run Code Online (Sandbox Code Playgroud) 我知道这Session
是Hibernate使用的一级缓存这一事实,一旦我们从 中检索实体session
,对于具有相同标识符的同一实体的后续 get 调用将从而不是从 DB 中获取,直到是Open。session
session
话虽如此,我对休眠如何将一级缓存与数据库同步有疑问?考虑以下场景
//Lets say I have created the session
Session s1 = sessionFactory.getSession();
User u1 = s1.get(User.class, 1); //Getting User with ID=1
//s1 is not yet closed
//Lets say I create some other session
Session s2 = sessionFactory.getSession();
User u2 = s2.get(User.class, 1); //Getting User with ID=1
u2.setName("Abc"); // Changed a field
s2.save(u2); // Saved the changes to DB
s2.close(); //Closed …
Run Code Online (Sandbox Code Playgroud) 下面有我的hql:
update User set count = count + ?2 where id = ?1
Run Code Online (Sandbox Code Playgroud)
以下是例外情况:
org.hibernate.hql.internal.ast.QuerySyntaxException: expecting OPEN, found '+' near line 1, column 71 [update com.yitaosoft.edm.common.persist.entity.User set count = count + ? where id = ?]
at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54)
at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:47)
at org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:79)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:278)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:182)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:138)
Run Code Online (Sandbox Code Playgroud)
我想更新用户设置count = count + xx,其中id = xx.但得到了语法错误.为什么?是不是在hql中支持?怎么解决这个问题?
下面是String.hashCode()
Java 8 的方法源代码片段(准确地说是1.8.0_131)
/**
* Returns a hash code for this string. The hash code for a
* {@code String} object is computed as
* <blockquote><pre>
* s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
* </pre></blockquote>
* using {@code int} arithmetic, where {@code s[i]} is the
* <i>i</i>th character of the string, {@code n} is the length of
* the string, and {@code ^} indicates exponentiation.
* (The hash value of the empty string is zero.)
*
* …
Run Code Online (Sandbox Code Playgroud) 我有一个非常基本的问题SynchronizedList
.
让我说我有synchronizedList作为 -
List syncList = Collections.synchronizedList(new ArrayList<>())
Run Code Online (Sandbox Code Playgroud)
现在我的场景是线程A试图访问add()
api和线程B试图访问remove()
synchronizedList的api.Both线程是否能够同时访问Both(添加和删除)api.
我相信线程不应该同时访问api(add()和remove()).如果我错了,请纠正我.
java ×9
hibernate ×2
concurrency ×1
constructor ×1
hashcode ×1
inheritance ×1
java-8 ×1
javac ×1
javascript ×1
jpa ×1
methods ×1
nested-class ×1
overloading ×1
regex ×1
spring ×1
spring-mvc ×1
string ×1