DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • I Built a Java Version Manager by Fixing Other Tools' Open Bugs
  • A Spring Boot App With Half the Startup Time
  • Alternative Structured Concurrency
  • Optimizing Java Applications for Arm64 in the Cloud

Trending

  • Java 8 Optional - Avoid Null and NullPointerException Altogether - and Keep It Pretty
  • Kafka With Spring Boot: Introduction
  • AI-Driven Integration in Large-Scale Agile Environments
  • What Cloud Engineers Actually Need to Know About AI Infrastructure

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Java
  4. JDK 11: New Default Collection Method toArray(IntFunction) [Snippet]

JDK 11: New Default Collection Method toArray(IntFunction) [Snippet]

Check out this post on how to use the new default Collection method toArray(IntFunction) in JDK 11. Click here to get the code now.

By 
Dustin Marx
·
Jul. 27, 18 · Code Snippet
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
15.8K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

The "JDK 11 Early-Access Release Notes" indicate that Early Access Build 20 of JDK 11 includes a new default method on the Collection interface that "allows the collection's elements to be transferred to a newly created array of the desired runtime type." This new default method, Collection.toArray(IntFunction), works similarly to the same-named method already available on the Stream interface, Stream.toArray(IntFunction).

The next code listing demonstrates a new JDK 11 default Collection method in action (on a Set in this case).

final Set<String> names = Set.of("Fred", "Wilma", "Barney", "Betty");  
out.println(Arrays.toString(names.toArray(String[]::new)));  


Because I used an (unordered) set, the order of the String in the generated array can be different than the order of the Stringspecified for initialization of the Set. This is demonstrated in the next screen snapshot. This also indicates that I'm using JDK 11 Early Access Build 23 for this example).

Image title

Many of us use the Java collections more frequently than arrays, but there are times when we need to convert these collections to arrays. The default method Collection.toArray(IntFunction) provides a highly convenient mechanism for this. There was already a similar method on Collecton,Collection.toArray(T[]), and the existence of these two methods means that it is no longer possible to pass null to the Collection.toArray(-) method. During this, the compiler is unable to distinguish them and will report the error message "reference to toArrayis ambiguous." This is not much of a price to pay since both methods throw a NullPointerException anyway when null is passed to them.

Java (programming language) Java Development Kit

Published at DZone with permission of Dustin Marx. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • I Built a Java Version Manager by Fixing Other Tools' Open Bugs
  • A Spring Boot App With Half the Startup Time
  • Alternative Structured Concurrency
  • Optimizing Java Applications for Arm64 in the Cloud

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook