如何创建更多条件的Map <integer,list <object =“” >>?

时间:2019-03-29 13:40:33

标签: java java-stream

我有这样的班级:

public Sale(...) {
    public String getField1() {
        return field1;
    }
    public String getField2() {
        return field2;
    }
    public Date getField3() {
        return field3;
    }
    public int getField4() {
        return field4;
    }
}

我有这个清单。

List<Sale> exampleList= new ArrayList(); 

a   x    2005    2
a   x    2005    3
b   y    2003    6
b   y    2004    2
c   x    2006    4
c   x    2006    9
I want to create a Map<List<Sale>, Integer> with java stream

field1 field2 field3 Sum(field4),其中前3个字段相同。

Map<List<Sale>, Integer> target = exampleList.stream().?????

示例:

{a   x    2005}    5
{b   y    2003}    6
{b   y    2004}    2
{c   x    2006}    13

那么如何针对此问题编写正确的流。

1 个答案:

答案 0 :(得分:0)

您应按SqlConnection conn = new SqlConnection(); conn.ConnectionString = "Data Source=LAPTOP-QVQ8QR1I;" + "Initial Catalog=BoneIDa2;" + "Integrated Security=SSPI;"; List<StrukturaObrok> obrok = new List<StrukturaObrok>(); var query = "select u.pin, u.firstname Name, u.lastname Surname, " + "ISNULL(CONVERT(varchar(50), sum(case when tmt.type = 'M1' then tmt.Price end)), '') as M1, " + "ISNULL(CONVERT(varchar(50), sum(case when tmt.type = 'M2' then tmt.Price end)), '') as M2, " + "ISNULL(CONVERT(varchar(50), sum(case when tmt.type = 'M3' then tmt.Price end)), '') as M3, " + "ISNULL(CONVERT(varchar(50), sum(case when tmt.type = 'M4' then tmt.Price end)), '') as M4, " + "(select Price from TA_MealsType Where type = 'M1') M1Cijena, " + "(select Price from TA_MealsType Where type = 'M2') M2Cijena, " + "(select Price from TA_MealsType Where type = 'M3') M3Cijena, " + "(select Price from TA_MealsType Where type = 'M4') M4Cijena, " + "ISNULL(CONVERT(varchar(50), sum(case when tmt.type in ('M1', 'M2') then tmt.Price end)), '') as UkupnoM1_M2, " + "ISNULL(CONVERT(varchar(50), sum(case when tmt.type in ('M3', 'M4') then tmt.Price end)), '') as UkupnoM3_M4 " + "from users u " + "left join TA_Meals tm " + "on u.pin = tm.pin " + "left join TA_MealsType tmt " + "on tm.MealType = tmt.id " + "where u.department = 1000001001 " + "group by u.pin, u.firstname, u.lastName"; SqlCommand SelectCommand = new SqlCommand(query, conn); SqlDataReader myreader; conn.Open(); myreader = SelectCommand.ExecuteReader(); while (myreader.Read()) { try { var ob = new StrukturaObrok( myreader.GetInt32(myreader.GetOrdinal("Pin")), myreader.GetString(myreader.GetOrdinal("Name")), myreader.GetString(myreader.GetOrdinal("Surname")), myreader.GetString(myreader.GetOrdinal("M1")), myreader.GetString(myreader.GetOrdinal("M2")), myreader.GetString(myreader.GetOrdinal("M3")), myreader.GetString(myreader.GetOrdinal("M4")), myreader.GetInt32(myreader.GetOrdinal("M1Cijena")), myreader.GetInt32(myreader.GetOrdinal("M2Cijena")), myreader.GetInt32(myreader.GetOrdinal("M3Cijena")), myreader.GetInt32(myreader.GetOrdinal("M4Cijena")), myreader.GetString(myreader.GetOrdinal("UkupnoM1_M2")), myreader.GetString(myreader.GetOrdinal("UkupnoM3_M4")) ); obrok.Add(ob); } catch (Exception ex) { throw new Exception(ex.Message + ' ' + ex.Source); } } return obrok; 对列表进行分组。为此,您只能为这些字段覆盖类field2, field2, field3中的equals/hashcode(不包含Sale)。然后使用流获取结果

field4