在以下代码中,在这里(在Value对象/ java bean中)具有isMatched()是否有意义
?有什么好的设计。顺便说一句,我通过遵循堆栈溢出的其他帖子尝试了compareTo,compare,hashSet等,但对于我从两个列表中删除公仔的方式仍然不起作用。
public class SessionAttributes {
private static final Logger LOGGER = Logger
.getLogger(SessionAttributes.class);
public SessionNotificationAttributes(String userName, String sessionState) {
this.userName = userName;
this.sessionState = sessionState;
}
String userName;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
// .. getters/setters for sessionState
public static isMatched (List<SessionAttributes> list1,
List<SessionAttributes> list2) {
//.. custom logic here...
}
}
====大卫注释中的每个询问的整个代码。看一下main()方法。这是直接从Eclipse复制粘贴来满足http://sscce.org/要求========
package snippet;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.apache.log4j.Logger;
public class SessionAttributes implements
Comparable<SessionAttributes>, Comparator<SessionAttributes> {
private static final Logger LOGGER = Logger
.getLogger(SessionAttributes.class);
public SessionAttributes(String userName, String sessionState) {
/*
* String nasPort, String endpointProfile, String audiSessionId, String
* epsStatus, String securityGroup, String nasIp, String postureStatus,
* String postureTimestamp) {
*/
this.userName = userName;
this.sessionState = sessionState;
/*
* this.nasPort = nasPort; this.endpoinProfile = endpointProfile;
* this.auditSessionId = audiSessionId; this.epsStatus = epsStatus;
* this.securityGroup = securityGroup; this.nasIp = nasIp;
* this.postureStatus = postureStatus; this.postureTimestamp =
* postureTimestamp;
*/
}
String userName;
public String getUserName() {
return userName;
}
String sessionState;
public String getSessionState() {
return sessionState;
}
public int compareTo(SessionAttributes o) {
// TODO Auto-generated method stub
if (this.getUserName().equals(o.getUserName()) && this.getSessionState().equalsIgnoreCase(o.getSessionState())) {
return 0;
}
return -1;
}
public String toString() {
return "\n User Name : " + this.getUserName() + " Session State : "
+ getSessionState() + "\n";
}
static boolean isMatched(List<SessionAttributes> list1,
List<SessionAttributes> list2) {
if (null == list1 || null == list2)
return false;
System.out.println("Actual List=>" + list1);
System.out.println("Expected List=>" + list2);
Iterator<SessionAttributes> iterator = list1.iterator();
while (iterator.hasNext()) {
SessionAttributes actual = iterator.next();
Iterator<SessionAttributes> iterator2 = list2
.iterator();
while (iterator2.hasNext()) {
SessionAttributes expected = iterator2.next();
if (expected.getUserName().equalsIgnoreCase(
actual.getUserName())) {
if (expected.getSessionState().equalsIgnoreCase(
actual.getSessionState())) {
System.out.println("Element matched - user name-"
+ expected.getUserName() + " State -"
+ expected.getSessionState());
iterator.remove();
iterator2.remove();
}
} else {
System.out.println("Element NOT matched - user name-"
+ expected.getUserName() + " State -"
+ expected.getSessionState());
}
}
}
System.out.println("Lists after removing Dups -");
System.out.println("list1 =>" + list1.toString() + " list2 -"
+ list2.toString());
if (list1.size() > 0 || list2.size() > 0)
return false;
return true;
}
static void sortLists () {
List<SessionAttributes> expectedSessionList = new ArrayList<SessionAttributes>();
SessionAttributes user11 = new SessionAttributes(
"postureuser1", "STARTED"); //
// ,null,null,null,null,null,null,null,null);
SessionAttributes user12 = new SessionAttributes(
"postureuser1", "DISCONNECTED");
SessionAttributes user13 = new SessionAttributes(
"postureuser5", "STARTED");
// ,null,null,null,null,null,null,null,null);
expectedSessionList.add(user11);
expectedSessionList.add(user12);
expectedSessionList.add(user13);
List<SessionAttributes> actualSessionList = new ArrayList<SessionAttributes>();
SessionAttributes user3 = new SessionAttributes(
"postureuser1", "STARTED");
// ,null,null,null,null,null,null,null,null);
SessionAttributes user4 = new SessionAttributes(
"postureuser1", "DISCONNECTED");
SessionAttributes user5 = new SessionAttributes(
"postureuser2", "DISCONNECTED");
// ,null,null,null,null,null,null,null,null);
actualSessionList.add(user3);
actualSessionList.add(user4);
actualSessionList.add(user5);
Set<SessionAttributes> removeDups = new HashSet<SessionAttributes>();
boolean b1 = removeDups.add(user11);
boolean b2 = removeDups.add(user12);
boolean b3 = removeDups.add(user13);
boolean b4 = removeDups.add(user3);
boolean b5 = removeDups.add(user4);
boolean b6 = removeDups.add(user5);
System.out.println(" Set--" + removeDups);
// removeDups.addAll(expectedSessionList);
// removeDups.addAll(actualSessionList);
System.out.println("== Printing Set ====");
int countMisMatch = 0;
System.out.println(isMatched(actualSessionList, expectedSessionList));
// int isMatch = user3.compareTo(user1);
// System.out.println("Compare=>" + isMatch);
}
static void sortSet () {
List<SessionAttributes> expectedSessionList = new ArrayList<SessionAttributes>();
SessionAttributes user11 = new SessionAttributes(
"postureuser1", "STARTED"); //
// ,null,null,null,null,null,null,null,null);
SessionAttributes user12 = new SessionAttributes(
"postureuser1", "DISCONNECTED");
SessionAttributes user13 = new SessionAttributes(
"postureuser5", "STARTED");
SessionAttributes user3 = new SessionAttributes(
"postureuser1", "STARTED");
// ,null,null,null,null,null,null,null,null);
SessionAttributes user4 = new SessionAttributes(
"postureuser1", "DISCONNECTED");
SessionAttributes user5 = new SessionAttributes(
"postureuser2", "DISCONNECTED");
// ,null,null,null,null,null,null,null,null);
Set<SessionAttributes> removeDups = new HashSet<SessionAttributes>();
boolean b1 = removeDups.add(user11);
boolean b2 = removeDups.add(user12);
boolean b3 = removeDups.add(user13);
boolean b4 = removeDups.add(user3);
boolean b5 = removeDups.add(user4);
boolean b6 = removeDups.add(user5);
System.out.println(" Set--" + removeDups);
// removeDups.addAll(expectedSessionList);
// removeDups.addAll(actualSessionList);
System.out.println("== Printing Set ====");
System.out.println(removeDups);
// int isMatch = user3.compareTo(user1);
// System.out.println("Compare=>" + isMatch);
}
public int compare(SessionAttributes o1,
SessionAttributes o2) {
LOGGER.debug("Compare called -[" + o1.getUserName() + "] ["
+ o2.getUserName() + "]");
boolean isSameUserName = o1.userName.equalsIgnoreCase(o2.userName);
boolean isSameState = o1.sessionState
.equalsIgnoreCase(this.sessionState);
if (isSameUserName && isSameState)
return 0;
return -1;
}
public boolean equals(SessionAttributes obj) {
if (obj == null || !(obj instanceof SessionAttributes)) {
return false;
}
System.out.println(" In equals==");
boolean isSameUserName = obj.userName.equalsIgnoreCase(this.userName);
boolean isSameState = obj.sessionState
.equalsIgnoreCase(this.sessionState);
return (isSameUserName && isSameState);
}
public int hashCode() {
System.out.println(" in hashcode ");
int hash = 1;
hash = hash * 17 + this.getUserName().hashCode();
hash = hash * 31 + this.getSessionState().hashCode();
// hash = hash * 13 + this.getAuditSessionId().hashCode();
System.out.println(" hash=>" + hash);
return hash;
}
public static void main(String[] args) {
//sortSet();
sortLists();
}
}
====来自David的代码,该代码应删除公母。仅粘贴相关部分以进行更好的比较。不知何故,这仍然行不通
public int compareTo(SessionAttributesFromDavid o) {
if (this == o) {
return 0;
}
// Null is considered less than any object.
if (o == null) {
return 1;
}
// Use compareToIgnoreCase since you used equalsIgnoreCase in equals.
int diff = userName.compareToIgnoreCase(o.userName);
if (diff != 0)
return diff;
diff = sessionState.compareToIgnoreCase(o.sessionState);
return diff;
}
public boolean equals(Object o) {
// See if o is the same object. If it is, return true.
if (o == this) {
return true;
}
// The instanceof check also checks for null. If o is null, instanceof will be false.
if (!(o instanceof SessionAttributes)) {
return false;
}
SessionAttributes that = (SessionAttributes) o;
return userName.equalsIgnoreCase(that.userName) && sessionState.equalsIgnoreCase(sessionState);
}
设置removeDups = new TreeSet();
boolean b1 = removeDups.add(user11);
boolean b2 = removeDups.add(user12);
boolean b3 = removeDups.add(user13);
boolean b4 = removeDups.add(user3);
boolean b5 = removeDups.add(user4);
boolean b6 = removeDups.add(user5);
System.out.println(" Set--" + removeDups);
组 - [
用户名:stantness2会话状态:已断开
,
用户名:stantness1会话状态:已启动
,
用户名:stantness5会话状态:已启动
,
用户名:stantness1会话状态:已断开
,
用户名:stantness1会话状态:已启动
]
最佳答案
我不会创建isMatched方法来比较两个SessionAttributes列表。
我绝对会选择让SessionAttributes实现equals和hashCode。至关重要的是要以相同的方式实现它们。如果要比较两个字符串是否相等,请使用两个字符串计算您的hashCode。如果您没有获得equals和hashCode正确的权限,那么这些都将无效。
如果要将SessionAttributes放在SortedSet中,我也将让SessionAttributes实现Comparable。
另外,我将使SessionAttributes不可变,因此没有设置方法,并且将两个String元素声明为final。如果执行此操作,则可以将SessionAttributes添加到Set或Map中,而不必担心它们的值会更改。如果您不将它们设为不可变的,则必须确保在将它们添加到列表或集合后,不要更改任何SessionAttributes值。
与其将它们放入列表中,不如将它们放入集合中以确保您在同一SessionAttributes集合中没有重复项。
如果要从其中一个Collection中删除重复项,请在其上使用Collection的removeAll方法,并将另一个SessionAttributes集合传递给它。
附带说明一下,sessionState看起来像一个带有有限数量的可能值的变量,因此我将考虑为其定义一个Enum而不是将其设为String。
我希望这有帮助。
编辑:
您的compareTo方法不起作用,因为如果相等则返回0,否则返回-1。
它不履行compareTo的合同。请阅读其javadocs。
以下是SSCCE,其中包含一些更改和注释。由于在equals中,您需要比较忽略大小写的相等性,因此必须在hashCode方法中将Strings转换为所有大写或小写字母,以便它与equals保持一致。您还必须在compareTo方法中使用compareIgnoreCase以获得一致性。
由于compareTo方法有效,因此您现在可以简单地使用TreeSet对对象的集合进行排序。
我删除了不再需要的方法,并尝试在代码中添加一些有用的注释。
package snippet;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
// You don't need to implement Comparator.
public class SessionAttributes implements Comparable<SessionAttributes> {
// You typically define member variables at the top of the class definition.
private final String userName;
private final String sessionState;
public SessionAttributes(String userName, String sessionState) {
// Throw a NullPointerException from the constructor if either of the Strings is null. This way, you know that
// if the object is constructed successfully, it is free of nulls.
if (userName == null) {
throw new NullPointerException("userName must not be null");
}
if (sessionState == null) {
throw new NullPointerException("sessionState must not be null");
}
/*
* String nasPort, String endpointProfile, String audiSessionId, String epsStatus, String securityGroup, String
* nasIp, String postureStatus, String postureTimestamp) {
*/
this.userName = userName;
this.sessionState = sessionState;
/*
* this.nasPort = nasPort; this.endpoinProfile = endpointProfile; this.auditSessionId = audiSessionId;
* this.epsStatus = epsStatus; this.securityGroup = securityGroup; this.nasIp = nasIp; this.postureStatus =
* postureStatus; this.postureTimestamp = postureTimestamp;
*/
}
public String getUserName() {
return userName;
}
public String getSessionState() {
return sessionState;
}
@Override
public int compareTo(SessionAttributes o) {
if (this == o) {
return 0;
}
// Null is considered less than any object.
if (o == null) {
return 1;
}
// Use compareToIgnoreCase since you used equalsIgnoreCase in equals.
int diff = userName.compareToIgnoreCase(o.userName);
if (diff != 0)
return diff;
diff = sessionState.compareToIgnoreCase(o.sessionState);
return diff;
}
// public int compareTo(SessionAttributes o) {
// // TODO Auto-generated method stub
// if (this.getUserName().equals(o.getUserName()) && this.getSessionState().equalsIgnoreCase(o.getSessionState())) {
// return 0;
// }
// return -1;
// }
public String toString() {
return "\n User Name : " + this.getUserName() + " Session State : " + getSessionState() + "\n";
}
// public boolean equals(SessionAttributes obj) {
//
// if (obj == null || !(obj instanceof SessionAttributes)) {
// return false;
// }
// System.out.println(" In equals==");
// boolean isSameUserName = obj.userName.equalsIgnoreCase(this.userName);
// boolean isSameState = obj.sessionState.equalsIgnoreCase(this.sessionState);
// return (isSameUserName && isSameState);
// }
public boolean equals(Object o) {
// See if o is the same object. If it is, return true.
if (o == this) {
return true;
}
// The instanceof check also checks for null. If o is null, instanceof will be false.
if (!(o instanceof SessionAttributes)) {
return false;
}
SessionAttributes that = (SessionAttributes) o;
return userName.equalsIgnoreCase(that.userName) && sessionState.equalsIgnoreCase(sessionState);
}
public int hashCode() {
System.out.println(" in hashcode ");
int hash = 1;
// Since in equals you are comparing for equality and ignoring case, you must convert the Strings to either
// lower
// or upper case when computing the hashCode so that it will always be consistent with equals.
hash = hash * 17 + this.getUserName().toUpperCase().hashCode();
hash = hash * 31 + this.getSessionState().toUpperCase().hashCode();
// hash = hash * 13 + this.getAuditSessionId().hashCode();
System.out.println(" hash=>" + hash);
return hash;
}
public static void main(String[] args) {
// sortSet();
// sortLists();
// expectedSessionList
List<SessionAttributes> expectedSessionList = new ArrayList<SessionAttributes>();
SessionAttributes user11 = new SessionAttributes("postureuser1", "STARTED"); //
// ,null,null,null,null,null,null,null,null);
SessionAttributes user12 = new SessionAttributes("postureuser1", "DISCONNECTED");
SessionAttributes user13 = new SessionAttributes("postureuser5", "STARTED");
expectedSessionList.add(user11);
expectedSessionList.add(user12);
expectedSessionList.add(user13);
System.out.println("expectedSessionList: " + expectedSessionList);
// actualSessionList
List<SessionAttributes> actualSessionList = new ArrayList<SessionAttributes>();
SessionAttributes user3 = new SessionAttributes("postureuser1", "STARTED");
// ,null,null,null,null,null,null,null,null);
SessionAttributes user4 = new SessionAttributes("postureuser1", "DISCONNECTED");
SessionAttributes user5 = new SessionAttributes("postureuser2", "DISCONNECTED");
// ,null,null,null,null,null,null,null,null);
actualSessionList.add(user3);
actualSessionList.add(user4);
actualSessionList.add(user5);
System.out.println("actualSessionList: " + actualSessionList);
// removeDups
// Use a TreeSet to sort it.
Set<SessionAttributes> removeDups = new TreeSet<SessionAttributes>();
boolean b1 = removeDups.add(user11);
boolean b2 = removeDups.add(user12);
boolean b3 = removeDups.add(user13);
boolean b4 = removeDups.add(user3);
boolean b5 = removeDups.add(user4);
boolean b6 = removeDups.add(user5);
System.out.println(" Set--" + removeDups);
actualSessionList.removeAll(expectedSessionList);
System.out.println("actualSessionList after removeAll: " + actualSessionList);
}
}
输出:
ExpectedSessionList:[
用户名:stantness1会话状态:已启动
,
用户名:stantness1会话状态:已断开
,
用户名:stantness5会话状态:已启动
]
ActualSessionList:[
用户名:stantness1会话状态:已启动
,
用户名:stantness1会话状态:已断开
,
用户名:stantness2会话状态:已断开
]
组 - [
用户名:stantness1会话状态:已断开
,
用户名:stantness1会话状态:已启动
,
用户名:stantness2会话状态:已断开
,
用户名:stantness5会话状态:已启动
]
removeAll之后的actualSessionList:[
用户名:stantness2会话状态:已断开
]