如何使用scala在列表中创建相同元素的组?

时间:2018-01-22 05:55:17

标签: scala

假设我们有一个案例类学生

case class Student(name: String,roll: String)

和学生名单

List(Student("a","123"),Student("b","345"),Student("a","678"),Student("b","321"))

我想制作名称相同的小组,例如

List(Student("a","123"),Student("a","678")) , List(Student("b","345"),Student("b","321"))

我怎么能用scala做到这一点?

3 个答案:

答案 0 :(得分:3)

以下是一种方法:

case class Student(name: String,roll: String)

val list = List(Student("a","123"),Student("b","345"),Student("a","678"),Student("b","321"))

val groupedList = list.groupBy(_.name).values.toList
// groupedList: List[List[Student]] = List(
//   List(Student(b,345), Student(b,321)), List(Student(a,123), Student(a,678))
// )

答案 1 :(得分:1)

如果您正在寻找List[List[Student]],那么您需要将Student分组到列表中

val groups = List( List(Student("a","123"),Student("a","678")), List(Student("b","345"),Student("b","321")) )

如果您正在寻找Map[String, List[Student]]

val groups = Map("a" -> List(Student("a","123"),Student("a","678")), "b" -> List(Student("b","345"),Student("b","321")))

答案 2 :(得分:1)

您可以使用列表中的<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="True"> <ContentTemplate> <asp:Repeater ID="GradeEditor" runat="server"> <ItemTemplate> <asp:Panel ID="Panel1" runat="server" Width="100%" Height="100%"> <table style="width:100%;"> <tr> <td> <asp:Label ID="LateLabel" runat="server" CssClass="label label-success" Visible="False"></asp:Label> </td> <td>&nbsp;</td> </tr> <tr> <td colspan="2"> <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" BackColor="Transparent" BorderColor="Transparent" OnTextChanged="Grade_TextChanged" style="text-align: center" Text='<%# Bind("Grade_Code") %>' Width="100%"></asp:TextBox> </td> </tr> </table> <asp:Label ID="Label4" runat="server" Text='<%# Bind("ID") %>' Visible="False"></asp:Label> <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Bind("ID") %>' /> <asp:HiddenField ID="HiddenField2" runat="server" Value='<%# Bind("Class_ID") %>' /> <asp:HiddenField ID="HiddenField3" runat="server" Value='<%# Bind("Assignment_ID") %>' /> <asp:HiddenField ID="HiddenField4" runat="server" Value='<%# Bind("Student_ID") %>' /> <asp:HiddenField ID="HiddenField5" runat="server" Value='<%# Bind("Exempt") %>' /> </asp:Panel> </ItemTemplate> </asp:Repeater> </ContentTemplate> </asp:UpdatePanel> ,然后提取值

protected void Grade_TextChanged(object sender, EventArgs e) {

   //PUT your original code for calling stored procedure here

   TextBox textBox = sender as TextBox;

   BindRepeater();

   RepeaterItem currentRepeaterItem = (RepeaterItem)(textBox.NamingContainer);
   int currentRepeaterItemIndex = currentRepeaterItem.ItemIndex;

   RepeaterItem nextRepeaterItem = null;

   //get the next item row of Repeater
   if ((rptCustomers.Items.Count - 1) >= (currentRepeaterItemIndex + 1)) {
    nextRepeaterItem = GradeEditor.Items[currentRepeaterItemIndex + 1];
   }

   if (nextRepeaterItem != null) {
    //get the textbox in next row of Repeater
    TextBox nextTextBox = nextRepeaterItem.FindControl("TextBox1") as TextBox;

    //emit JavasSript to focus the next textbox
    ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "focusnext", String.Format(" var nextTextBox = document.getElementById('{0}'); nextTextBox.focus(); nextTextBox.select();", nextTextBox.ClientID), true);
   }
}