如何展平包含列表的对象列表并将每个元素传递给方法?

时间:2019-07-08 20:46:39

标签: java java-stream

给出以下实体:

def Ciphertext(message):
    cipher = ""
    for i in message:
        #determine if this is an uppercase or lowercase character, and treat it accordingly
        number_code = 0
        if i.isupper():
            number_code = (((ord(i) - 65) + 3) % 26) + 65
        elif i.islower():
            number_code = (((ord(i) - 97) + 3) % 26) + 97
        else:
            number_code = ord(i)
        letter_code = chr(number_code)
        cipher += letter_code
    return cipher

def Plaintext(cipher):
    text = ""
    for i in cipher:
        unencrypted_code = 0
        if i.isupper():
            unencrypted_code = (((ord(i) - 65) - 3) % 26) + 65
        elif i.islower():
            unencrypted_code = (((ord(i) - 97) - 3) % 26) + 97
        else:
            unencrypted_code = ord(i)
        letter_code = chr(unencrypted_code)
        text += letter_code
    return text

print (Ciphertext("Hello world! wxyz"))
print (Plaintext("Khoor zruog! zabc"))

我正在尝试学习如何使用Java 8流API来使public class Parent extends { private Long id; private List<Child> children = new ArrayList<>(); } 扁平化,每个List<Parent>包含一个List<Child>并将Child的流传递给void方法。

例如:

List<Parent> parents = //..

schedules.stream()
        .flatMap(Parent::getChildren)
        .forEach(this::invoke);

出现错误:

  

方法参考中的错误返回类型:无法将java.util.List 转换为java.util.stream.Stream <?扩展R>

1 个答案:

答案 0 :(得分:1)

Stream.flatMap()需要一个SELECT S.Name, Result.Id, Longitud, Latitud, Trandate, EndTrandate, Salesman, TranId, ClientId, TranType, TranStatus, Result.Division_Id, UniqueTransactionId, Result.RecordId FROM (SELECT ROW_NUMBER() OVER (PARTITION BY Salesman ORDER BY EndTranDate DESC) AS RowNum, Id, Longitud, Latitud, Trandate, EndTrandate, Salesman, TranId, ClientId, TranType, TranStatus, Division_Id, UniqueTransactionId, RecordId FROM LocationHistory) AS Result INNER JOIN Salesman S ON Result.Salesman = S.Id WHERE Result.RowNum = 1 ORDER BY Salesman ,它返回一个Function作为参数。您正在尝试传递一个返回Stream的{​​{1}}。改用它:

Function

或者,您可以使用ListList<Parent> parents = ...; parents.stream() .flatMap(p -> p.getChildren().stream()) .forEach(this::invoke);

Stream.map()