小编san*_*hat的帖子

Javascript正则表达式 - 开头没有空格+允许中间空格

我想有一个正则表达式匹配字符串与开头没有空格.但是包含中间空格的字符串可以匹配.到目前为止,我在下面尝试过

[^-\s][a-zA-Z0-9-_\\s]+$
Run Code Online (Sandbox Code Playgroud)

正则表达式可视化

Debuggex演示

上面不允许在开头使用空格,但也不允许在字符串的中间/末尾.请帮我.

javascript regex

12
推荐指数
4
解决办法
4万
查看次数

Java方法重载 - 歧义

虽然我正在做一些运行来测试这个线程中的一些代码,但我发现了一个奇怪的事情,如果你考虑以下程序

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 overloading javac

8
推荐指数
1
解决办法
1153
查看次数

ExecutorService.submit(<callable>)花费更多时间?

我试图了解java.util.concurrent包中的实用程序,并了解到我们可以在方法中成功完成任务之后向callable对象提交ExecutorService,该对象返回Future,其中填充了返回的值.callablecall()

我理解所有的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)

java concurrency java.util.concurrent

7
推荐指数
1
解决办法
4395
查看次数

Dispatcher servlet spring和url模式

我是spring框架的新手今天我遇到了web.xml文件中的调度程序servlet配置,我想出了一个关于url模式的问题,比如这个语法/.那么,如果我在tomcat服务器中部署web应用程序,实际上"/"符号适用的是:host:port /或host:port/myWeb /

java spring spring-mvc

6
推荐指数
1
解决办法
6742
查看次数

在java中创建构造函数的额外好处是什么?

我注意到构造函数和类的简单方法做同样的工作.创建类的构造的确切原因是什么?如果我创建MyClass(){}构造函数和MyClassMethod(){}方法,它将执行与编写这些方法和构造函数的正文部分相同的工作.那么构建的需要是什么?它有什么特殊用途吗?

java methods constructor

6
推荐指数
3
解决办法
1万
查看次数

将嵌套类继承到子类中

当我正想通过这个文章,下部分私人会员超类中,我看到了这条线

嵌套类可以访问其封闭类的所有私有成员,包括字段和方法。因此,子类继承的公共或受保护的嵌套类可以间接访问超类的所有私有成员。

我的问题是我们如何才能直接访问NestedBaseDerived(就像我们可以访问任何publicprotected字段)?

如果有一种方法,怎么能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)

java inheritance nested-class

5
推荐指数
1
解决办法
4668
查看次数

Hibernate 一级缓存 - 它同步吗?

我知道这SessionHibernate使用的一级缓存这一事实,一旦我们从 中检索实体session,对于具有相同标识符同一实体的后续 get 调用将从而不是从 DB 中获取,直到是Opensessionsession

话虽如此,我对休眠如何将一级缓存与数据库同步有疑问?考虑以下场景

//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)

java hibernate jpa hibernate-session

5
推荐指数
1
解决办法
3300
查看次数

Hibernate抛出期待的OPEN,发现'+'

下面有我的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中支持?怎么解决这个问题?

java hibernate

5
推荐指数
2
解决办法
1万
查看次数

字符串hashCode()文档与实现

下面是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)

java string hashcode java-8

5
推荐指数
2
解决办法
126
查看次数

由多个线程访问synchronizedList

我有一个非常基本的问题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 multithreading

4
推荐指数
1
解决办法
741
查看次数