在不使用集合作为返回类型的情况下从java函数中重新调整多个值?

在不使用集合作为返回类型的情况下从java函数中重新调整多个值?,java,function,Java,Function,我有这样一种方法: public boolean isDocumentsDeletedFormDB(String strDocumentsID) { } public class MyPOJO{ private boolean booleanValue; private Integer integerValue; public void setBooleanValue(boolean booleanValue){ this.booleanValue

我有这样一种方法:

public boolean isDocumentsDeletedFormDB(String strDocumentsID) {
}
public class MyPOJO{
    private boolean booleanValue;
    private Integer integerValue;


    public void setBooleanValue(boolean booleanValue){
        this.booleanValue = booleanValue
    }

    public void setIntegerValue(Integer integerValue){
        this.integerValue = integerValue
    }

    public boolean getBooleanValue(){
        return this.booleanValue;
    }

    public Integer getIntegerValue(){
        return this.integerValue;
    }
}
public MyPOJO isDocumentsDeletedFormDB(String strDocumentsID){
    /*
    * Your logic
    */

    MyPOJO pojo = new MyPOJO();
    pojo.setIntegerValue(/* Your integer value */);
    pojo.setBooleanValue(/* Your boolean value */);

    return pojo;
}
在这个方法中,我还想返回一个
整数
值。由于已经有一个返回类型为
Boolean
,如何也返回
Integer

将Wrapper
Integer
作为协议传递也不能解决我的问题


任何人都可以在返回类型中建议不使用
集合的解决方案。

您不能从Java中的函数返回多个值。您必须使用集合或类或结构来包含这两个值。

您不能从Java中的函数返回多个值。您必须使用集合或类或结构来包含这两个值。

您必须定义一个包含
布尔值和
整数字段的对象并返回该对象,或者,如果您可以使用外部库,您可以查看Apache Commons提供的类。

您必须定义一个包含
布尔值
整型
字段的对象并返回该对象,或者,如果您可以使用外部库,您可以查看Apache Commons提供的类。

创建一个普通的旧Java对象(POJO)类并返回该类的一个实例。您的POJO类可以是这样的:

public boolean isDocumentsDeletedFormDB(String strDocumentsID) {
}
public class MyPOJO{
    private boolean booleanValue;
    private Integer integerValue;


    public void setBooleanValue(boolean booleanValue){
        this.booleanValue = booleanValue
    }

    public void setIntegerValue(Integer integerValue){
        this.integerValue = integerValue
    }

    public boolean getBooleanValue(){
        return this.booleanValue;
    }

    public Integer getIntegerValue(){
        return this.integerValue;
    }
}
public MyPOJO isDocumentsDeletedFormDB(String strDocumentsID){
    /*
    * Your logic
    */

    MyPOJO pojo = new MyPOJO();
    pojo.setIntegerValue(/* Your integer value */);
    pojo.setBooleanValue(/* Your boolean value */);

    return pojo;
}
您需要更改方法签名,并执行以下操作:

public boolean isDocumentsDeletedFormDB(String strDocumentsID) {
}
public class MyPOJO{
    private boolean booleanValue;
    private Integer integerValue;


    public void setBooleanValue(boolean booleanValue){
        this.booleanValue = booleanValue
    }

    public void setIntegerValue(Integer integerValue){
        this.integerValue = integerValue
    }

    public boolean getBooleanValue(){
        return this.booleanValue;
    }

    public Integer getIntegerValue(){
        return this.integerValue;
    }
}
public MyPOJO isDocumentsDeletedFormDB(String strDocumentsID){
    /*
    * Your logic
    */

    MyPOJO pojo = new MyPOJO();
    pojo.setIntegerValue(/* Your integer value */);
    pojo.setBooleanValue(/* Your boolean value */);

    return pojo;
}
创建一个普通的旧Java对象(POJO)类并返回该类的一个实例。您的POJO类可以是这样的:

public boolean isDocumentsDeletedFormDB(String strDocumentsID) {
}
public class MyPOJO{
    private boolean booleanValue;
    private Integer integerValue;


    public void setBooleanValue(boolean booleanValue){
        this.booleanValue = booleanValue
    }

    public void setIntegerValue(Integer integerValue){
        this.integerValue = integerValue
    }

    public boolean getBooleanValue(){
        return this.booleanValue;
    }

    public Integer getIntegerValue(){
        return this.integerValue;
    }
}
public MyPOJO isDocumentsDeletedFormDB(String strDocumentsID){
    /*
    * Your logic
    */

    MyPOJO pojo = new MyPOJO();
    pojo.setIntegerValue(/* Your integer value */);
    pojo.setBooleanValue(/* Your boolean value */);

    return pojo;
}
您需要更改方法签名,并执行以下操作:

public boolean isDocumentsDeletedFormDB(String strDocumentsID) {
}
public class MyPOJO{
    private boolean booleanValue;
    private Integer integerValue;


    public void setBooleanValue(boolean booleanValue){
        this.booleanValue = booleanValue
    }

    public void setIntegerValue(Integer integerValue){
        this.integerValue = integerValue
    }

    public boolean getBooleanValue(){
        return this.booleanValue;
    }

    public Integer getIntegerValue(){
        return this.integerValue;
    }
}
public MyPOJO isDocumentsDeletedFormDB(String strDocumentsID){
    /*
    * Your logic
    */

    MyPOJO pojo = new MyPOJO();
    pojo.setIntegerValue(/* Your integer value */);
    pojo.setBooleanValue(/* Your boolean value */);

    return pojo;
}

您可以编写一个泛型Pair类并返回Pair。但是,我想您也应该考虑更改方法名,因为每个人都希望名为isSomething()的方法只返回布尔值。

您可以编写一个泛型Pair类并返回Pair。但是,我想您也应该考虑更改方法名,因为每个人都希望名为isSomething()的方法只返回布尔值。

返回多个元素的标准方法是返回封装这些元素的对象

例如:

public static class DeletedFromDBReturn {
  public final int integerValue;
  public final bool booleanValue;

  private DeletedFromDBReturn(int i, bool b) {
    integerValue = i; booleanValue = b;
  }
}

DeletedFromDBReturn sDocumentsDeletedFormDB(String strDocumentsID) {
  // ...
  return new DeletedFromDBReturn(intVal, boolVal);
}

返回多个元素的标准方法是返回封装这些元素的对象

例如:

public static class DeletedFromDBReturn {
  public final int integerValue;
  public final bool booleanValue;

  private DeletedFromDBReturn(int i, bool b) {
    integerValue = i; booleanValue = b;
  }
}

DeletedFromDBReturn sDocumentsDeletedFormDB(String strDocumentsID) {
  // ...
  return new DeletedFromDBReturn(intVal, boolVal);
}

首先我必须说你的方法有问题一个方法只能做一件事,而您的方法只能做两件事(计算您需要返回的两个值)。正确的解决方案是创建另一个方法,该方法的目的是返回其中一个值,而另一个值由原始方法返回

无论如何,如果你真的需要这样做,你有一些选择:

  • 创建一个包装类,其中包含作为私有字段的值,并从方法返回这个新类的值
  • 提取一个返回的对象作为字段变量,该变量将被其他需要的方法读取(如果只有私有方法需要)
  • 如果返回的类型之一为错误状态,请使用异常

  • 首先我必须说你的方法有问题一个方法只能做一件事,而您的方法只能做两件事(计算您需要返回的两个值)。正确的解决方案是创建另一个方法,该方法的目的是返回其中一个值,而另一个值由原始方法返回

    无论如何,如果你真的需要这样做,你有一些选择:

  • 创建一个包装类,其中包含作为私有字段的值,并从方法返回这个新类的值
  • 提取一个返回的对象作为字段变量,该变量将被其他需要的方法读取(如果只有私有方法需要)
  • 如果返回的类型之一为错误状态,请使用异常

  • 您可以做的是将返回类型更改为
    int
    。返回
    -1
    如果
    false
    否则返回
    有效整数。


    从方法名中,我可以猜到您使用
    布尔
    返回类型来检查文档是否被删除。要返回的
    Integer
    将是
    已删除的文档数

     public int isDocumentsDeletedFormDB(String strDocumentsID) {}
    
    因此,您可以签入使用此函数作为

       if(isDocumentsDeletedFormDB(str)==-1)
        //Do that you do when it is false.
    

    您可以做的是将返回类型更改为
    int
    。返回
    -1
    如果
    false
    否则返回
    有效整数。


    从方法名中,我可以猜到您使用
    布尔
    返回类型来检查文档是否被删除。要返回的
    Integer
    将是
    已删除的文档数

     public int isDocumentsDeletedFormDB(String strDocumentsID) {}
    
    因此,您可以签入使用此函数作为

       if(isDocumentsDeletedFormDB(str)==-1)
        //Do that you do when it is false.
    

    然后,您可能希望使用一个简单的字符串,它可以返回这两种类型,但以字符串形式。
    在调用代码中,只需检查它是否为(“true”或“false”),并相应地对其进行解析。

    您可能需要使用一个简单的字符串,它可以返回这两种类型,但采用字符串形式。
    在调用代码中,只需检查它是否为(“true”或“false”),并对其进行相应的解析。

    根据标准编程实践,您必须重新设计程序,以避免返回两个值的必要性。整数的作用是什么?也许这会带来一些更好的想法。我想以整数值的形式返回成功删除的文件数。根据标准编程实践,您必须重新设计程序,以避免返回两个值的必要性。整数应该执行什么操作?也许这会带来一些更好的想法。我想以整数值的形式返回成功删除的文件数。这是旧的C实践,在Java中没有好的编程风格。但是我们不知道从t返回两个值的目的是什么