小编Vin*_*C M的帖子

java.util.HashMap实现中"2的幂"的意义

可能重复:
Java HashMap默认初始容量

我正在java.util.HashMap中阅读HashMap的实现.初始容量,最大容量等是2的幂.

从java.util.HashMap复制的声明部分

/**
 * The default initial capacity - MUST be a power of two.
 */
static final int DEFAULT_INITIAL_CAPACITY = 16;


 /**
 * The maximum capacity, used if a higher value is implicitly specified
 * by either of the constructors with arguments.
 * MUST be a power of two <= 1<<30.
 */
static final int MAXIMUM_CAPACITY = 1 << 30;


/**
 * The table, resized as necessary. Length MUST Always be a power of two.
 */ …
Run Code Online (Sandbox Code Playgroud)

java hashmap

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

有什么类似于Mockito中的Junit设置方法

我有以下场景

 interface DAO
 {
      String a();
      String b();
      String c();

 }
Run Code Online (Sandbox Code Playgroud)

我创建了一个这个DAO接口的模拟器,并将其提供给称为DAOProcess的东西.在DAOProcess中,我有各种方法调用DAO方法a,b和c.

现在每次我需要在DAOProcess中对一个方法进行单元测试时,我最终都会写when(mockDAO.a()).thenReturn("test").

无论如何,我可以将这些when(mockDAO.a()).thenReturn("test")共同点移动到所有测试用例中吗?

java junit mocking mockito

7
推荐指数
2
解决办法
5537
查看次数

覆盖 Clone 而不调用 super.clone

clone不实现 Cloneable 接口和不调用 super.clone() 的情况下重写方法是否是一个好习惯。这样,CloneNotSupportedException就不会抛出异常。

考虑这个类:

class Money {

    private BigDecimal x ;

    public Object clone() {
        Money m1 = new Money();
        m1.setX(this.x);
        return m1;
    }

    public BigDecimal getX() {
        return x;
    }

    public void setX(BigDecimal x) {
        this.x = x;
    }

}        
Run Code Online (Sandbox Code Playgroud)

这个类不会抛出CloneNotSupportedException,它就像一个复制构造函数。

这是一个好方法吗?

java clone

7
推荐指数
2
解决办法
3881
查看次数

安装dm-mysql-adapter时出错

我是Ruby的新手.我安装了DataMapper,我正在尝试安装dm-mysql-adapter-1.0.2 gem.但是当我尝试安装时,我收到以下错误.我正在使用ubuntu OS.

vinoth@vinoth-laptop:~/Downloads$ gem install dm-mysql-adapter-1.0.2 -- --with-mysql-                    lib=/usr/lib/mysql -- --with-mysql-conf=/usr/bin/mysql 
WARNING:  Installing to ~/.gem since /home/vinoth/gems and
/home/vinoth/gems/bin aren't both writable.
WARNING:  You don't have /home/vinoth/.gem/ruby/1.8/bin in your PATH,
gem executables will not run.
Building native extensions.  This could take a while...
ERROR:  Error installing dm-mysql-adapter-1.0.2:
ERROR: Failed to build gem native extension.

/usr/bin/ruby1.8 extconf.rb --with-mysql-lib=/usr/lib/mysql -- --with-mysql-     conf=/usr/bin/mysql
checking for mysql_query() in -lmysqlclient... no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably …
Run Code Online (Sandbox Code Playgroud)

ruby datamapper

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

克隆如何在引擎盖下工作?

Clone不会调用对象构造函数来创建对象的副本.那么克隆使用什么算法呢?

我正在寻找本机方法克隆的实现细节.任何指针将不胜感激.

请注意,我知道克隆的缺点.

java clone

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

为什么使用私有锁而非内部锁?

在阅读有关同步的过程中,我遇到了"监控模式"来封装可变状态.

以下是示例代码

   public class MonitorLock {
      private final Object myLock = new Object();
      Widget widget;
      void someMethod() {
        synchronized(myLock) {
         // Access or modify the state of widget
        }
    }
Run Code Online (Sandbox Code Playgroud)

}

以任何方式拥有私有锁而不是内在锁更好吗?

java synchronization locking

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

有没有人现在使用Java-config的Spring框架?

我正在浏览Spring Java Config页面.

它说

虽然该项目现在应该被视为"退役",但为了后人的利益,此处的资源将无限期地保留:

这是否意味着它已经过时了?现在有人在他们的项目中使用它吗?它是否比传统的xml配置有任何优势?

java spring

6
推荐指数
2
解决办法
824
查看次数

spring安全性中的beans.NotReadablePropertyException

春天安全我是新手.我拿起本书并尝试执行代码.

虽然我这样做,但我得到了

org.springframework.beans.NotReadablePropertyException: Invalid property
'principal.username' of bean class 
[org.springframework.security.authentication.AnonymousAuthenticationToken]: 
Bean property 'principal.username' is not readable or has an invalid getter 
method: 
Does the return type of the getter match the parameter type of the setter?
Run Code Online (Sandbox Code Playgroud)

我的spring-security xml配置:

<http auto-config="true" use-expressions="true">
    <intercept-url pattern="/login.do" access="permitAll"/>
    <intercept-url pattern="/*" access="hasRole('ROLE_USER')"/>
    <form-login login-page="/login.do"/>
</http>


<authentication-manager alias="authenticationManager">

    <authentication-provider>
        <user-service id="userService">
            <user authorities="ROLE_USER" name="guest" password="guest"/>
        </user-service>
    </authentication-provider>
    <!-- Ch 3 Change Password Service -->
    <!-- 
    <authentication-provider user-service-ref="userService"/>
     -->
</authentication-manager>
Run Code Online (Sandbox Code Playgroud)

我错过了什么吗?

如果您需要任何其他信息,请与我们联系.

spring-security

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

使用struts 2标记的列表总长度

我使用Struts 2标签迭代一个列表.

<s:iterator value="popUpScreenDetailsList" status ="rowCounter">
    //do something
<s:/iterator>
Run Code Online (Sandbox Code Playgroud)

有没有办法,我可以通过它获得列表的长度而无需迭代?

这样做的动机是使用javascript来验证复选框.

java struts2

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

查找负载最少的服务器

给定三个服务器A,B,C,其中A可以处理50%的流量,B可以处理30%的流量,C可以处理20%的流量,得出一个公式来有效地分配负载.服务器的当前负载也是该功能的输入.

我无法想出他要求的"公式".这个问题有什么具体的答案吗?

algorithm load-balancing

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