Java方法返回多值(含不同类型)的实现方案咨询
Hey there! Let's work through your problem—Java doesn't let you return multiple values directly with return A, B, C (that's a syntax error), but we have several clean, practical ways to handle this, especially since you need to support two scenarios: returning 3 Strings, or 2 Strings + 1 int.
1. Use a Custom Class (Highly Recommended)
This is the most idiomatic, type-safe approach in Java. Create a dedicated class to hold all possible return values, using nullable types (like Integer instead of int) to handle cases where some values aren't needed.
Step 1: Define the Result Class
class MethodResult { private final String valueA; private final String valueB; private final String valueC; private final Integer numericValue; // Integer allows null for optional use // Constructor for 3 Strings case public MethodResult(String valueA, String valueB, String valueC) { this.valueA = valueA; this.valueB = valueB; this.valueC = valueC; this.numericValue = null; } // Constructor for 2 Strings + 1 int case public MethodResult(String valueA, String valueB, int numericValue) { this.valueA = valueA; this.valueB = valueB; this.valueC = null; // Set to null since we don't need it here this.numericValue = numericValue; } // Getters to access the values public String getValueA() { return valueA; } public String getValueB() { return valueB; } public String getValueC() { return valueC; } public Integer getNumericValue() { return numericValue; } }
Step 2: Update Your Custom Method
Modify cusmethod to return an instance of MethodResult:
private static MethodResult cusmethod(String test) { String a = "A" + test; String b = "B" + test; String c = "C" + test; // Example 1: Return 3 Strings return new MethodResult(a, b, c); // Example 2: Return 2 Strings + 1 int (uncomment this if needed) // int someNumber = 100; // return new MethodResult(a, b, someNumber); }
Step 3: Retrieve Values in Main
public static void main(String[] args) { MethodResult result = cusmethod("string1"); // Fetch string values String a = result.getValueA(); String b = result.getValueB(); String c = result.getValueC(); // Check if a numeric value exists (for the 2 Strings + int case) if (result.getNumericValue() != null) { int num = result.getNumericValue(); System.out.println("Got numeric value: " + num); } }
2. Use a List (Quick but Less Safe)
If you don't want to create a custom class, you can use a List<Object> to hold mixed types. Note that this isn't type-safe—you'll have to cast values manually, which can lead to runtime errors if you're not careful.
Example Code
private static List<Object> cusmethod(String test) { List<Object> result = new ArrayList<>(); String a = "A" + test; String b = "B" + test; // Case 1: Add 3 Strings result.add(a); result.add(b); result.add("C" + test); // Case 2: Add 2 Strings + 1 int (replace above lines with this) // result.add(a); // result.add(b); // result.add(42); return result; } // In main: public static void main(String[] args) { List<Object> result = cusmethod("string1"); String a = (String) result.get(0); String b = (String) result.get(1); // Handle the third element based on its type Object thirdItem = result.get(2); if (thirdItem instanceof String) { String c = (String) thirdItem; } else if (thirdItem instanceof Integer) { int num = (Integer) thirdItem; } }
3. Use a Map (For Named Values)
Another option is a Map<String, Object> where keys label each value (like "a", "b", "c", "number"). This makes it clearer what each value represents, but still has the same type-safety limitations as lists.
Example Code
private static Map<String, Object> cusmethod(String test) { Map<String, Object> result = new HashMap<>(); result.put("a", "A" + test); result.put("b", "B" + test); // Case 1: Add third string result.put("c", "C" + test); // Case 2: Add numeric value (replace above line with this) // result.put("number", 42); return result; } // In main: public static void main(String[] args) { Map<String, Object> result = cusmethod("string1"); String a = (String) result.get("a"); String b = (String) result.get("b"); if (result.containsKey("c")) { String c = (String) result.get("c"); } else if (result.containsKey("number")) { int num = (Integer) result.get("number"); } }
Which Approach Should You Pick?
- Custom Class: Always the best choice for production code. It's type-safe, self-documenting, and easy to maintain as your project grows.
- List/Map: Only use these for quick throwaway scripts or small utility code—avoid them in larger projects due to the risk of casting mistakes.
内容的提问来源于stack exchange,提问作者TTT




