小编sch*_*der的帖子

为什么java8的foreach循环中的变量应该是final?

我在Java 7循环和Java 8 forEach循环中迭代数组列表.Java 8循环希望循环内的变量是最终的.例如,

List<String> testList = Arrays.asList( "apple", "banana", "cat", "dog" );
int count = 0;

testList.forEach(test -> {
  count++; // Compilation error: Local variable count defined in an enclosing scope must be final or effectively final
});

for (String test : testList) {
  count++; // Code runs fine
}
Run Code Online (Sandbox Code Playgroud)

有人可以解释为什么会这样吗?这是Java 8的缺点吗?

lambda java-8

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

如何为名称中带有" - "(连字符)的工件添加"要求"

我在Maven pom.xml中包含了这些依赖项:

 <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>${httpclient.version}</version>
</dependency>

<dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

我试图在module-info.java中添加这种依赖,如下所示:

module io.github.wildcraft.restclient {
    requires httpcore; // no compilation problem
    requires httpclient; // no compilation problem
    requires commons-io; // shows compilation error
}
Run Code Online (Sandbox Code Playgroud)

对于commons-io,我收到编译错误.我怎样才能做到这一点?

java maven java-platform-module-system java-9 java-module

21
推荐指数
2
解决办法
1160
查看次数

使用Spring MVC将文件上载到服务器目录

我正在尝试从客户端计算机上传文件到服务器目录.我使用以下代码:

FileUpload.jsp

<form:form commandName="fileUpload" action="upload.action" method="post"  enctype="multipart/form-data">
<form:label path="fileData">Upload a File</form:label> <br />
<form:input type="file"  path="fileData" />
<input type="submit" value="upload" >
</form:form>
Run Code Online (Sandbox Code Playgroud)

在我的控制器中:

@RequestMapping("/upload.action")
public String upload(@ModelAttribute("fileUpload") FileUpload fileUpload,HttpServletResponse response,Model model)
{
    CommonsMultipartFile multipartFile = fileUpload.getFileData();
    String orginalName = multipartFile.getOriginalFilename();
    String filePath = "/my_uploads/"+orginalName;
    File destination = new File(filePath);
    String status ="success";
    try {
        multipartFile.transferTo(destination);
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        status="failure";
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        status="iofailure";
    } …
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc

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

按VPC ID过滤LoadBalancer

我有两个负载均衡器与两个不同的VPC相关联.

我可以使用AWS CLI列出所有负载均衡器.

现在我需要检索特定VPC中的负载均衡器的名称.是否可以通过AWS CLI完成?

amazon-web-services aws-cli

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

比较Java中的两个对象

我有两个不同的同一实体"社区"对象

两个对象(社区和com)具有相同的值

Communty.java有以下变量:

   private Integer communityId;
   private String communityName;
   private String description;

   // many to many relationship
   private Set<Faculty> faculties = new HashSet<Faculty>();
   private Set<User> users = new HashSet<User>();
Run Code Online (Sandbox Code Playgroud)

我用同样的方法:

@Override
   public boolean equals(Object obj) {
          // TODO Auto-generated method stub
          if(obj==null)
                 return false;
          if(obj==this)
                 return true;
          if(!(obj instanceof Community)) return false;

          Community community = (Community)obj;
          return community.getCommunityId() == this.getCommunityId();
   }
Run Code Online (Sandbox Code Playgroud)

当我检查时community==com,它返回false ..为什么?我做错了什么?从数据库中检索这两个对象!

java

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