Site Search:

Hell.java

Back>



import java.util.*;
public class Hell {
    public static final Devil d1 = new Devil();
    private static Map<Devil, String> jail;
    public static void main(String...args) {
        jail = new TreeMap<>( (o1, o2) -> ((Devil)o1).claw - ((Devil)o2).claw);
        //What helped Diablo to escape?
        jail.put(new Devil(), "Baal");
        jail.put(d1, "Diablo");
        jail.keySet().forEach(d -> d.kill()); 
        
                
        if (d1.alive) System.out.println("Diablo escaped!!");
        jail = new HashMap<> (jail);
        jail.put(d1, "Diablo");
        jail.keySet().forEach(d -> d.alive = false); 
        
        System.out.println(jail.size());
        System.out.println(jail.keySet().iterator().next().alive);
        System.out.println(jail.values().iterator().next());
        if (d1.alive) System.out.println("Diablo escaped!!");
    }
      public interface Evil {public void kill();}
    private static class Devil implements Evil{
        public void kill() {alive = false;}
        private boolean alive = true;
        private int claw = 666;
        public boolean equals(Object obj) {
            if(!(obj instanceof Devil)) return false;
            Devil d = (Devil)obj;
            return (claw - d.claw) == 0 ? true: false;
        }
        public int hashCode() {return claw;}
    }

}