What methods should go into a java.util.Objects class in JDK 7?

Jason Mehrens jason_mehrens at hotmail.com
Fri Oct 2 22:14:35 UTC 2009


> I think a better name would be "defaultToString" since it is the default 
> toString from Object. However, I haven't ever heard anyone else request 
> easier access to the default toString before so I'm not convinced this 
> should go into Objects.
> 
> -Joe

 

One use case is implementing toString for identity maps and collections.  The current IdentityHashMap is immune to poison objects except for its toString implementation. http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6579224

 

Another point of confusion is that the toString does not match what is going on in equals.  Take the following code:

 

 public static void main(String[] args) {
    Map<Integer, Integer> dis = 
        new IdentityHashMap<Integer, Integer>(one());
    Map<Integer, Integer> dat = 
        new IdentityHashMap<Integer, Integer>(one());

    System.out.println(dis.equals(dat));
    System.out.println(dis.toString().equals(dat.toString()));
  }
    
  private static Map<Integer, Integer> one() {
    Integer kv = new Integer(256);
    return Collections.singletonMap(kv, kv);
  }

 

 

It prints false and then true.  If defaultToString was used on the key/value pairs it would print false and then false making it clear why the two maps are not equal.  Something to consider if a new reference map or identity map is added to the JDK (jsr166/extra166y).

 

Jason


 
 		 	   		  
_________________________________________________________________
Hotmail: Free, trusted and rich email service.
http://clk.atdmt.com/GBL/go/171222984/direct/01/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/core-libs-dev/attachments/20091002/f2736bba/attachment.html>


More information about the core-libs-dev mailing list