小编pet*_*ter的帖子

Java:通用方法和返回类型

任何人都可以在下面的代码中解释返回类型的含义

public static <T> ArrayList<T> a()
{       
       return null;
} 
Run Code Online (Sandbox Code Playgroud)

public static <String> ArrayList<Vector> a()
{       
       return null;
} 
Run Code Online (Sandbox Code Playgroud)

java generics

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

Java:只有静态的泛型方法吗?

我想知道我们只有在方法是静态的时候才使用泛型方法吗?对于非静态的,您将定义一个泛型类,并且您不必将它作为泛型方法.那是对的吗 ?

例如,

  public class Example<E>{

         //this is suffice with no compiler error
         public void doSomething(E [] arr){
                for(E item : arr){
                    System.out.println(item);
                }
         }

         //this wouldn't be wrong, but is it necessary ?
         public <E> doSomething(E [] arr){
                for(E item : arr){
                    System.out.println(item);
                }
         }
  }
Run Code Online (Sandbox Code Playgroud)

而编译器将强制添加类型参数,使其成为通用方法,如果它是静态的.

  public static <E> doSomething(E [] arr){


  }
Run Code Online (Sandbox Code Playgroud)

我不确定我是否正确.

java generics

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

如何使非同步数据结构线程安全

我想知道是否还有其他方法可以使非同步数据结构成为线程安全的,除了使用synchronizedHashtable和之类的数据结构Vector,或者使用像Collections.synchronizedList(List<T> arg)或的包装器Collections.synchronizedMap(Map<K,V> arg)

有人问我在采访中了解到如何使一个HashMap线程安全的,我告诉他使用HashtableConcurrentHashMap或使用Collections.synchronizedMap的包装,但是,好像这些答案是不是他正在寻找

java synchronized thread-safety

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

Java平台库Hashcode

在Effective Java Item 9中(覆盖hashCode时始终覆盖equals)它所说的位置

Java平台库中的许多类(如String,Integer和Date)在其规范中包含hashCode方法返回的实际值作为实例值的函数.这通常不是一个好主意,因为它严重限制了您在将来的版本中改进哈希函数的能力.

这是什么意思 ?

java hashcode

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

Autowired 为空且不适用于 Jersey + Spring

我无法弄清楚为什么 My Jersey RESTful 入口点找不到我在应用服务器启动时配置的 Spring Bean。尝试后,它不断收到 NullPointerException

网页.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener> 

<servlet>
    <servlet-name>jersey-serlvet</servlet-name>
    <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.testing.resource</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>jersey-serlvet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)

弹簧上下文.xml

<context:annotation-config />
<context:component-scan base-package="com.testing.config, com.testing.repository, com.testing.workflow" />

<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>classpath:jdbc.properties</value>
    </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

Jersey servlet 入口点

@Component
@Produces(MediaType.APPLICATION_JSON)
@Path("/{userId}/items")
public class …
Run Code Online (Sandbox Code Playgroud)

java spring dependency-injection jersey mybatis

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

如何在Maven pom.xml上可变化shell脚本输出以使用

我想知道是否有一种方法可以使maven执行可卷曲资源并使响应可用的shell脚本,例如maven可以引用的环境变量或全局变量,或者可以这样做Groovy吗?

因此,当我进行Maven构建时,我想执行此Shell脚本。脚本本身将使curl变为某些资源URI并输出响应(我们可能不得不等待它返回),并且maven或Groovy可以某种方式获取该curl响应并将其用于设置某些配置。

bash shell groovy maven-plugin maven

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

在什么情况下你想为你的随机数生成器设置不同的种子?

我想知道你在什么情况下想要为你的随机数发生器设置不同的种子.我看到代码在某个地方人们Random每次需要时创建一个对象,并且看到人们有时会将它作为在构造对象时使用单个种子的实例变量.例如

// same seed
class A {
    private Random random;
    public A() { random = new Random(); }
    public int nextInt() { return random.nextInt(10000); }
    public double nextDouble() { return random.nextDouble(); }
}
Run Code Online (Sandbox Code Playgroud)

