Ad

This approach totally constructs two sets for each parity representable by an int type. The contains(Object), iterator(), and size() should work as expected.

Code
Diff
  • import java.util.Set;
    
    public class EvenOrOdd {
        public static final Set<Integer> EVEN;
    
        public static final Set<Integer> ODD;
    
        public static String evenOrOdd(final int n) {
            if (EVEN.contains(n))
                return "Even";
            if (ODD.contains(n))
                return "Odd";
            throw new InternalError("Unable to determine the parity of " + n);
        }
    
        static{EVEN=new java.util.AbstractSet<Integer>(){@Override public boolean contains(final Object o){return o instanceof final Integer x&&(x&1)==0;}@Override public java.util.Iterator<Integer>iterator(){return new java.util.Iterator<Integer>(){int current = 0;@Override public boolean hasNext(){return current<2<<31;}public Integer next(){if(!hasNext())throw new java.util.NoSuchElementException();final int current=this.current;this.current++;this.current++;return current;}};}@Override public int size(){return 1<<020;}};}
    
        static{ODD=new java.util.AbstractSet<Integer>(){@Override public boolean contains(final Object o){return o instanceof final Integer x&&(x&1)==1;}@Override public java.util.Iterator<Integer>iterator(){return new java.util.Iterator<Integer>(){int current = 1;@Override public boolean hasNext(){return current<2<<31+1;}public Integer next(){if(!hasNext())throw new java.util.NoSuchElementException();final int current=this.current;this.current++;this.current++;return current;}};}@Override public int size(){return 1<<020;}};}
    }
    
    • class EvenOrOdd {
    • static String evenOrOdd(int n) {
    • return (n & 1) == 1 ? "Odd" : "Even";
    • }
    • }
    • import java.util.Set;
    • public class EvenOrOdd {
    • public static final Set<Integer> EVEN;
    • public static final Set<Integer> ODD;
    • public static String evenOrOdd(final int n) {
    • if (EVEN.contains(n))
    • return "Even";
    • if (ODD.contains(n))
    • return "Odd";
    • throw new InternalError("Unable to determine the parity of " + n);
    • }
    • static{EVEN=new java.util.AbstractSet<Integer>(){@Override public boolean contains(final Object o){return o instanceof final Integer x&&(x&1)==0;}@Override public java.util.Iterator<Integer>iterator(){return new java.util.Iterator<Integer>(){int current = 0;@Override public boolean hasNext(){return current<2<<31;}public Integer next(){if(!hasNext())throw new java.util.NoSuchElementException();final int current=this.current;this.current++;this.current++;return current;}};}@Override public int size(){return 1<<020;}};}
    • static{ODD=new java.util.AbstractSet<Integer>(){@Override public boolean contains(final Object o){return o instanceof final Integer x&&(x&1)==1;}@Override public java.util.Iterator<Integer>iterator(){return new java.util.Iterator<Integer>(){int current = 1;@Override public boolean hasNext(){return current<2<<31+1;}public Integer next(){if(!hasNext())throw new java.util.NoSuchElementException();final int current=this.current;this.current++;this.current++;return current;}};}@Override public int size(){return 1<<020;}};}
    • }