I have my object class :
public class Test {
private String name;
private int id;
private boolean aBoolean;
public Test(String name, int id, boolean aBoolean) {
this.name = name;
this.id = id;
this.aBoolean = aBoolean;
}
and 2 arrayList of this object:
ArrayList<Test> myFirstList = new ArrayList<>();
ArrayList<Test> mySecondList = new ArrayList<>();
myFirstList.add(new Test("a", 1, false));
myFirstList.add(new Test("b", 2, false));
myFirstList.add(new Test("c", 3, false));
mySecondList.add(new Test("a", 1, false));
mySecondList.add(new Test("b", 2, false));
mySecondList.add(new Test("c", 3, false));
now I need to check that these array list contain objects which have same fields inside; is there any ways but to use fori loop and getting each parameter for compare?
Record
Apparently you want to compare objects of the same class for equality by examining the content of each and every member field.
You get that behavior automatically by using the records feature. This feature is new in Java 16, with early access builds available now. In a record you simply declare the member fields. The compiler implicitly creates the constructor, getters, equals & hashCode, and toString.
So no need for you to override equals wih your own implementation as seen in the other Answers.
Use a record where the primary purpose of a class is to immutably and transparently carry data. In contrast, if your class has a focus on behavior with encapsulated data, or uses other OOP features such as inheritance, then your should use a regular class, not records.
So your code:
public class Test {
private String name;
private int id;
private boolean aBoolean;
public Test(String name, int id, boolean aBoolean) {
this.name = name;
this.id = id;
this.aBoolean = aBoolean;
}
… becomes:
public record Test ( String name , int id , boolean aBoolean ) {}
Using that record:
Test x = new Test ( "Alice" , 42 , true ) ;
Test y = new Test ( "Alice" , 42 , false ) ;
boolean same = x.equals( y ) ; // false because of third field.
Comparing lists
Use List#equals to see if two lists contain equal elements in the same order.
List x = List.of(
new Test ( "Alice" , 1 , true ) ,
new Test ( "Bob" , 2 , true ) ,
new Test ( "Carol" , 3 , true )
);
List y = List.of(
new Test ( "Alice" , 1 , true ) ,
new Test ( "Bob" , 2 , true ) ,
new Test ( "Carol" , 3 , true )
);
boolean same = x.equals( y ) ; // True.
If you override equals() in our class you should be able to sort both lists with myFirstList.sort() and then just use
myFirstList.equals(mySecondList)
This is not related to ArrayList, it is related to your Test Class. As said by #VGR, In order to know if the two objects of the Test Class have the same fields, you have to override the equals method inside your Test class like this:
public class Test {
// your constructors and methods
#Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (!(o instanceof Test)) {
return false;
}
Test t = (Test) o;
// Compare the data members and return accordingly
return this.name.equals(t.name)
&& this.id == t.id;
&& this.aBoolean == t.aBoolean;
}
}
Now to check if two objects in ArrayList are equal, Simply do this:
boolean result = myFirstList.get(indexOfObject1).equals(indexOfObject2);
System.out.println("Result : "+result);
And obviously, to search for equal objects in two ArrayLists, you have to loop through every single object in the first list, then compare that object with all objects in the second list, or implement your own algorithm. That's where data structure and algorithms are used!
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
This is the class
public class Book {
private String name;
private int publicationYear;
public Book(String name, int publicationYear) {
this.name = name;
this.publicationYear = publicationYear;
}
public boolean equals(Book compared) {
if (this == compared) {
return true;
}
if (!(compared instanceof Book)) {
return false;
}
Book comparedBook = (Book) compared;
if (this.name.equals(comparedBook.name)
&& this.publicationYear == comparedBook.publicationYear) {
return true;
}
return false;
}
}
I tried doing this in the main
while (true) {
System.out.println("Name (empty will stop):");
String name = scanner.nextLine();
if (name.isEmpty()) {
break;
}
System.out.println("Publication year:");
int publicationYear = Integer.valueOf(scanner.nextLine());
Book book = new Book(name, publicationYear);
if (!(books.contains(book))) {
books.add(book);
}
So if the user keeps giving a book with the same name and year the program adds them to the list still
You can’t change the signature of Object.equals(Object).
You should use the #Override annotation to catch this kind of error. See tutorial by Oracle.
#Override
public boolean equals(Object compared) {
if (this == compared) {
return true;
}
if (!(compared instanceof Book)) {
return false;
}
Book comparedBook = (Book) compared;
if (this.name.equals(comparedBook.name)
&& this.publicationYear == comparedBook.publicationYear) {
return true;
}
return false;
}
The Answer by Elliott Frisch is correct and wise.
Alternatively, in Java 16+ you could avoid your issue of incorrectly writing the equals method by letting Java handle the implementation of equals.
record
Java 16 brought the records feature. A record is a brief way to write a class whose main purpose is to communicate data transparently and immutably. You simply declare the type and name of each member field. The compiler implicitly creates the constructor, getters, equals & hashCode, and toString.
And by the way, Java offers a class to represent a year: java.time.Year.
public record Book ( String title , Year published ) {}
The implicit methods such as equals are implemented to consider each and every member field.
new Book( "Free To Choose" , Year.of( 1990 ) )
.equals(
new Book( "The Foundation Trilogy" , Year.of( 1974 ) )
)
false
You can define a record locally, or nested or separately. Ditto for enum and for interfaces, in Java 16 and later.
Set
To collect objects while automatically eliminating duplicates, use a Set implementation.
Set< Book > books = new HashSet<>() ;
books.add( new Book( "Free To Choose" , Year.of( 1990 ) ) ) ;
books.add( new Book( "Free To Choose" , Year.of( 1990 ) ) ) ; // Duplicate! Not added, because a set does not allow duplicates.
books.add( new Book( "The Foundation Trilogy" , Year.of( 1974 ) ) ) ;
Interrogate for the size. You’ll find a size of two rather than three.
I have created an object ArrayList,
private ArrayList<Object> objects;
and I am initializing it in a constructor.
public ObjectManager(Handler handler) {
this.handler = handler;
objects = new ArrayList<>();
}
This ArrayList is then painted/added it to a canvas.
public void renderObjects(Graphics g) {
handler.getObjectManager().addObject(new InstanceOfObject(handler, 1000, 1000, g));
}
The method addObject(), adds an object to the ArrayList.
public void addObject(Object e) {
objects.add(e);
}
I would like to remove this object later, by using a similar line of code,
public void removeObject(Object e) {
objects.remove(e);
}
however I do not know how to do that because I do not know how to pass in the object that is being removed. The only way I can think of passing in the object is by doing the following:
handler.getObjectManager().removeObject(new InstanceOfObject(handler, 1000, 1000, g));
I don't even know if this would work because it's removing an "new" object. And even if it does, "g" is not defined. If I define it in the constructor, I have to change many different things which results in an error (usually a NullPointerException), but even then I cannot figure out how to call this method by passing in the Graphics g parameters.
Your Question is not clear, but this might help.
The List interface implemented by ArrayList already offers a remove method. No need for you to re-invent that.
Object reference
To remove an object, keep and pass a reference to the particular object.
Dog alice = new Dog( "Alice" , "Labrador" ) ;
Dog bob = new Dog( "Bob" , "Chihuahua" ) ;
List< Dog > dogs = new ArrayList<>() ;
dogs.add( alice ) ;
dogs.add( bob ) ;
…
dogs.remove( bob ) ;
Index number
Alternatively, remember the slot (index) of the list containing the object you want to remove. Pass that zero-based index number to the remove method.
You can actually find Java's source code on the web (like https://github.com/AdoptOpenJDK/openjdk-jdk11/blob/master/src/java.base/share/classes/java/util/ArrayList.java#L644), or even as src.zip in the JDK itself. So this is how remove() looks like:
public boolean remove(Object o) {
final Object[] es = elementData;
final int size = this.size;
int i = 0;
found: {
if (o == null) {
for (; i < size; i++)
if (es[i] == null)
break found;
} else {
for (; i < size; i++)
if (o.equals(es[i]))
break found;
}
return false;
}
fastRemove(es, i);
return true;
}
and while the loops with the labeled breaks may look a bit esoteric, the important part is the o.equals(): if your "InstanceOfObject" class implements its own equals(), you can make the comparison work with freshly made throwaway instances too.
If I have some many constants like shortName="rule1"; descrition="description1"; rule2/description2; rule3 / description3? and I need to find out the description for a certain shortName, given by a parameter, what would be the best way to do this?
Would it be a enum like this?
public enum Description {
RULE1 ("rule1", "description1"),
RULE2 ("rule2", "description2"),
RULE3 ("rule3", "description3");
private final String shortName;
private final String description;
Description(String shortName, String description) {
this.shortName= shortName;
this.description = description;
}
}
But if I have a method like private String getDescription(String shortName) how can I use the enum to get the description of a shortName declared in enum?
I can't use constants because I have ~200 definitions like this.
An enum defines a type
You asked:
and I need to find out the description for a certain shortName, given by a parameter, what would be the best way to do this?
Would it be a enum like this?
Yes, if those rule1, rule2, and such are all conceptually members of the same type.
For example:
public enum Pet{ DOG , CAT , BIRD , HAMSTER }
public enum Flavor{ VANILLA , CHOCOLATE, STRAWBERRY }
public enum Color{ BURLYWOOD , CORNFLOWER_BLUE, DARK_SLATE_GREY }
Using an enum such as those means you can write other code that is type-safe, ensures valid values, and is more self-documenting.
pictureMaker.displayIceCreamCone( Flavor.CHOCOLATE )
On the other hand, if your values are unrelated, just a hodgepodge of various strings for various purposes, I would use string constants. And if they are resources for localization, use specific localization tools.
You asked:
But if I have a method like private String getDescription(String shortName) how can I use the enum to get the description of a shortName declared in enum?
That question suggests you are passing around the text of the short name as a key to finding the description. But you should not be passing around some string, you should be passing around the enum object. Take, for example, java.time.DayOfWeek enum. You should be passing around DayOfWeek.SATURDAY rather than "SATURDAY".
But if you must, you could implement a static method on your enum to loop through all the enum objects to find one that matches.
// Utility method to loop all the enum objects until finding a match.
public static String getLongStringForShortName ( String shorty )
{
String result = null;
if ( RULE1.shortName.equals( shorty ) ) { result = RULE1.description; }
if ( RULE2.shortName.equals( shorty ) ) { result = RULE2.description; }
if ( RULE3.shortName.equals( shorty ) ) { result = RULE3.description; }
return result;
}
Or, in alternative syntax, use streams to softcode references to each and every enum object.
// Utility method to loop all the enum objects until finding a match.
public static String getLongStringForShortName ( String shorty )
{
String result = "";
Optional < Description > optionalDesc = Arrays.stream( Description.values() ).filter( ( Description d ) -> d.description.equals( shorty ) ).findFirst();
if ( optionalDesc.isPresent() ) { result = optionalDesc.get().description; }
return result;
}
Map
But that code has a smell about it. You likely have the wrong data structure if you do this often, or this is your main purpose. This looks like we are abusing the enum where instead should be using a Map.
Map < String, String > descriptions =
Map.of(
"rule1" , "description1" ,
"rule2" , "description2" ,
"rule3" , "description3"
)
;
String longDesc = descriptions.get( "rule1" );
EnumMap
You could mix the concepts of enum and map. Your Question lacks the context to know if this is right for you or not. But FYI…
Change your enum class to just this:
package work.basil.example;
public enum Description
{
RULE1, RULE2, RULE3;
}
Use an EnumMap to map each of these enum objects to some other object such as a string.
Map < Description, String > descriptionToLongForm = new EnumMap <>( Description.class );
descriptionToLongForm.put( Description.RULE1 , "description1" );
descriptionToLongForm.put( Description.RULE2 , "description2" );
descriptionToLongForm.put( Description.RULE3 , "description3" );
String longDesc = descriptionToLongForm.get( Description.RULE2 );
Or, in alternative syntax, using Map.of. This produces a non-modifiable map.
Map < Description, String > descriptionToLongForm =
Map.of(
Description.RULE1 , "description1" ,
Description.RULE2 , "description2" ,
Description.RULE3 , "description3"
);
String longDesc = descriptionToLongForm.get( Description.RULE2 );
I'm writing a program to sort datetimes by date.
There is a DateTime beginning and a DateTime end.
These are put into their own object from user input.
The DateTime beginning and the DateTime end make up their own term.
So each term has an object of its own with a DateTime beginning and a DateTime end.
What I want to do is to sort all DateTime beginning and all DateTime end by date.
How can I do this? I'm thinking about a comperator but I can't seem to do this on custom objects.
So lets say user imputs one beginning date of 01/01/2000 and one end date of 01/01/2002. This makes up one term.
The user then imputs a second term consisting of a beginning date of 01/01/2001 and an end date of 01/01/2003.
What I now want to do is to sort the dates and make the three new terms which would be:
beginning 01/01/2000 end 01/01/2001
beginning 01/01/2001 end 01/01/2002
beginning 01/01/2002 end 01/01/2003
I'm stuck on how to proceed with this, any ideas?
Put every date in a new collection, sort it by the date, and then create new objects that consist neighbour dates from the collection.
Try:
public static void main(String[] args) {
List<YourClass> list = new ArrayList<>();
list.add(new YourClass(new Date(100000000), new Date(200000000)));
list.add(new YourClass(new Date(150000000), new Date(250000000)));
list.add(new YourClass(new Date(50000000), new Date(300000000)));
System.out.println(list);
List<Date> dates = new ArrayList<>();
for (YourClass yc : list){
if (!dates.contains(yc.beginning)) dates.add(yc.beginning);
if (!dates.contains(yc.end)) dates.add(yc.end);
}
Collections.sort(dates);
List<YourClass> list2 = new ArrayList<>();
for (int i=0; i < dates.size() -1; i++){
list2.add(new YourClass(dates.get(i), dates.get(i+1)));
}
System.out.println(list2);
}
public static class YourClass {
Date beginning;
Date end;
public YourClass(Date beginning, Date end) {
this.beginning = beginning;
this.end = end;
}
#Override
public String toString() {
return "\n" + beginning + " -> " + end ;
}
}
tl;dr
What I want to do is to sort all DateTime beginning and all DateTime end by date.
You can do one or the other but not both.
To sort by start date (seems sensible in practice), implement compareTo method.
return this.getDateRange().getStart().compareTo( thatStart );
To sort by stop date (I do not see any sense in this), implement the Comparator interface.
return
t1.getDateRange().getEnd().compareTo(
t2.getDateRange().getEnd()
)
;
LocalDate
As others noted, you should be using the modern java.time classes, never the terrible old Date/Calendar/SimpleDateFormat classes. For a date-only value, without time-of-day and without time zone, use LocalDate.
LocalDateRange
As the Answer by jbx discusses, you should represent your term’s start and stop dates as a pair. But do not write a class when one already exists. Use LocalDateRange class from the ThreeTen-Extra project. This project adds functionality to the java.time classes.
Comparable
On your Term class, implement Comparable interface to enable simple easy sorting. Add the method compareTo. The obvious approach there would be to compare the starting LocalDate of each Term object’s LocalDateRange object.
The LocalDate class implements compareTo, no we don’ have to.
#Override
public int compareTo ( Object o ) {
if ( this == o ) return 0;
if ( o == null || getClass() != o.getClass() ) return 0;
LocalDate thatStart = ( ( Term ) o ).getDateRange().getStart();
return this.getDateRange().getStart().compareTo( thatStart );
}
See the Java Tutorial on object-ordering.
Sort by stop date
Your Question is not clear, but you seem to be asking to alternatively sort by the ending date. I cannot imagine how this is useful in practical terms. But anyways, the solution is to sort by providing an implementation of the Comparator interface.
#Override
public int compare ( Term t1 , Term t2 ) {
return t1.getDateRange().getEnd().compareTo( t2.getDateRange().getEnd() );
}
Example class
Here is an example Term class. May not be production-quality code, but should get you going in the right direction.
package com.basilbourque.example;
import org.threeten.extra.LocalDateRange;
import java.time.LocalDate;
import java.time.Month;
import java.util.*;
public class Term implements Comparable {
private UUID id;
private LocalDateRange dateRange;
// Constructor
public Term ( LocalDate start , LocalDate stop , UUID id ) {
Objects.requireNonNull( start ); // TODO: Add more such checks for all arguments.
if ( start.getYear() < 2015 ) { // TODO: Add more such checks for too far into the past or future, for both start and for stop.
throw new IllegalArgumentException( "Year of start date is too far in the past. Message # afcd30a0-b639-4ccf-b064-18cc2ea8587b." );
}
this.id = id;
this.dateRange = LocalDateRange.of( start , stop );
}
// Alternative constructor.
public Term ( LocalDateRange dateRange , UUID id ) {
this( dateRange.getStart() , dateRange.getEnd() , id );
}
// --------| Object |-------------------------
#Override
public String toString ( ) {
return "Term{ " +
"id=" + id +
" | dateRange=" + dateRange +
" }";
}
public UUID getId ( ) {
return id;
}
public LocalDateRange getDateRange ( ) {
return dateRange;
}
#Override
public boolean equals ( Object o ) {
if ( this == o ) return true;
if ( o == null || getClass() != o.getClass() ) return false;
Term term = ( Term ) o;
return this.getId().equals( term.getId() );
}
#Override
public int hashCode ( ) {
return Objects.hash( this.getId() );
}
#Override
public int compareTo ( Object o ) {
if ( this == o ) return 0; // If same object.
if ( o == null || getClass() != o.getClass() ) return 0;
LocalDate thatStart = ( ( Term ) o ).getDateRange().getStart();
return this.getDateRange().getStart().compareTo( thatStart );
}
static public class StopDateComparator implements Comparator < Term > {
#Override
public int compare ( Term t1 , Term t2 ) {
return t1.getDateRange().getEnd().compareTo( t2.getDateRange().getEnd() );
}
}
}
Try it.
public static void main ( String[] args ) {
Term t1 = new Term( LocalDate.of( 2018 , Month.JUNE , 23 ) , LocalDate.of( 2018 , Month.JULY , 23 ) , UUID.randomUUID() );
Term t2 = new Term( LocalDate.of( 2018 , Month.JANUARY , 23 ) , LocalDate.of( 2018 , Month.DECEMBER , 23 ) , UUID.randomUUID() );
Term t3 = new Term( LocalDate.of( 2018 , Month.MARCH , 23 ) , LocalDate.of( 2018 , Month.APRIL , 23 ) , UUID.randomUUID() );
List < Term > terms = new ArrayList <>( List.of( t1 , t2 , t3 ) );
System.out.println( "Before natural sort: " + terms );
Collections.sort( terms );
System.out.println( "After natural sort: " + terms );
Collections.sort( terms , new Term.StopDateComparator() );
System.out.println( "After Comparator sort: " + terms );
}
Before natural sort: [Term{ id=27c0b9e6-076f-4ded-9bbd-bf1a2c7914bc | dateRange=2018-06-23/2018-07-23 }, Term{ id=792bf365-eca4-460b-afad-c5cf62cf9a29 | dateRange=2018-01-23/2018-12-23 }, Term{ id=c49f79e1-11cd-4865-aa46-8fbf3c85dbfd | dateRange=2018-03-23/2018-04-23 }]
After natural sort: [Term{ id=792bf365-eca4-460b-afad-c5cf62cf9a29 | dateRange=2018-01-23/2018-12-23 }, Term{ id=c49f79e1-11cd-4865-aa46-8fbf3c85dbfd | dateRange=2018-03-23/2018-04-23 }, Term{ id=27c0b9e6-076f-4ded-9bbd-bf1a2c7914bc | dateRange=2018-06-23/2018-07-23 }]
After Comparator sort: [Term{ id=c49f79e1-11cd-4865-aa46-8fbf3c85dbfd | dateRange=2018-03-23/2018-04-23 }, Term{ id=27c0b9e6-076f-4ded-9bbd-bf1a2c7914bc | dateRange=2018-06-23/2018-07-23 }, Term{ id=792bf365-eca4-460b-afad-c5cf62cf9a29 | dateRange=2018-01-23/2018-12-23 }]
abuts
If your Term objects should run up against one another in succession, you can test for that using the LocalDateRange::abuts method.
The approach in comparing is Half-Open, where the beginning is inclusive while the ending is exclusive. So a year starts on the first of the year and runs up to, but does not include, the first of the following year. You show this in your examples in the Question.
I assume the question is not just about sorting, but also about splitting overlapping intervals into smaller segments. You have to play a lot with Interval Arithmetic.
With Java 8 you can start by encoding your 'terms' as a time Interval, which in itself is Comparable. The second part would be to split your intervals into multiple ones if the user specifies overlapping ones.
class Interval implements Comparable<Interval> {
private final LocalDateTime start;
private final LocalDateTime end;
public Interval(LocalDateTime start, LocalDateTime end) {
this.start = start;
this.end = end;
}
public int compareTo(Interval that) {
return this.start.compareTo(that.start);
}
public boolean overlaps(Interval that) {
return !this.isBefore(that) && !this.isAfter(that);
}
public boolean contains(Interval that) {
return this.start.isBefore(that.start) && this.end.isAfter(that.end);
}
public boolean isBefore(Interval that) {
return this.end.isBefore(that.start);
}
public boolean isAfter(Interval that) {
return this.start.isAfter(that.end);
}
public Set<Interval> fragment(Interval that) {
if (that.start.isBefore(this.start)) {
return that.fragment(this);
}
Set<Interval> result = new HashSet<>();
if (this.end.isBefore(that.start)) {
result.add(this);
result.add(that);
result.add(new Interval(this.end, that.start));
} else if ((this.end.isAfter(that.start) && this.end.isBefore(that.end)) {
result.add(new Interval(this.start, that.start);
result.add(new Interval(that.start, this.end);
result.add(new Interval(this.end, that.end));
} else if (this.end.isAfter(that.end)) {
result.add(new Interval(this.start, that.start);
result.add(new Interval(that);
result.add(new Interval(that.end, this.end));
}
}
}
You can now keep them sorted, since Intervals are comparable by start date.
Whenever the user enters a new Interval (term) you have to go through the list and checks whether it contains() the existent interval, or whether it comes before it, with isBefore() or isAfter(). If it overlaps() you have to be careful if to also check whether it overlaps with the next interval in the list.
You can then call fragment() which will combine the 2 intervals together into smaller ones. You need to be careful to remove the previous ones. So maybe makes sense to just go through the list and check if they overlap or not. If you reach the end you can still use fragment() to combine two disjoint intervals.
What if I'll use switch in getByIntValue()? Is it really neccessary to use a SparseArray?
public enum Gender {
Unknown(0),
Male(1),
Female(2);
private static final SparseArray<Gender> lookupTable = new SparseArray<Gender>();
static {
for (final Gender gender : EnumSet.allOf(Gender.class)) {
lookupTable.put(gender.intValue, gender);
}
}
private final int intValue;
public static Gender getByIntValue(int val) {
return lookupTable.get(val);
}
private Gender(int intValue) {
this.intValue = intValue;
}
public int getIntValue() {
return intValue;
}
}
Since your int values go from 0 to 2, without hole, you could indeed simply use an array. A switch would also be fine, although it would probably be slightly slower than an array lookup. But unless you call the method billions of times, it won't make any noticeable difference. Use what you find the clearest and easiest to understand and maintain.
If you have posted realistic int values, then you don't need to set them explicitly on each enum member, and don't need switch. Just use
Gender.values()[intValue]
List.copyOf( EnumSet.allOf( Gender.class ) )
Caveat: This exercise in optimization seems silly for all but the most extreme scenario, as mentioned by JB Nizet. For real work, I would probably recommend the solution seen in the Answer by Marko Topolnik. But, for fun, I swung a bat at this ball.
Seems the goal is to render a static unmodifiable collection with very fast access by the given numbers 0, 1, 2.
As of Java 10, we have these new implemented (“default”) methods on the List interface: List.of & List.copyOf. These produce an unmodifiable collection. Though the backing implementation is undocumented and subject to change, I will assume it is something akin to an array with similar performance. Performance might even be faster than a conventional array, if the backing implementation detected the presence of an EnumSet and used some kind of bit vector.
I populate the List by passing an EnumSet to List.copyOf( Collection ).
So, this:
private static final SparseArray<Gender> lookupTable = new SparseArray<Gender>();
static {
for (final Gender gender : EnumSet.allOf(Gender.class)) {
lookupTable.put(gender.intValue, gender);
}
}
…becomes this:
private static final List < Gender > lookupTable = List.copyOf( EnumSet.allOf( Gender.class ) );
Entire class, with main for demo.
package com.basilbourque.example;
import java.util.EnumSet;
import java.util.List;
public enum Gender {
UNKNOWN( 0 ),
MALE( 1 ),
FEMALE( 2 );
private static final List < Gender > lookupTable = List.copyOf( EnumSet.allOf( Gender.class ) );
private final int intValue;
public static Gender getByIntValue ( int val ) {
return lookupTable.get( val );
}
public int getIntValue () {
return intValue;
}
// Constructor
private Gender ( int intValue ) {
this.intValue = intValue;
}
public static void main ( String[] args ) {
// Testing.
System.out.println( Gender.UNKNOWN.intValue );
System.out.println( Gender.getByIntValue( 0 ) );
System.out.println( "----" );
System.out.println( Gender.MALE.intValue );
System.out.println( Gender.getByIntValue( 1 ) );
System.out.println( "----" );
System.out.println( Gender.FEMALE.intValue );
System.out.println( Gender.getByIntValue( 2 ) );
}
}
When run.
0
UNKNOWN
1
MALE
2
FEMALE
By the way, as the biological default, FEMALE should come before MALE.