Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如果返回值是作为参数提供的对象,那么使用函数的返回是否是一种良好的做法?_Java_Performance_Hashmap_Theory - Fatal编程技术网

Java 如果返回值是作为参数提供的对象,那么使用函数的返回是否是一种良好的做法?

Java 如果返回值是作为参数提供的对象,那么使用函数的返回是否是一种良好的做法?,java,performance,hashmap,theory,Java,Performance,Hashmap,Theory,我只是想问,这是一个好的java实践,还是有更好的方法(官方方法)来做同样的事情 首先,我需要更新一些hashmap信息: Map<Date, Object> rows = new HashMap<Date, Object>(); 而且 private HashMap<Date, Object> performQuery1(rows, foo, bar, Date, Date) { // Some code that adds or removes el

我只是想问,这是一个好的java实践,还是有更好的方法(官方方法)来做同样的事情

首先,我需要更新一些hashmap信息:

Map<Date, Object> rows = new HashMap<Date, Object>();
而且

private HashMap<Date, Object> performQuery1(rows, foo, bar, Date, Date) {
  // Some code that adds or removes elements from the hashmap "rows"
  rows.put(date1, o1);

  //Then return the same object
  return rows;
}

是不是?

问题的核心归结为

此外,您应该返回新创建的对象或修改参数引用的对象,但不能同时返回和修改这两个对象。有一些有用的例外 喜欢,但这里似乎不是这样


当您从方法返回数据时,程序员希望数据是新创建的对象,而通过参数引用的对象保持不变。让方法返回void(或成功代码)提示程序员,该方法修改参数引用的对象,而不是返回新对象,并使代码更易于阅读。

问题确实非常广泛,或者,集中在“最佳实践”部分,可能基于观点,但主要不是基于观点,因为这样的模式有一个有效的参数

通常,您有一个方法可以在某处获取数据,并将其放入目标数据结构中(在您的情况下,可能是一个集合或一个映射)

这种方法的签名有几个选项(大致与您的示例相同,但这种模式可以推广)

第一个可能是

/**
 * Computes ... something, and returns the result as a map
 */
Map<Date, Result> execute(Date d0, Date d1) { ... }
/**
 * Computes ... something, and places the results into the
 * given map
 */
void execute(Date d0, Date d1, Map<Date, Result> results)  { ... }

Map结果=
执行(d0,d1,新LinkedHashMap());
分别

  • 您不必为每个调用创建新映射。例如,您可以创建一系列调用

    Map<Date, Result> results = new HashMap<Date, Result>();
    execute(d0, d1, results);
    execute(d2, d3, results);
    
    Map results=newhashmap();
    执行(d0,d1,结果);
    执行(d2、d3、结果);
    
    在给定的映射中累积结果


  • 当考虑到这种方法可以简单地模拟两种备选方案时,这种方法的威力可能会变得更加明显:

    class DB {
    
        // The private method that can emulate both public methods:
        private Map<Date, Result> executeImpl(
            Date d0, Date d1, Map<Date, Result> results);
    
        // The implementation that returns a new map
        public Map<Date, Result> execute(Date d0, Date d1) {
            return executeImpl(d0, d1, null);
        }
    
        // The implementation that fills a given map
        public void execute(Date d0, Date d1, Map<Date, Result> results) {
            executeImpl(d0, d1, results);
        }
    
    }
    
    classdb{
    //可以模拟两个公共方法的私有方法:
    私有映射executeImpl(
    日期d0、日期d1、地图结果);
    //返回新映射的实现
    公共地图执行(日期d0,日期d1){
    返回executeImpl(d0,d1,null);
    }
    //填充给定映射的实现
    公共作废执行(日期d0、日期d1、地图结果){
    executeImpl(d0,d1,结果);
    }
    }
    

    旁白:在JavaSDK的某些地方也使用了类似的模式。例如,在不同的应用程序案例中:

    BufferedImage过滤器(BufferedImage src,BufferedImage dest)

    。。。如果目标映像为空,则创建具有适当颜色模型的BuffereImage

    返回:已筛选的BuffereImage


    这个问题太宽泛了,没有好的答案。但是,在这种情况下,返回
    HashMap
    是完全不必要的。因此,它将不是“rows=performQuery1(…)”而是“performQuery1(…)”,performQuery1将是“private void”,对吗?这不是一个问题吗?不是对您确切问题的回答,但在映射中使用复杂对象作为键是一种糟糕的做法。请看。我认为java不会传递参数reference@AleksandrM谢谢你指出我在回答中遗漏的区别。我在回答中把这个问题连在一起。我仍然认为答案的重要部分(您应该将引用作为参数传递或返回新对象)是正确的。如果不是这样,请让我知道。哇!!!非常感谢你,马可!答案很好,而且肯定比我的好:-)我想补充一点,你的组合选项有一个缺点,就是有点反直觉,在不阅读文档的情况下很难正确阅读和使用,这在很多情况下可能会超过它的好处。
    /**
     * Computes ... something, and places the results into the
     * given map, which is then returned. If the given map is
     * null, then a new map will be created and returned.
     */
    Map<Date, Result> execute(
        Date d0, Date d1, Map<Date, Result> results)  { ... }
    
    Map<Date, Result> results = execute(d0, d1, null);
    
    Map<Date, Result> results = 
        execute(d0, d1, new HashMap<Date, Result>());
    
    Map<Date, Result> results = 
        execute(d0, d1, new LinkedHashMap<Date, Result>());
    
    Map<Date, Result> results = new HashMap<Date, Result>();
    execute(d0, d1, results);
    execute(d2, d3, results);
    
    class DB {
    
        // The private method that can emulate both public methods:
        private Map<Date, Result> executeImpl(
            Date d0, Date d1, Map<Date, Result> results);
    
        // The implementation that returns a new map
        public Map<Date, Result> execute(Date d0, Date d1) {
            return executeImpl(d0, d1, null);
        }
    
        // The implementation that fills a given map
        public void execute(Date d0, Date d1, Map<Date, Result> results) {
            executeImpl(d0, d1, results);
        }
    
    }