Tuesday, November 6, 2007

Minor API fixes for JDK 7

Josh, Doug and I are proposing a handful of minor API additions to the Java Class Libraries (lang, util, math and reflect) for JDK 7.

The very quick overview of our recommendations:
  1. Static compare() methods for all non-void wrapper classes, not just Double and Float.
  2. Static hashCode() methods on all non-void wrapper classes.
  3. Integer.mod() and Long.mod()
  4. RoundingMode.round(double)
  5. Arrays.concat()
  6. EnumSet.complementOf(Collection)
  7. All JDK maps should implement putIfAbsent(), etc.
  8. Proxy.newProxyInstance(type, handler)
We're pretty confident that these changes will make it into JDK 7, barring any particular controversy that could develop.

Please have a read over the document. What do you think about our proposals? What do you think we've missed?

19 comments:

konberg said...

I frequently need to add a single element to an array. Arrays.concat(T[], T) would be helpful. Arrays.concat(T[], T...) would be fine too. Arrays.concat(T[], new T[] { t }) would still be annoying.

This happens frequently when writing Eclipse IDE plug-ins

Stephen Colebourne said...

Whilst I think this list is a start, I don't think it goes far enough.

Commons lang and commons collections contains a whole wealth of ideas, as do similar utility projects elsewhere. My hope is that these methods won't be added to the JDK simply because they come from Google. I'd like to see a more rational process for determining what gets added and what doesn't.

And yes, that might mean a "Java 7 core libraries change JSR".

Kevin Bourrillion said...

I don't think it goes far enough either, but it is NOT our intent to say "these are the ONLY library changes we want to let in." These happen to be the things that Doug and Josh and I were able to reach consensus on so far, so we put them in a document that bears our names; that's all.

You sent me some ideas in email; thanks for that (you should post them here too!)

Unknown said...

+1

Great work, guys!

Unknown said...

Are we going to have some of the Google Collections goodies too in Java 7 ? I think that will be great !

Chandra said...

How about Enum.valueOf(int ordinal) method? Today, only Enum.valueOf(String name) method is generated by compiler. Enum.values() returns an array which result in a lot of boiler plate code in enum classes to support such method.

Chandra said...

1) Trivial String test method in java.lang.String.
public static boolean isTrivial(String s)
A trivial string is either null or made of only whitespace.

2) Try parse methods for Numbers which don't throw NumberFormatException.
int Integer.tryParse(String s, int defaultValue)
int Integer.tryParse(String s, int redix, int defaultValue)

phil varner said...

Would putIfAbsent use reference or value equality?

Paulo Faria said...

I think values since if reference it would be caught in most equals implementations.

Paulo Faria said...

I also have a suggestion.
How about 2 auxiliar methods like this:
/**
* Close closeables. Use this in a finally clause.
*/
public static void close(Closeable... closeables) {
for (Closeable c : closeables) {
if (c != null) {
try {
c.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}

and

/**
* If possible reads an object, if not tries to instanciate the
* object with the given Callable. This allows lazy instantiation.
* If an exception occurs in the lazy instantiantion it returns null.
*/
public static T T readObject(String objectLocation, Callable t) {
ObjectInputStream s = null;
try {
s = new ObjectInputStream(new BufferedInputStream(new FileInputStream(objectLocation)));
return (T) s.readObject();
} catch (Exception ex) {
try {
return (T) t.call();
} catch (Exception e) {
e.printStackTrace();
}
} finally {
close(s);
}
return null;
}

Sultan Rehman said...

What about calling the new Map intf MapEx or ExMap? Pretty descriptive of its nature and still a fairly compact name, IMHO.

Paulo Faria said...

If you're trying to add a method to the Map interface your extensions dovetail nicely with the proposed extension methods. Just a probably unneeded FYI.

Jesse Kuhnert said...

I was pleasantly not surprised to see that you guys have a Join class in your collections API.

Man, it would be ~so~ nice if that were supported (somehow) by any object of type Collection or array. ..but it'd be a huge task to implement...

combattery84 said...

Dell Inspiron 600m battery
Dell Inspiron 8100 Battery
Dell Y9943 battery
Dell Inspiron 1521 battery
Dell Inspiron 510m battery
Dell Latitude D500 battery
Dell Latitude D520 battery
Dell GD761 battery
Dell NF343 battery
Dell D5318 battery
Dell G5260 battery
Dell Inspiron 9200 battery
Dell Latitude C500 battery
Dell HD438 Battery
Dell GK479 battery
Dell PC764 battery
Dell KD476 Battery
Dell Inspiron 1150 battery
Dell inspiron 8500 battery
Dell Inspiron 4100 battery
Dell Inspiron 4000 battery
Dell Inspiron 8200 battery
Dell FK890 battery
Dell Inspiron 1721 battery

Dell Inspiron 1300 Battery
Dell Inspiron 1520 Battery
Dell Latitude D600 Battery
Dell XPS M1330 battery
Dell Latitude D531N Battery
Dell INSPIRON 6000 battery
Dell INSPIRON 6400 Battery
Dell Inspiron 9300 battery

tpjewelry said...

Other ways to unlock trapped cash thomas sabo is in the form of selling thomas sabo shop silverware, silver flatware, sterling silver thomas sabo jewellery and scrap silver. Each of these thomas sabo schmuck will fetch different values depending on charm club thomas sabo the product and purity factors. sabo charm club With the current economic condition, selling thomas sabo 2010 precious metals, either pure or scrap, has gained thomas sabo sales a lot of importance since it thomas sabo reduziert has great intrinsic value attached to it and selling the scrap is one of the smartest ways of making money.

karthik reddy said...

I don't think these have made it into JDK7. Hopefully JDK8 will be blessed with these. Fingers crossed.

mohit said...

nice post thanks for sharing
appvn apk ios
tutuapp apk ios

manimaran said...

Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care
http://rexapparels.com/boxer-manufacturers-in-tirupur-india/
http://rexapparels.com/track-pants-manufacturers-in-tirupur-india/
http://rexapparels.com/innerwear-manufacturers-in-tirupur-india/
http://rexapparels.com/buying-office-in-tirupur-india/
http://rexapparels.com/export-surplus-t-shirts-in-tirupur-india/
http://rexapparels.com/t-shirt-manufacturer-in-tirupur-india/

Unknown said...

Organic Chemistry tutor
hadoop training in chennai
Drilling consultants
Online drilling consultancy