如果您有两个班级列表,您如何比较它们以查看它们是否相同,无论顺序如何。
Class[] list1[class1, class1, class2]
Class[] list2[class2, class1, class1]
这些列表无论顺序如何都相同但java布尔怎么样呢? e.g
if(list1.sort == list2.sort){}
答案 0 :(得分:1)
最佳解决方案是添加Guava
并使用MultiSet
。
HashMultiset<Class> hms1 = new HashMultiSet<>();
HashMultiset<Class> hms1 = new HashMultiSet<>();
for (Class c : list1) {
hms1.add(c);
}
for (Class c : list2) {
hms2.add(c);
}
if (hms1.equals(hms2) {
//lists are the same in your understanding of the same
}
更简单的解决方案是使用Map<Class, Integer>
HashMap<Class, Integer> hm1 = new HashMap<>();
HashMap<Class, Integer> hm2 = new HashMap<>();
for (Class c : list1) {
if (!hm1.containsKey(c)) {
hm1.put(c, 1);
} else {
hm1.put(c, hm1.get(c)+1);
}
}
for (Class c : list2) {
if (!hm2.containsKey(c)) {
hm2.put(c, 1);
} else {
hm2.put(c, hm2.get(c)+1);
}
}
if (hm1.equals(hm2) {
//lists are the same in your understanding of the same
}
答案 1 :(得分:0)
Class<?>[] list1 = new Class[] { String.class, String.class, Integer.class };
Class<?>[] list2 = new Class[] { Integer.class, String.class, String.class };
Comparator<Class<?>> classComparator = new Comparator<Class<?>>() {
@Override
public int compare(Class<?> o1, Class<?> o2) {
return o1.getCanonicalName().compareTo(o2.getCanonicalName());
}
};
Arrays.sort(list1, classComparator);
Arrays.sort(list2, classComparator);
if (Arrays.equals(list1, list2)) {
System.out.println("same regardless of order");
} else {
System.out.println("NOT same regardless of order");
}
以上打印
same regardless of order
我正在修改原始列表。如果不需要,您可能需要先复印。
Arrays.equals()
正在使用Class.equals()
,而Object.equals()
只是public override Task ValidateTokenRequest(ValidateTokenRequestContext context)
{
// Reject the token request if it doesn't use grant_type=password, refresh_token
// or urn:ietf:params:oauth:grant-type:facebook_access_token.
if (!context.Request.IsPasswordGrantType() &&
!context.Request.IsRefreshTokenGrantType() &&
context.Request.GrantType != "urn:ietf:params:oauth:grant-type:facebook_access_token")
{
context.Reject(
error: OpenIdConnectConstants.Errors.UnsupportedGrantType,
description: "The specified grant type is not supported by this server.");
return Task.FromResult(0);
}
// Reject the token request if the assertion parameter is missing.
if (context.Request.GrantType == "urn:ietf:params:oauth:grant-type:facebook_access_token" &&
string.IsNullOrEmpty(context.Request.Assertion))
{
context.Reject(
error: OpenIdConnectConstants.Errors.InvalidRequest,
description: "The assertion is missing.");
return Task.FromResult(0);
}
// Since there's only one application and since it's a public client
// (i.e a client that cannot keep its credentials private), call Skip()
// to inform the server the request should be accepted without
// enforcing client authentication.
context.Skip();
return Task.FromResult(0);
}
public override Task HandleTokenRequest(HandleTokenRequestContext context)
{
// Only handle grant_type=password and urn:ietf:params:oauth:grant-type:facebook_access_token
// requests and let the OpenID Connect server middleware handle the refresh token requests.
if (context.Request.IsPasswordGrantType())
{
// Skipped for brevity.
}
else if (context.Request.GrantType == "urn:ietf:params:oauth:grant-type:facebook_access_token")
{
// The assertion corresponds to the Facebook access token.
var assertion = context.Request.Assertion;
// Create a new ClaimsIdentity containing the claims that
// will be used to create an id_token and/or an access token.
var identity = new ClaimsIdentity(OpenIdConnectServerDefaults.AuthenticationScheme);
// Validate the access token using Facebook's token validation
// endpoint and add the user claims you retrieved here.
identity.AddClaim(ClaimTypes.NameIdentifier, "FB user identifier");
// Create a new authentication ticket holding the user identity.
var ticket = new AuthenticationTicket(
new ClaimsPrincipal(identity),
new AuthenticationProperties(),
OpenIdConnectServerDefaults.AuthenticationScheme);
// Set the list of scopes granted to the client application.
ticket.SetScopes(new[]
{
/* openid: */ OpenIdConnectConstants.Scopes.OpenId,
/* email: */ OpenIdConnectConstants.Scopes.Email,
/* profile: */ OpenIdConnectConstants.Scopes.Profile,
/* offline_access: */ OpenIdConnectConstants.Scopes.OfflineAccess
}.Intersect(context.Request.GetScopes()));
context.Validate(ticket);
}
return Task.FromResult(0);
}
。只要每个类只有一个Class对象,就可以了。我相信只要你只使用一个类加载器就可以了。所以请注意,这里有点微妙。