我有一个方法如下
public List<List<CustomClass>> categorize(List<CustomClass> customClass){
List<List<CustomClass>> returnValue = new ArrayList<>();
for (CustomClass customClassValue: customClass) {
List<CustomClass> one = new ArrayList<>(), two = new ArrayList<>(), three = new ArrayList<>(), four = new ArrayList<>();
switch (customClassValue.getAge()){
case 1:
one.add(customClassValue);
break;
case 2:
two.add(customClassValue);
break;
case 3:
three.add(customClassValue);
break;
case 4:
four.add(customClassValue);
break;
}
returnValue.add(one);
returnValue.add(two);
returnValue.add(three);
returnValue.add(four);
}
return returnValue;
}
在有效性和性能方面,java集合中是否有使用List<List<CustomClass>>
的替代方法。
只需描述该功能的作用:
输入自定义对象列表并根据对象中的字段对其进行分类,并单独返回每个类别项目。
答案 0 :(得分:2)
当您想要将内容分类到存储桶时,我的第一直觉是使用Map
而不是List
。根据年龄将List<CustomClass>
划分为子列表似乎是使用Map<Integer, List<CustomClass>>
的最佳时机。这是一种方法:
public Map<Integer, List<CustomClass>> categorize(List<CustomClass> customClass) {
Map<Integer, List<CustomClass>> returnValue = new HashMap<>();
for (CustomClass customClassValue: customClass) {
List<CustomClass> sublist = returnValue.get(customClassValue.getAge());
if (sublist == null) {
sublist = new ArrayList<>();
returnValue.put(customClassValue.getAge(), sublist);
}
sublist.add(customClassValue);
}
return returnValue;
}
答案 1 :(得分:0)
Multimap是Public Class EmailService
Implements IIdentityMessageService
Public Function SendAsync(message As IdentityMessage) As Task Implements IIdentityMessageService.SendAsync
' Plug in your email service here to send an email.
'Return Task.FromResult(0)
Dim client As New Net.Mail.SmtpClient(DTAppSettings.SendGrid_SMTPServer, 587)
Dim credentials As New Net.NetworkCredential(DTAppSettings.SendGrid_Username, DTAppSettings.SendGrid_Password)
client.Credentials = credentials
client.EnableSsl = True
Dim mailmessage As New Net.Mail.MailMessage With {
.From = New Net.Mail.MailAddress(DTAppSettings.SendGrid_FromAddress, DTAppSettings.SendGrid_FromName),
.Subject = message.Subject,
.Body = message.Body,
.IsBodyHtml = True
}
mailmessage.To.Add(message.Destination)
Return client.SendMailAsync(mailmessage)
End Function
End Class
Public Class SmsService
Implements IIdentityMessageService
Public Function SendAsync(message As IdentityMessage) As Task Implements IIdentityMessageService.SendAsync
' Plug in your SMS service here to send a text message.
Return Task.FromResult(0)
End Function
End Class
' Configure the application user manager used in this application. UserManager is defined in ASP.NET Identity and is used by the application.
Public Class ApplicationUserManager
Inherits UserManager(Of ApplicationUser)
Public Sub New(store As IUserStore(Of ApplicationUser))
MyBase.New(store)
End Sub
Public Shared Function Create(options As IdentityFactoryOptions(Of ApplicationUserManager), context As IOwinContext) As ApplicationUserManager
Dim manager = New ApplicationUserManager(New UserStore(Of ApplicationUser)(context.[Get](Of ApplicationDbContext)()))
' Configure validation logic for usernames
manager.UserValidator = New UserValidator(Of ApplicationUser)(manager) With {
.AllowOnlyAlphanumericUserNames = False,
.RequireUniqueEmail = True
}
' Configure validation logic for passwords
manager.PasswordValidator = New PasswordValidator() With {
.RequiredLength = 6,
.RequireNonLetterOrDigit = True,
.RequireDigit = True,
.RequireLowercase = True,
.RequireUppercase = True
}
' Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user.
' You can write your own provider and plug in here.
'manager.RegisterTwoFactorProvider("Phone Code", New PhoneNumberTokenProvider(Of ApplicationUser)() With {
' .MessageFormat = "Your security code is {0}"
'})
'manager.RegisterTwoFactorProvider("Email Code", New EmailTokenProvider(Of ApplicationUser)() With {
' .Subject = "Security Code",
' .BodyFormat = "Your security code is {0}"
'})
' Configure user lockout defaults
manager.UserLockoutEnabledByDefault = True
manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5)
manager.MaxFailedAccessAttemptsBeforeLockout = 5
manager.EmailService = New EmailService()
manager.SmsService = New SmsService()
Dim dataProtectionProvider = options.DataProtectionProvider
If dataProtectionProvider IsNot Nothing Then
manager.UserTokenProvider = New DataProtectorTokenProvider(Of ApplicationUser)(dataProtectionProvider.Create("ASP.NET Identity")) With {
.TokenLifespan = TimeSpan.FromHours(1)
}
End If
Return manager
End Function
End Class
Public Class ApplicationSignInManager
Inherits SignInManager(Of ApplicationUser, String)
Public Sub New(userManager As ApplicationUserManager, authenticationManager As IAuthenticationManager)
MyBase.New(userManager, authenticationManager)
End Sub
Public Overrides Function CreateUserIdentityAsync(user As ApplicationUser) As Task(Of ClaimsIdentity)
Return user.GenerateUserIdentityAsync(DirectCast(UserManager, ApplicationUserManager))
End Function
Public Shared Function Create(options As IdentityFactoryOptions(Of ApplicationSignInManager), context As IOwinContext) As ApplicationSignInManager
Return New ApplicationSignInManager(context.GetUserManager(Of ApplicationUserManager)(), context.Authentication)
End Function
End Class
的更简洁版本,Google的Guava有很好的实现。它应该解决你的问题。
答案 2 :(得分:0)
试试这个。
public List<List<CustomClass>> categorize(List<CustomClass> customClass) {
return customClass.stream()
.filter(v -> v.getAge() >= 1 && v.getAge() <= 4)
.collect(
() -> IntStream.range(0, 4)
.mapToObj(i -> new ArrayList<CustomClass>())
.collect(Collectors.toList()),
(list, v) -> list.get(v.getAge() - 1).add(v),
(a, b) -> {});
}