Exploring Java 21's Intriguing Additions (Part 1)
Learn about a few notable features that stand out in the upcoming Java 21, in an overview of their functionalities and potential use cases.
Join the DZone community and get the full member experience.
Join For FreeJava 21 is going to introduce several intriguing additions that enhance the language's capabilities. This article aims to discuss a few notable features that stand out, providing an overview of their functionalities and potential use cases.
Character Enhancements
Character.isEmoji(int codePoint)
: This method determines whether a character is considered an emoji based on its Unicode properties defined in Unicode Emoji (Unicode Emoji Technical Standard #51). For example, executingCharacter.isEmoji(9203)
would returntrue
for the character ⏳, which has the code point 9203.Character.isEmojiPresentation(int codePoint)
: Similar toisEmoji()
, this method checks if a character has the Emoji Presentation property. It helps identify characters that display as emojis when rendered.
Example:
boolean isEmoji = Character.isEmoji(9203); System.out.println(isEmoji);
The output will be true
because the character with code point 9203 (⏳) is considered an emoji.
You can find more details about these methods in the Java documentation.
StringBuffer and StringBuilder Improvements
Java 21 enhances the StringBuffer
and StringBuilder
classes with the repeat()
method, which allows repetitive concatenation of characters or character sequences. The two overloaded versions of repeat()
are as follows:
StringBuffer.repeat(int codePoint, int count)
: Repeatscount
copies of the string representation of the specifiedcodePoint
argument in theStringBuffer
sequence; for instance, executingbuffer.repeat(9203, 5)
would append ⏳ five times to theStringBuffer
, resulting in "⏳⏳⏳⏳⏳"StringBuffer.repeat(CharSequence cs, int count)
: Appendscount
copies of the specifiedCharSequence
cs
to theStringBuffer
; Similarly, these methods are also available in theStringBuilder
class.
Example:
var buffer = new StringBuffer(); buffer.repeat(9203, 5); System.out.println(buffer);
The output will be ⏳⏳⏳⏳⏳
, as the code point 9203 (⏳) is repeated five times using the repeat()
method.
String Enhancements
Java 21 introduces two new methods in the String
class, namely indexOf(String str, int beginIndex, int endIndex)
and indexOf(int ch, int beginIndex, int endIndex)
, which expand the searching capabilities within a specified range of a string.
String.indexOf(String str, int beginIndex, int endIndex)
: Returns the index of the first occurrence of the specified substring within the given index range of the string; This method provides the same result as invokings.substring(beginIndex, endIndex).indexOf(str) + beginIndex
. If theindexOf(String)
method returns a non-negative index, it is returned; otherwise, -1 is returned.String.indexOf(int ch, int beginIndex, int endIndex)
: This returns the index of the first occurrence of the specified character within the given range of the string. The search starts atbeginIndex
and stops beforeendIndex
. This method supports characters in the range from 0 to 0xFFFF, as well as other Unicode code points.
Java 21 also enhances the String
class with a splitWithDelimiters(String regex, int limit)
method. This method splits a string based on a given regular expression and returns both the strings and the matching delimiters. For example:
var booAndFoo = "boo:::and::foo"; String[] splits = booAndFoo.splitWithDelimiters(":+", 3); System.out.println("splits = " + Arrays.toString(splits));
The output will be: splits = [boo, :::, and, ::, foo]
, where the string is split around the :+
delimiter, limiting the result to a maximum of three splits.
Collections Framework Enhancements
Java 21 introduces several new methods in the Collections
class that enhance the Collections Framework's functionality.
Collections.newSequencedSetFromMap(SequencedMap<E, Boolean> map)
: This method returns aSequencedSet
backed by the specifiedSequencedMap
. The resulting set maintains the ordering, concurrency, and performance characteristics of the backing map. This enables developers to obtain aSequencedSet
implementation corresponding to anySequencedMap
implementation.Collections.shuffle(List<?> list, RandomGenerator rnd)
: This method randomly permutes the elements of the specified list using the provided source of randomness. The permutations occur with equal likelihood, assuming a fair source of randomness.Collections.unmodifiableSequencedCollection(SequencedCollection<? extends T> c)
: This returns an unmodifiable view of the specifiedSequencedCollection
. It allows read-only access to the underlying collection, throwing anUnsupportedOperationException
if modification is attempted.Collections.unmodifiableSequencedSet(SequencedSet<? extends T> s)
: Returns an unmodifiable view of the specifiedSequencedSet
, providing read-only access to the set while disallowing modificationsCollections.unmodifiableSequencedMap(SequencedMap<? extends K, ? extends V> m)
: Similar to the previous methods, this returns an unmodifiable view of the specifiedSequencedMap
, allowing read-only operations.
SequencedCollection and SequencedMap are new interfaces introduced in Java 21 that preserve the order of elements and entries, respectively.
To know more about Sequence Collection, read Collections Framework Makeover.
HashMap and HashSet Improvements
Java 21 introduces convenient factory methods for creating HashMap
and HashSet
instances.
Collections.newHashMap(int numMappings)
: This creates a new, emptyHashMap
suitable for the expected number of mappings. The map uses a default load factor of 0.75, and its initial capacity is typically large enough to accommodate the expected number of mappings without requiring resizing.Collections.newHashSet(int numElements)
: This creates a new, emptyHashSet
suitable for the expected number of elements. The set also uses a default load factor of 0.75, with an initial capacity that can accommodate the expected number of elements efficiently.
Conclusion
If you want to experiment with these new features can download the OpenJDK from the OpenJDK JDK 21 Early-Access Builds.
Another alternative is to use SDKMan, a software development kit manager, to download and manage different versions of Java. SDKMan can be used via the command line, which can make the process easier for developers who prefer this method.
However, these are early-access builds, so they may not be as stable as the final release, scheduled for September 2023, and are intended for testing and feedback purposes.
Nonetheless, Java 21 introduces various exciting language and collections framework additions. The new methods related to character properties, string manipulation, and collections provide developers with enhanced functionality and flexibility in their code. These additions aim to improve the productivity and performance of Java developers.
Note: To explore the complete set of new functionalities introduced in Java 21, stay tuned for future articles discussing additional features.
Don't forget to share this post!
Published at DZone with permission of A N M Bazlur Rahman, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments