I'm working with hashCode for the first time and not sure how to check if two objects are equal. This is what I have so far.
/** Represents a City */
class City {
/** Decimal format to print leading zeros in zip code */
static DecimalFormat zipFormat = new DecimalFormat("00000");
int zip;
String name;
String state;
double longitude;
double latitude;
/** The full constructor */
public City (int zip, String name, String state,
double longitude, double latitude) {
this.zip = zip;
this.name = name;
this.state = state;
this.longitude = longitude;
this.latitude = latitude;
}
/** to make sure the two cities have the same name, state, zip code,
* and the same latitude and longitude */
public boolean equals(Object obj){
if (obj == null)
return false;
City temp = (City)obj;
System.out.println(City.equals(obj));
}
Assuming equality in your case means all properties are same in your equals method add
City temp = (City)obj;
return this.zip == temp.zip &&
this.name == temp.name &&
...
;
eclipse auto-generates for you:
#Override
public int hashCode()
{
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(latitude);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(longitude);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((state == null) ? 0 : state.hashCode());
result = prime * result + zip;
return result;
}
#Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
City other = (City) obj;
if (Double.doubleToLongBits(latitude) != Double
.doubleToLongBits(other.latitude))
return false;
if (Double.doubleToLongBits(longitude) != Double
.doubleToLongBits(other.longitude))
return false;
if (name == null)
{
if (other.name != null)
return false;
}
else if (!name.equals(other.name))
return false;
if (state == null)
{
if (other.state != null)
return false;
}
else if (!state.equals(other.state))
return false;
if (zip != other.zip)
return false;
return true;
}
Related
This question already has answers here:
What issues should be considered when overriding equals and hashCode in Java?
(11 answers)
Closed 7 years ago.
Create the hashCode and equals method for the following class.
private static class MyOb {
private String name;
private Integer quality;
private final int MAXIMUM = 23;
}
I could not solve this question
Java class has default hashCode and equals method method implemented through super class. If u want to over ride them u can by following:
class MyOb {
private String name;
private Integer quality;
private final int MAXIMUM = 23;
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((quality == null) ? 0 : quality.hashCode());
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MyOb other = (MyOb) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (quality == null) {
if (other.quality != null)
return false;
} else if (!quality.equals(other.quality))
return false;
return true;
}
}
Note: class cannot be private or static
if you want see hash code of class.
create object
Myob myObject=new Myob();
System.out.println(myObject);
if want to equals method
myObject.equals(which one you want to campare);
I have two sets which contain some elements as object. I want to remove common element from the set. How can I remove common elements from set?
Set<AcceptorInventory> updateList = new HashSet<AcceptorInventory>();
Set<AcceptorInventory> saveList = new HashSet<AcceptorInventory>();
Both sets have some items, the saveList have duplicated items & I wish to remove duplicated items from saveList. I tried with foreach loop, but it did not work.
Sample output:
save 5
save 20
save 50
save 10
update 5
update 10
update 20
AcceptorInventory Hashcode and equals
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + count;
result = prime * result
+ ((currency == null) ? 0 : currency.hashCode());
result = prime * result + ((date == null) ? 0 : date.hashCode());
result = prime * result + (int) (id ^ (id >>> 32));
result = prime * result + (isCleared ? 1231 : 1237);
result = prime * result
+ ((kioskMachine == null) ? 0 : kioskMachine.hashCode());
result = prime * result + ((time == null) ? 0 : time.hashCode());
long temp;
temp = Double.doubleToLongBits(total);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
AcceptorInventory other = (AcceptorInventory) obj;
if (count != other.count)
return false;
if (currency == null) {
if (other.currency != null)
return false;
} else if (!currency.equals(other.currency))
return false;
if (date == null) {
if (other.date != null)
return false;
} else if (!date.equals(other.date))
return false;
if (id != other.id)
return false;
if (isCleared != other.isCleared)
return false;
if (kioskMachine == null) {
if (other.kioskMachine != null)
return false;
} else if (!kioskMachine.equals(other.kioskMachine))
return false;
if (time == null) {
if (other.time != null)
return false;
} else if (!time.equals(other.time))
return false;
if (Double.doubleToLongBits(total) != Double
.doubleToLongBits(other.total))
return false;
return true;
}
updateList.removeAll(saveList);
would remove from updateList all the elements of saveList.
If you also want to remove from saveList the elements of updateList, you'll have to create a copy of one of the sets first :
Set<AcceptorInventory> copyOfUpdateList = new HashSet<>(updateList);
updateList.removeAll (saveList);
saveList.removeAll (copyOfUpdateList);
Note that in order for your AcceptorInventory to function properly as an element of a HashSet it must override the equals and hashCode methods, and any two AcceptorInventory which are equal must have the same hashCode.
You can remove common items from current saveList using
saveList.removeAll(updateList);
Okay, so I get a text file, tokenize it, and then try to write it to another file.
I have a class called course which has 4 parameters: name, level, code, year
I have divided each line of the text file onto a different line
#Edit: Should have mentioned this earlier, but I have 3000 courses stored in a text file
public void readFromFile() throws IOException
{
int index = 0;
int j = -1;
int spaceCount = 0;
courseNumber++;
setCourseFields();
BufferedReader inputFile = new BufferedReader(new FileReader(INPUT_FILE));
PrintWriter outputFile = new PrintWriter(new FileWriter(OUTPUT_FILE));
String lineOfText = inputFile.readLine();
while (lineOfText != null)
{
outputFile.println(lineOfText);
lineOfText = inputFile.readLine();
for (char c : lineOfText.toCharArray())
{
if (c == ' ')
{
spaceCount++;
}
}
if(spaceCount == 1)
{
delimiter = DELIMITER_SPACE;
}
else if(spaceCount == 2)
{
delimiter = DELIMITER_TAB;
}
System.out.println("Course" + courseNumber + "\n");
// for each token in the string
while ((j = lineOfText.indexOf(delimiter, (index = ++j))) != -1)
{
System.out.println(courseField[k] + ": " + lineOfText.substring(index, j));
counter++;
k++;
}
// extract the last token
if (index > 0)
{
System.out.println("Year: " + lineOfText.substring(index));
++courseNumber;
}
if(k == 3)
{
k = k - k;
index = 0;
}
delayDisplay();
} // while(lineOfText != null)
inputFile.close();
outputFile.close();
}
And then I'd get something like this
Course1
Name: Computer Engineering Technology
Level: IB
Code: TEJ4M
Year: 2017
Course2
Name: Communications Technology
Level: Special Education
Code: TGJ2O
Year: 2002
Each course is unique, and I need help so that I can assign each of the above strings to a variable.
Each course has 4 instance fields + a unique serial number(Course Number)
The 4 fields are:
String name;
char level;
String code;
int academicYear;
I want to make courses out of each of the 4 lines
Here is my course class
public class Course extends Object implements Serializable
{
// class constants
public static final String DEFAULT_CODE = "AAA10";
public static final char DEFAULT_LEVEL = 'X';
public static final String DEFAULT_NAME = "unassigned";
public static final int DEFAULT_YEAR = 1900;
private static final char ACADEMIC_LEVEL = '1';
private static final char ENGLISH_LANGUAGE_LEARNERS_LEVEL = '9';
private static final int ERROR_CODE = -1;
private static final char IB_LEVEL = '7';
private static final int MAXIMUM_YEAR = 2014;
private static final int MINIMUM_YEAR = 1900;
private static final char SPECIAL_EDUCATION_LEVEL = '8';
// instance variables
private int academicYear;
private String code;
private static int counter = 0;
private char level;
private String name;
private int serialNumber;
/**
* Constructs a course with default characteristics.
*/
public Course()
{
this.academicYear = DEFAULT_YEAR;
this.code = DEFAULT_CODE;
this.level = DEFAULT_LEVEL;
this.name = DEFAULT_NAME;
serialNumber = ++counter;
} // end of constructor Course()
/**
* Constructs a course with the specified characteristics.
*
* #param name - the Ministry-assigned name of this course;
* ex: Introduction to Computer Science
* #param code - the Ministry-assigned code of this course; ex: ICS3U
* #param level - one of the enumerated levels ex: '7', '1', '9', '8'
* #param academicYear - a 4-digit year of the Gregorian calendar
* set between a minimum and maximum year
*/
public Course(String name, String code, char level, int academicYear)
{
if(name == null)
{
this.name = DEFAULT_NAME;
}
else
{
this.name = name;
} // end of if(name == null)
if(code == null)
{
this.code = DEFAULT_CODE;
}
else
{
this.code = code;
} // end of if(code == null)
if (level == IB_LEVEL || level == ACADEMIC_LEVEL
|| level == ENGLISH_LANGUAGE_LEARNERS_LEVEL
|| level == SPECIAL_EDUCATION_LEVEL)
{
this.level = level;
}
else
{
this.level = DEFAULT_LEVEL;
} // end of if (level == IB_LEVEL || level == ACADEMIC_LEVEL || level ==
ENGLISH_LANGUAGE_LEARNERS_LEVEL || level == SPECIAL_EDUCATION_LEVEL )
if (academicYear >= MINIMUM_YEAR && academicYear <= MAXIMUM_YEAR)
{
this.academicYear = academicYear;
}
else
{
this.academicYear = DEFAULT_YEAR;
} // end of (academicYear >= MINIMUM_YEAR && academicYear <= MAXIMUM_YEAR)
serialNumber = ++counter;
} // end of constructor Course(String name, String code, char level, int academicYear)
/* Comparison methods*/
/**
* Indicates whether another object has a state identical to this object’s state.
*
* #param otherCourse - the object whose state is compared to this object’s
*
* #return true if the other object has an identical state; otherwise false
*/
public boolean equals(Object otherCourse)
{
if(otherCourse == null) return false;
if (this.getClass() != otherCourse.getClass()) return false;
if (this == otherCourse) return true;
// Typecasting Object otherCourse into a Course to compare objects' states
Course course1 = (Course) otherCourse;
if(serialNumber != course1.getSerialNumber()) return false;
if(academicYear != course1.getYear()) return false;
if(level != course1.getLevel()) return false;
if(code != course1.getCode()) return false;
if(name != course1.getName()) return false;
// needed to satisfy the compiler
return true;
} // end of method boolean equals(Object course)
/**
* Indicates whether another object has a state identical to this object’s state,
* ignoring each object's unique serial number.
*
* #param otherCourse - the object whose state is compared to this object’s
*
* #return true if the other object has an identical state; otherwise false
*/
public boolean equalsIgnoreSerial(Object otherObject)
{
if(otherObject == null) return false;
if (this.getClass() != otherObject.getClass()) return false;
boolean courseEquals;
Course anotherCourse = (Course) otherObject;
// Ignore unique serial number of each course
if(this.serialNumber == anotherCourse.getSerialNumber()) return false;
if(this.academicYear != anotherCourse.getYear()) return false;
else courseEquals = true;
if(this.level != anotherCourse.getLevel()) return false;
else courseEquals = true;
if(this.code != anotherCourse.getCode()) return false;
else courseEquals = true;
if(this.name != anotherCourse.getName()) return false;
else courseEquals = true;
return courseEquals;
} // end of method boolean equalsIgnoreSerial(Object course
/**
* Compares this Course to another.
*
* #return a negative value, if this course should come before the other course;
* 0, if this course and the other course have equal states;
* a positive value if this course should come after the other course
*/
public int compareTo(Course otherCourse)
{
int before = -1;
int after = 1;
int equals = 0;
int resultCode = code.compareTo(otherCourse.getCode());
int resultName = name.compareTo(otherCourse.getName());
if(otherCourse == null) return -1;
if(this.equals(otherCourse)) return equals;
if(serialNumber < otherCourse.getSerialNumber()) return before;
if(serialNumber > otherCourse.getSerialNumber()) return after;
if(academicYear < otherCourse.getYear()) return before;
if(academicYear > otherCourse.getYear()) return after;
if(!(sortLevel(level) == -1 || sortLevel(otherCourse.getLevel()) == -1))
{
if(sortLevel(level) < sortLevel(otherCourse.getLevel())) return before;
if(sortLevel(level) > sortLevel(otherCourse.getLevel())) return after;
} // end of if(!(sortLevel(level) == -1 || sortLevel(otherCourse.getLevel()) ==
-1))
if(code.compareTo(otherCourse.getCode()) != 0) return resultCode;
if(name.compareTo(otherCourse.getName()) != 0) return resultName;
// neccessary to satisfy the compiler
return 5;
} // end of public int compareTo(Course otherCourse)
/* utility methods*/
public static int sortLevel(char level)
{
/*************************************
final char[] LEVEL = {'7', '1', '9', '8'};
for (int index = 0; index < LEVEL.length; index++)
{
if(LEVEL[index] == level) return index;
if(LEVEL[index] != level) return ERROR_IO_EXCEPTION;
} // end of for (int index = 0; index < LEVEL.length; index++)
// error code for not found, should not be reached
return -1;
****************************************/ //code taken from in class discussion
final char[] LEVEL = {'7', '1', '9', '8'};
for (int index = 0; index < LEVEL.length; index++)
{
if(LEVEL[index] == level) return index;
if(LEVEL[index] != level) return -1;
} // end of for (int index = 0; index < LEVEL.length; index++)
// error code for not found, should not be reached
return ERROR_CODE;
} // end of public static int sortLevel(char level)
/* accessors*/
/**
* Returns the code of this course.
*
* #returns Returns the code of this course
*/
public String getCode()
{
return code;
} // end of method getCode()
/**
* Returns the level of this course.
*
* #return the level of this course
*/
public char getLevel()
{
return level;
} // end of method getLevel()
/**
* Returns the name of this course.
*
* #return the name of this course.
*/
public String getName()
{
return name;
} // end of method getName()
/**
* Returns the unique serial number of this course.
*
* #return the unique serial number of this course.
*/
public int getSerialNumber()
{
return serialNumber;
} // end of method getSerialNumber()
/**
* Returns the academic year of this course.
*
* #return the 4-digit academic year of this course
*/
public int getYear()
{
return academicYear;
} // end of method getYear()
/* mutators */
/**
* Sets the code of this course.
*
* #param newCode - the new code of this course.
*/
public void setCode(String newCode)
{
if (newCode == null) return;
this.code = newCode;
} // end of method setCode(String newCode)
/**
* Sets the level of this course.
*
* #param newLevel - one of the enumerated levels
*/
public void setLevel(char newLevel)
{
if(newLevel == IB_LEVEL || newLevel == ACADEMIC_LEVEL || newLevel ==
ENGLISH_LANGUAGE_LEARNERS_LEVEL || newLevel == SPECIAL_EDUCATION_LEVEL)
{
level = newLevel;
}
else
{
level = DEFAULT_LEVEL;
} // end of if(newLevel == IB_LEVEL || newLevel == ACADEMIC_LEVEL || newLevel ==
ENGLISH_LANGUAGE_LEARNERS_LEVEL || newLevel == SPECIAL_EDUCATION_LEVEL)
} // end of method setLevel(char newLevel)
/**
* Sets the name of this course.
*
* #param newName - the new name of this course
*/
public void setName(String newName)
{
if (newName == null) return;
this.name = newName;
} // end of method setName(String newName)
/**
* Sets the academic year of this course.
*
* #param newYear - the new 4-digit academic year of this course
*/
public void setYear(int newYear)
{
if (newYear >= MINIMUM_YEAR && newYear <= MAXIMUM_YEAR)
{
this.academicYear = newYear;
}
else
{
academicYear = DEFAULT_YEAR;
} // end of if (newYear >= MINIMUM_YEAR && newYear <= MAXIMUM_YEAR)
} // end of method setYear(int newYear)
/**
* Returns a string representation of this course.
*
* #override toString in class Object
*
* #return a string representation of this course.
*/
public String toString()
{
return
this.getClass().getName()
+"["
+ "Serial Number: " + serialNumber
+ ", academic year: " + academicYear
+ ", level: " + level
+ ", code: " + code
+ ", name: " + name
+"]";
} // end of String toString()
Also, I need help converting the string into a char for level and into an int for academic year, any ideas
Here is my CourseUtility class, which is not finished yet
public class CourseUtility
{
// class constants
private static final String INPUT_FILE = "courses.text";
private static final String OUTPUT_FILE = "CoursesTokenized.text";
private static int counter = 0;
private static int courseNumber = 0;
private static int k = 0;
private static final String DELIMITER_SPACE = " ";
private static final String DELIMITER_TAB = "\t";
String delimiter = DELIMITER_TAB;
private static final String DEFAULT_LEVEL = "X";
String name = "";
String code = "";
String year = "";
String level = "";
String lineOfText;
private static String[] courseField = new String[5];
/**
* Constructor for objects of class CourseUtility
*/
public CourseUtility() throws IOException
{
}
public void readFromFile() throws IOException
{
int index = 0;
int j = -1;
int spaceCount = 0;
courseNumber++;
setCourseFields();
BufferedReader inputFile = new BufferedReader(new FileReader(INPUT_FILE));
PrintWriter outputFile = new PrintWriter(new FileWriter(OUTPUT_FILE));
String lineOfText = inputFile.readLine();
while (lineOfText != null)
{
for (char c : lineOfText.toCharArray())
{
if (c == ' ')
{
spaceCount++;
}
}
if(spaceCount == 1)
{
delimiter = DELIMITER_SPACE;
}
else if(spaceCount == 2)
{
delimiter = DELIMITER_TAB;
}
System.out.println("Course" + courseNumber);
// for each token in the string
while ((j = lineOfText.indexOf(delimiter, (index = ++j))) != -1)
{
System.out.println(courseField[k] + ": " + lineOfText.substring(index, j));
System.out.println("");
outputFile.println(lineOfText.substring(index, j));
counter++;
k++;
}
// extract the last token
if (index > 0)
{
System.out.println("Year: " + lineOfText.substring(index));
outputFile.println(lineOfText.substring(index));
++courseNumber;
}
// for each token in the string
// Course c = new Course(hm.get("Name"), hm.get("Code"),
hm.get("Level"), Integer.parseInt(hm.get("Year")) );
// System.out.println(c);
if(k == 3)
{
k = k - k;
index = 0;
}
delayDisplay();
lineOfText = inputFile.readLine();
} // while(lineOfText != null)
inputFile.close();
outputFile.close();
}
public CourseUtility(String name, String code, String level, String year)
{
if(name == null)
{
this.name = Course.DEFAULT_NAME;
}
else
{
this.name = name;
} // end of if(name == null)
if(code == null)
{
this.code = Course.DEFAULT_CODE;
}
else
{
this.code = code;
} // end of if(code == null)
if(level == null)
{
this.level = DEFAULT_LEVEL;
}
else
{
this.level = level;
} // end of if(level == null)
if(year == null)
{
this.year = null;;
}
else
{
this.year = year;
} // end of if(year == null)
}
private void delayDisplay()
{
try
{
Thread.sleep(1000);
} catch(InterruptedException ex)
{
Thread.currentThread().interrupt();
} // end of try
} // end of method void delayDisplay()
private void convertLevel(String courseLevel)
{
level = DEFAULT_LEVEL;
if(level == "IB") level = "7";
if(level == "Academic")level = "1";
if(level == "Applied") level = "1";
if(level == "ELL") level = "9";
if(level == "Special Education") level = "8";
} // end of method convertLevel(String courseLevel)
public void deleteCourse()
{
private void setCourseFields()
{
courseField[0] = "Name";
courseField[1] = "Level";
courseField[2] = "Code";
courseField[3] = "Year";
} // end of method setCourseFields()
}
I think I see your problem. You are parsing the fields, but not saving them. One thing you can do is save the fields once you have them. Store them in a HashMap, where the key is the name of the field. Then use your HashMap to retrieve the fields and call the constructor:
// for each token in the string
HashMap<String,String> hm = new HashMap<String,String>(); //NEW
while ((j = lineOfText.indexOf(delimiter, (index = ++j))) != -1)
{
hm.put(courseField[k], lineOfText.substring(index, j)); //NEW
System.out.println(courseField[k] + ": " + lineOfText.substring(index, j));
counter++;
k++;
}
// extract the last token
if (index > 0)
{
System.out.println("Year: " + lineOfText.substring(index));
hm.put("Year", lineOfText.substring(index)); //NEW
++courseNumber;
}
//translate level string to a character
char lvl;
String sLevel = hm.get("Level");
if (sLevel.equals("IB"))
lvl = '7';
else if (sLevel.equals("Special Education"))
lvl = '8';
else if (sLevel.equals("Academic"))
lvl = '8';
else if (sLevel.equals("English Language Learners"))
lvl = '1';
else
lvl = ' '; /* unknown level */
//create the Course object
Course c = new Course(hm.get("Name"), hm.get("Code"), lvl , Integer.parseInt(hm.get("Year")) );
I'm about to create two methods for creating and changing customer profiles. Creating profile is no problem. Everything seems to go well there. But, when I shall then go in and change the profile, I get it not to work.
The indexOf() gives me -1, even though the value I search for available :S
Anyone have a good solution to this?
The problem is in the editProfile-method!
public class Profile{
String name;
long id;
int accNr = 1000;
double balance;
}
ArrayList<Profile> profileList = new ArrayList<Profile>();
public boolean newProfile(long id, String name, int amount){
Profile newProfile = new Profile();
Profile accNr = new Profile();
int ACC = accNr.accNr++;
newProfile.accNr = ACC;
newProfile.id = id;
newProfile.name = name;
newProfile.balance = amount;
profileList.add(newProfile);
return true;
}
public void editProfile(long id, String newName){
int ID = (int)id;
System.out.print(ID);
int index = profileList.indexOf(id);
System.out.print(index);
profileList.get(index);
}
The indexOf method will use the equals method to determine if your Profile exists in the list. You must override the equals method in Profile to return the proper result.
Second, it won't find your Profile, because you are passing a long to indexOf, and neither a long nor a Long will be found in the list. If you must retrieve the Profile by a long, then it makes more sense to have a Map<Long, Profile> instead of an ArrayList<Profile>. Then you can call get(id) to retrieve the Profile. Usually, you should override the hashCode method if you override equals, but because a Profile isn't being used as the key here, it's not necessary.
profileList contains Profile instances and you are trying to get the index of a long.
One solution would be overriding equals method in Profile class.
#Override
public boolean equals(Object obj) {
...
}
Another solution (not very recommended) would be looping over elements of profileList and manually checking for matches, like:
for (Profile element : profileList)
if (element.getID() == id)
...
Probably your Profileneeds to override equals and hashCode methods. Eclipse can generate then, Would be like taking your example:
public class Profile {
String name;
long id;
int accNr = 1000;
double balance;
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + accNr;
long temp;
temp = Double.doubleToLongBits(balance);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + (int) (id ^ (id >>> 32));
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Profile other = (Profile) obj;
if (accNr != other.accNr)
return false;
if (Double.doubleToLongBits(balance) != Double
.doubleToLongBits(other.balance))
return false;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
I want to have a Sorted map as follows:
srcAddr, dstAddr, srcPort, dstPort, protocol as keys
and a List of values as
packetLength, timeArrival for each key.
Is it possible to implement them in separate classes? I am confused if it will work this way.
Update:
I am getting an error indicating im not overriding abstract method compareTo(). Can you help me with it?
package myclassifier;
import java.io.Serializable;
import java.util.*;
public class Flows implements Serializable, Comparable {
String srcAddr, dstAddr, srcPort, dstPort, protocol;
public int compareTo(Flows other) {
int res = this.srcAddr.compareTo(other.srcAddr);
if (res != 0) {
return res;
}
res = this.dstAddr.compareTo(other.dstAddr);
if (res != 0) {
return res;
}
res = this.srcPort.compareTo(other.srcPort);
if (res != 0) {
return res;
}
res = this.dstPort.compareTo(other.dstPort);
if (res != 0) {
return res;
}
return this.protocol.compareTo(other.protocol);
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((dstAddr == null) ? 0 : dstAddr.hashCode());
result = prime * result + ((dstPort == null) ? 0 : dstPort.hashCode());
result = prime * result + ((srcAddr == null) ? 0 : srcAddr.hashCode());
result = prime * result + ((srcPort == null) ? 0 : srcPort.hashCode());
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Flows other = (Flows) obj;
if (dstAddr == null) {
if (other.dstAddr != null)
return false;
} else if (!dstAddr.equals(other.dstAddr))
return false;
if (dstPort == null) {
if (other.dstPort != null)
return false;
} else if (!dstPort.equals(other.dstPort))
return false;
if (srcAddr == null) {
if (other.srcAddr != null)
return false;
} else if (!srcAddr.equals(other.srcAddr))
return false;
if (srcPort == null) {
if (other.srcPort != null)
return false;
} else if (!srcPort.equals(other.srcPort))
return false;
return true;
}
}
You can write a, say a 'MyKey' class with srcAddr, dstAddr, srcPort, dstPort and protocol as member variables of it. You have to carefully override the equals and hashCode method of this class. Also this class has to implement the Comparable interface to indicate how your ordering will be determined based on member fields.
You can implement a class MyValue to have packetLength, timeArrival etc as members. This will be the value you want to store in your map.
Use TreeMap to store MyValue against MyKey.
you can implement different classes one for the key
public class Packet implements Serializable, Comparable {
String srcAddr, dstAddr, srcPort, dstPort;
public int compareTo(Object arg0) {
// your sorting logic here
return ...;
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((dstAddr == null) ? 0 : dstAddr.hashCode());
result = prime * result + ((dstPort == null) ? 0 : dstPort.hashCode());
result = prime * result + ((srcAddr == null) ? 0 : srcAddr.hashCode());
result = prime * result + ((srcPort == null) ? 0 : srcPort.hashCode());
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Packet other = (Packet) obj;
if (dstAddr == null) {
if (other.dstAddr != null)
return false;
} else if (!dstAddr.equals(other.dstAddr))
return false;
if (dstPort == null) {
if (other.dstPort != null)
return false;
} else if (!dstPort.equals(other.dstPort))
return false;
if (srcAddr == null) {
if (other.srcAddr != null)
return false;
} else if (!srcAddr.equals(other.srcAddr))
return false;
if (srcPort == null) {
if (other.srcPort != null)
return false;
} else if (!srcPort.equals(other.srcPort))
return false;
return true;
}
}
And one for the data
public class Payload {
Integer packetLength;
Date timeArrival;
}
then when you put payload with a certain key in a sorted map it will be placed in order according to the compareTo method
You want sort your map based on its keys or values? Do you want the keys to be sorted alphabetically or in the order you've specified above? Overall it shouldn't be too difficult:
SorterMap<String, List> mySortedMap = new TreeMap<String, List>(myComparator);
mySortedMap.put("srcAddr", Arrays.asList(new Object[] {packetLengthValue, arrivalTimeValue}))
Where you should implement myComparator to sort the keys according to your requirements.