簡體   English   中英

如何從Java中的函數返回通用Collection?

[英]How to return generic Collection from a function in Java?

因此,我可以遍歷這段代碼,這是Java中通用方法的演示:

public static <T> T addAndReturn(T element, Collection<T> collection){
    collection.add(element);
    return element;
}

....

String stringElement = "stringElement";
List<String> stringList = new ArrayList<String>();
String theElement = addAndReturn(stringElement, stringList); 

但是,我不想返回元素,而是要發回整個集合對象 我嘗試了很少的給定返回類型作為Collection的方法,但是它不起作用,

public static <T> Collection<T> addAndReturn(T element, Collection<T> collection) {
        collection.add(element);
        System.out.println(collection);
        return collection;
    }

    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
        String stringElement = "stringElement";
        List<String> strList = new ArrayList<String>();

        ArrayList<String> strRes = addAndReturn(stringElement, strList);
        System.out.println(strRes);
    }

並收到此錯誤:

Main.java:22:錯誤:不兼容的類型:不存在類型變量T的實例,因此Collection符合ArrayList ArrayList strRes = addAndReturn(stringElement,strList); ^其中T是類型變量:T擴展在方法addAndReturn(T,Collection)中聲明的對象1錯誤

誰能幫助我解決這個問題?

代碼示例來自Jenkov.com

根據OP的評論進行編輯:必須理解ArrayList從Collection派生的。 因此,當方法的接口返回Collection<T> ,當然,您只能將結果分配給Collection ,而不能分配給List / ArrayList。 您不知道傳入List實例的事實。 編譯器只會看到Collection回來了!

回到第一個問題:您的代碼正在返回添加的元素:

public static <T> T addAndReturn(T element, Collection<T> collection){
    collection.add(element);
    return element;
}

只需更改簽名和返回的內容:

public static <T> Collection<T> addAndReturn(T element, Collection<T> collection){
    collection.add(element);
    return collection;
}

做完了嗎 並不是的。

如:這不是一個好習慣。 返回以參數形式出現的內容會很快導致代碼閱讀者感到困惑。 混淆讀者是一件壞事

除此之外:整個方法本身就是偽造的 ,因為方法的調用者也可以這樣寫:

thatCollection.add(thatElement);

他們自己。 您的方法不會為上述方法增加任何價值! 對於讀者而言,這相當模糊。

為什么有人要寫:

addAndReturn(someX, listOfXes);

代替

listOfXes.add(someX);

如果要這樣做:

addAndReturn(someX, listOfXes).addAndReturn(someOtherX, listOfXes);

您寧願去:

listOfXes.addAll(Arrays.asList(someX, someOtherX));

例如。 不要為可以使用標准庫調用處理的事情發明“實用程序”。

這是你想要的?

public static <T> Collection<T> addAndReturn(T element, Collection<T> collection) {
    collection.add(element);
    return collection;
}

就像下面的清單一樣。

public static <T> Collection<T> addAndReturn(final T element, final Collection<T> collection) {
    collection.add(element);
    return collection;
}

@Test
public void test() {
    final String stringElement = "stringElement";
    final List<String> stringList = new ArrayList<String>();
    final List<String> nextList = (List<String>) addAndReturn(stringElement, stringList);
    assertThat(nextList.get(0), is(equalTo(stringElement)));
}

您只需要關心正確的返回參數Collection<T> 如果您想從子類或接口(如List final List<String> nextList = (List<String>) addAndReturn(stringElement, stringList);

錯誤消息指向此行:

 ArrayList<String> strRes = addAndReturn(stringElement, strList); 

它告訴您(使用非Compilerese普通英語),它找不到允許該分配發生的通用類型。

您的方法返回Collection<T> (在本例中為Collection<String> )。 Collection<String>不是ArrayList<String>的子類型,因此您不允許將Collection<String>類型的對象分配給ArrayList<String>類型的字段。 您的方法返回Collection<String> ,因此最簡單的解決方法是將其分配給Collection<String>類型的字段:

Collection<String> strRes = addAndReturn(stringElement, strList);

或者,您可以使用局部變量類型推斷(假設您使用的是Java的當前版本):

var strRes = addAndReturn(stringElement, strList);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM