小编bra*_*orm的帖子

如果我在Java中覆盖“等于”方法,为什么需要覆盖哈希码?

我知道只要equals在Java中重写该方法,就需要重写哈希码。那仅仅是合同。我试图了解其背后的逻辑。我正在阅读Joshua Bloch的 * Effective Java ,发现了以下代码(第9页,第45页):

import java.util.HashMap;
import java.util.Map;

public final class PhoneNumber {
    private final short areaCode;
    private final short prefix;
    private final short lineNumber;

    public PhoneNumber(int areaCode, int prefix, int lineNumber) {
        rangeCheck(areaCode, 999, "area code");
        rangeCheck(prefix, 999, "prefix");
        rangeCheck(lineNumber, 9999, "line number");
        this.areaCode = (short) areaCode;
        this.prefix = (short) prefix;
        this.lineNumber = (short) lineNumber;
    }

    private static void rangeCheck(int arg, int max, String name) {
        if (arg < 0 || arg > max) …
Run Code Online (Sandbox Code Playgroud)

java equals object hashcode effective-java

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

在Java的列表界面中需要使用listIterator()和iterator()是什么?

List接口有两个方法listIterator()iterator().为什么这两个都是必需的.

来自文档:

Iterator<E> iterator()

Returns an iterator over the elements in this list in proper sequence.

ListIterator<E> listIterator()

Returns a list iterator over the elements in this list (in proper sequence).

    ListIterator<E> listIterator(int index)

    Returns a list iterator over the elements in this list (in proper sequence), starting at the 
specified position in the list. The specified index indicates the first element that would be 
returned by an initial call to next. An …
Run Code Online (Sandbox Code Playgroud)

java iterator list

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

在Java中为自定义检查异常编写JUnit测试用例?

我正在为我的类编写一个测试用例,其中包含抛出异常的方法(包括checked和runtime).我已尝试过此链接中建议的不同可能的测试方法..看起来它们似乎只适用于运行时异常.对于Checked异常,我需要执行try/catch/assert,如下面的代码所示.有没有替代尝试/ catch /断言/.你会注意到testmethod2() and testmethod2_1()显示编译错误,但testmethod2_2()没有显示使用try/catch的编译错误.

class MyException extends Exception {

    public MyException(String message){
        super(message);
    }
}


public class UsualStuff {

    public void method1(int i) throws IllegalArgumentException{
        if (i<0)
           throw new IllegalArgumentException("value cannot be negative");
        System.out.println("The positive value is " + i );
    }

    public void method2(int i) throws MyException {
        if (i<10)
            throw new MyException("value is less than 10");
        System.out.println("The value is "+ i);
    }

    }
Run Code Online (Sandbox Code Playgroud)

测试类:

import static org.junit.Assert.*;

import org.junit.Before; …
Run Code Online (Sandbox Code Playgroud)

java exception try-catch junit4 checked-exceptions

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

为什么数组被初始化为默认值而不是java中的arraylist?

引擎盖下ArrayList使用的实施Array.但是,Arrays初始化为默认值(0 or null)ArrayList只是空的.为什么是这样?

       int[] arr = new int[10];
       String[] arr1 = new String[11];
       System.out.println(Arrays.toString(arr));
       System.out.println(Arrays.toString(arr1));
      List<Integer> list = new ArrayList<Integer>(10);
      System.out.println(list);

      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
      [null, null, null, null, null, null, null, null, null, null, null]
      []
Run Code Online (Sandbox Code Playgroud)

这意味着我每次使用时ArrayList,都需要填写内容; 我想在我的代码下面的一部分,它被扔NoSuchElementException,然后我意识到,这不是违约,作为地方Arrays

if (list.get(i)==null){
         list.add(i,x);
  else:
        list.add(i,list.get(i)+x)
Run Code Online (Sandbox Code Playgroud)

编辑:

even List<Integer> list = new ArrayList<Integer>(10);
prints [] although I initialized the size;
Run Code Online (Sandbox Code Playgroud)

java arrays list arraylist

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

django中SITE_ID设置的用途是什么?

当我在做平面教程时,我因SITE_ID没有设置而收到错误.我插入SITE_ID=1设置文件,一切正常.但我不知道这究竟意味着什么.

我读完了django docs.但我并不完全清楚它的用途.我什么时候会使用类似SITE_ID = 2的东西.

同样,我在代码中使用了以下代码片段而实际上并不知道它的作用:

current_site=Site.objects.get_current()
Run Code Online (Sandbox Code Playgroud)

我认为这与某些事情有关,SITE_ID但可能不是.

一些示例来说明哪里SITE_ID可能采用不同于1的值会有所帮助.

django django-sites

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

mapreduce作业的map阶段输出总是排序?

我对从Mapper获得的输出有点困惑.

例如,当我运行一个简单的wordcount程序时,使用此输入文本:

hello world
Hadoop programming
mapreduce wordcount
lets see if this works
12345678
hello world
mapreduce wordcount
Run Code Online (Sandbox Code Playgroud)

这是我得到的输出:

12345678    1
Hadoop  1
hello   1
hello   1
if  1
lets    1
mapreduce   1
mapreduce   1
programming 1
see 1
this    1
wordcount   1
wordcount   1
works   1
world   1
world   1
Run Code Online (Sandbox Code Playgroud)

如您所见,mapper的输出已经排序.我根本没跑Reducer.但我发现在另一个项目中,mapper的输出没有排序.所以我对此完全清楚..

我的问题是:

  1. 映射器的输出总是排序吗?
  2. 排序阶段是否已经集成到映射器阶段,以便映射阶段的输出已经在中间数据中排序?
  3. 有没有办法从sort and shuffle阶段收集数据并在它进入Reducer之前保留它?减速器带有一个键和一个迭代列表.有没有办法,我可以保留这些数据吗?

hadoop mapreduce hadoop2

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

组合器在哪里组合映射器输出 - 在映射阶段还是减少Map-reduce工作中的阶段?

我的印象是组合器就像在本地映射任务上执行的reducer一样,即它聚合单个Map任务的结果,以减少输出传输的网络带宽.

从阅读中Hadoop- The definitive guide 3rd edition,我的理解似乎是正确的.

从第2章(第34页)

组合器函数许多MapReduce作业受到群集上可用带宽的限制,因此最大限度地减少map和reduce任务之间传输的数据是值得的.Hadoop允许用户指定要在地图输出上运行的组合器函数 - 组合器函数的输出形成reduce函数的输入.由于组合器函数是一种优化,因此Hadoop无法保证为特定的地图输出记录调用它的次数(如果有的话).换句话说,调用组合器函数零,一次或多次应该从reducer产生相同的输出.

所以我在wordcount问题上尝试了以下内容:

job.setMapperClass(mapperClass);
job.setCombinerClass(reduceClass);
job.setNumReduceTasks(0);
Run Code Online (Sandbox Code Playgroud)

这是柜台:

14/07/18 10:40:15 INFO mapred.JobClient: Counters: 10
14/07/18 10:40:15 INFO mapred.JobClient:   File System Counters
14/07/18 10:40:15 INFO mapred.JobClient:     FILE: Number of bytes read=293
14/07/18 10:40:15 INFO mapred.JobClient:     FILE: Number of bytes written=75964
14/07/18 10:40:15 INFO mapred.JobClient:     FILE: Number of read operations=0
14/07/18 10:40:15 INFO mapred.JobClient:     FILE: Number of large read operations=0
14/07/18 10:40:15 INFO mapred.JobClient:     FILE: Number of write operations=0
14/07/18 10:40:15 INFO …
Run Code Online (Sandbox Code Playgroud)

hadoop mapreduce hadoop2

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

由于facebook范围,无法获得spring-social-showcase-boot工作?

我按照这里提供的说明在我的本地运行spring-social-showcase-boot.

这是我尝试使用Facebook进行身份验证时出现的错误.错误很明显,read_stream范围无效.

在此输入图像描述

但我无法确定示例中scope配置的位置spring-social-showcase-boot.

任何帮助解决这个问题将非常感激.

spring spring-social spring-social-facebook

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

无法在docker容器中编辑/etc/resolv.conf

我想为/etc/resolv.conf我的docker容器添加一个域条目.

这是我的dockerFile

FROM tomcat:8.0.20-jre7
VOLUME /tmp
#RUN sed -i "s|search local|domain com.example.com|g;" /etc/resolv.conf
RUN echo "domain com.example.com" >> /etc/resolv.conf
# Expose ports.
EXPOSE 8080
Run Code Online (Sandbox Code Playgroud)

我想这两个echosed.有sed,我在构建过程中遇到错误.

sed: cannot rename /etc/sed6LcoES: Device or resource busy

但是使用echo容器构建并成功运行.但是,当我进入容器时,我没有看到我的域名被添加/etc/resolv.conf.

为什么不工作?

注意:我dns-search在运行参数期间通过传递工作

docker run -p 8080:8080 --dns-search=com.example.com -d --name myawesome my/myawesome:latest

但我有兴趣让dockerFile工作.

docker dockerfile

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

无法使用 NashornscriptEngine 在 java 8 上执行 es6

我正在尝试在 java 8 (1.8.0_102) 中执行 javascript (ES6) 函数。

这是精简后的 javascript 片段。

 const myfunc = (args) => {

   if (!(args.name || args.zip))
return

  const result = {...args}
  const { name, zip, date } = result

...
}
Run Code Online (Sandbox Code Playgroud)

这是我的java代码

public static Object processArbitraryJavaScript(String params)
    {
        String[] options = new String[] {"--language=es6"};
        NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
        NashornScriptEngine engine = (NashornScriptEngine) factory.getScriptEngine(options);
        Object result = null;
        try
        {
            engine.eval(new FileReader("sample.js"));
            Invocable inv = (Invocable) engine;
            result = inv.invokeFunction("myfunc", params);
        }
        catch (ScriptException scriptException …
Run Code Online (Sandbox Code Playgroud)

java scriptengine java-8 nashorn

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