Java如何使用多个参数函数将List转换为Map

时间:2015-11-27 11:42:15

标签: java

我想使用流和收集器将List转换为Map。我已经搜索了很多例子,但我无法弄清楚如何转换以下示例:

foo(3) = {
    "a": "aaa",
    "b": "bbb",
    "c": "ccc",
    "d": "ddd"
};

所以结果应该是:

void Map<String, String> foo(int repeatCount) {
    return list.stream().collect(Collectors.toMap(
            a -> repeatAlphabet(a, repeatCount),
            Function.identity()));
}

这是我尝试过的:

Wrong first argument found: <lambda parameter>, required: Java.Lang.String

但它显示错误:

function getOrdem($id){
     return $this->db->select('*')

          ->join('users', 'users.id = ordens.idCriadorOs AND users.id = ordens.idFuncionarioOs', 'inner')
          ->join('cargos', 'cargos.idCargo = ordens.idFuncionarioOs', 'inner')
          ->join('empresas', 'empresas.idEmpresa = ordens.idClienteOs', 'inner')
          ->get_where('ordens', array('idOs' => $id))
          ->row_array();

2 个答案:

答案 0 :(得分:0)

如果我理解你的问题,你需要返回的只是:

<WebMethod()> _
Public Function DailyCheckDealer(records As String()()) As List(Of Dealer)

' code left out

Dim myConnection2 = New SqlConnection(myConnString)
Dim objComm As New SqlCommand("Select IDNo, IDFound, POFound From DailyDealerCheck Order By IDNo", myConnection2)
' Create list of Dealers for return
Dim dealerList as New List(Of Dealer)

myConnection2.Open()
Dim sdr As SqlDataReader = objComm.ExecuteReader()
If sdr.Read() Then
    objDealer.IDNo = sdr("IDNo").ToString()
    objDealer.ICFound = sdr("IDFound").ToString()
    objDealer.POFound = sdr("POFound").ToString()
    ' Add the latest to the list
    dealerList.Add(objDealer)
End If
myConnection2.Close()

' Return list of dealers
Return dealerList
End Function

这会返回return list.stream().collect(Collectors.toMap(s->s,s->repeatAlphabet(s, repeatCount))); ,其关键字是Map的元素,值是重复的List

P.S。我假设String应该返回repeatAlphabet。我想这是你问题中的拼写错误(未指定返回类型)。

答案 1 :(得分:0)

我仍然无法评论,所以我必须使用答案。 @Eran的回答是对的, 也许你的问题是方法签名有两种返回类型。 问题是:

void Map<String, String> foo(int repeatCount)

但正确的是:

Map<String, String> foo(int repeatCount)