// different seed is used every time when you call the method
class B {
    public int nextInt() { return new Random().nextInt(10000); }
    public double nextDouble() { return new Random().nextDouble(); }
}
Run Code Online (Sandbox Code Playgroud)

请忽略这两个包装类的设计,只是试图显示差异.

java random probability

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

组合总和内循环和外循环的动态编程可互换吗?

对于组合总和的动态编程解决方案,我有些困惑,您会得到一个数字列表和一个目标总计,并且您希望计算可以用多少种方法来求和该目标总和。数字可以重复使用多次。我对内循环和外循环是否可互换感到困惑。有人可以解释以下两者之间的区别吗?在哪种情况下,我们将使用其中一个而不使用另一个,或者它们是相同的。

int [] counts = new int[total];
counts[0] = 1;

// (1) 
for(int i = 0; i <= total; i++) {
   for(int j = 0; j < nums.length; j++) {
       if(i >= nums[j])
          counts[i] += counts[i - nums[j]];
   }
}

// (2)
for(int j = 0; j < nums.length; j++)
   for(int i = nums[j]; i <= total; i++) {
       counts[i] += counts[i - nums[j]];
   }
}
Run Code Online (Sandbox Code Playgroud)

java algorithm dynamic-programming

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

ATM如何在并发环境中工作

一个简单的例子:

      class Account{
           private String account_name;
           private String password;
           private double balance;

           public synchronized double getBalance(){
                  return balance;
           }
           public synchronized void setBalance(double add){
                  balance += add;
           }
      }
Run Code Online (Sandbox Code Playgroud)

根据我的理解,获取与对象关联的锁并不会阻止其他线程访问该对象。它们必须是相同的锁以防止访问。

因此,如果两个人尝试在不同的 ATM 上访问同一个帐户,那么它将创建此Account对象的两个不同实例,对吗?那么它不是用相同的锁保护的,对吗?

假设人 A(线程 A)试图将钱存入账户,而同时人 B(线程 B)试图获得账户的总余额。

它是如何工作的 ?他们是否缓存Account正在使用的时间,以便Account在下一个请求到来时返回相同的对象?

java concurrency multithreading

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

Spring安全性无效的登录处理

我是Spring安全的新手,因此我尝试使用Spring MVC来处理无效登录,但最终却找不到Page Not Found 404

在我的security-context.xml中,我有这个AuthenticationProvider处理所有身份验证登录,所以基本上只是检查用户的帐户和密码,但由于某些原因,authentication-failure-url只要有无效的登录尝试,它就会一直说找不到404.

  <security:authentication-manager alias="authenticationManager">
    <security:authentication-provider ref="AuthenticationProvider"/>
  </security:authentication-manager>

  <bean id="preAuthenticationFilter"
      class="authentication.PreAuthenticationFilter"
      p:authenticationManager-ref="authenticationManager" />

  <security:http auto-config="true">
    <security:intercept-url pattern="/member/**" access="MEMBER" requires-channel="https"/>
    <security:form-login login-page="/login"
                         username-parameter="email"
                         password-parameter="password"
                         default-target-url="/member/"
                         authentication-failure-url="/loginfailed" />
    <security:custom-filter position="PRE_AUTH_FILTER" ref="preAuthenticationFilter" />
  </security:http>
Run Code Online (Sandbox Code Playgroud)

但我确实有一个相应的控制器侦听该url模式以处理无效登录.

@Controller
public class LoginController
{
    @RequestMapping(value = "/loginfailed", method = RequestMethod.GET)
    public String loginError(ModelMap model)
    {     
         model.addAttribute("error", "true");
         return "login";
    }
}
Run Code Online (Sandbox Code Playgroud)

****更新*****

在某些时候,我会AuthenticationProvider验证用户并在用户拥有错误凭据时抛出异常(我不知道这是否重要)

@Component
public class AuthenticationProvider{
    private User validateUser(String userName, …
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc spring-security

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