3

Java 21中字符、集合等功能改进与增强简介

 1 year ago
source link: https://www.jdon.com/67142.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Java 21中字符、集合等功能改进与增强简介

Java 21 引入了各种令人兴奋的语言和集合框架新增功能。

与字符属性、字符串操作和集合相关的新方法为开发人员的代码提供了增强的功能和灵活性。

这些新增内容旨在提高 Java 开发人员的生产力和性能。

字符的增强:

  • Character.isEmoji(int codePoint):该方法根据Unicode Emoji(Unicode Emoji技术标准#51)中定义的Unicode属性来确定一个字符是否被认为是一个表情符号。例如,执行Character.isEmoji(9203)将返回true,因为字符 ,它的代码点为9203。
  • Character.isEmojiPresentation(int codePoint):与isEmoji()类似,该方法检查一个字符是否具有Emoji Presentation属性。它有助于识别在渲染时显示为emojis的字符。
boolean isEmoji = Character.isEmoji(9203);
System.out.println(isEmoji);

输出结果将是true,因为代码点为9203()的字符被认为是一个表情符号。

更多:Java documentation.

StringBuffer和StringBuilder的改进:
Java 21用 repeat()方法增强了StringBuffer和StringBuilder类,该方法允许重复连接字符或字符序列。repeat()的两个重载版本如下:

  • StringBuffer.repeat(int codePoint, int count):在StringBuffer序列中重复指定codePoint参数的字符串表示的计数副本。例如,执行buffer.repeat(9203, 5)会在StringBuffer中追加5次,结果是"(无个小酒杯表情符号)"。
  • StringBuffer.repeat(CharSequence cs, int count):向StringBuffer追加指定的CharSequence cs的计数副本。同样,这些方法在StringBuilder类中也可用。
var buffer = new StringBuffer();
buffer.repeat(9203, 5);
System.out.println(buffer);

输出结果将是 (无个小酒杯表情符号),因为使用repeat()方法,代码点9203()重复了五次。

字符串的增强:
Java 21在String类中引入了两个新方法,即indexOf(String str, int beginIndex, int endIndex)和indexOf(int ch, int beginIndex, int endIndex),它们扩展了在一个字符串的指定范围内的搜索能力。

  • String.indexOf(String str, int beginIndex, int endIndex):返回指定子串在字符串给定索引范围内第一次出现的索引。该方法提供的结果与调用s.substring(beginIndex, endIndex).indexOf(str) + beginIndex相同。如果indexOf(String)方法返回一个非负数的索引,就会返回;否则,就会返回-1。
  • String.indexOf(int ch, int beginIndex, int endIndex):返回指定字符在字符串给定范围内第一次出现的索引。搜索从beginIndex开始,在endIndex之前停止。该方法支持从0到0xFFFF范围内的字符,以及其他Unicode码位。

Java 21还用splitWithDelimiters(String regex, int limit)方法加强了String类。这个方法根据给定的正则表达式分割字符串,并返回字符串和匹配的分隔符。比如说:

var booAndFoo = "boo:::and::foo";
String[] splits = booAndFoo.splitWithDelimiters(":+", 3);
System.out.println("splits = " + Arrays.toString(splits));

输出结果将是:splits = [boo, ::, and, ::, foo],其中字符串围绕:+分隔符进行分割,将结果限制在最多三个分割。

集合Collection框架的增强:
Java 21在集合类中引入了几个新方法,增强了集合框架的功能。

  • Collections.newSequencedSetFromMap(SequencedMap<E, Boolean> map):这个方法返回一个由指定的SequencedMap支持的SequencedSet。得到的集合保持了背靠地图的排序、并发和性能特征。这使得开发者能够获得与任何SequencedMap实现相对应的SequencedSet实现。
  • Collections.shuffle(List<?> list, RandomGenerator rnd):该方法使用所提供的随机性源对指定列表的元素进行随机排列。假设有一个公平的随机性来源,那么这些排列发生的可能性是相同的。
  • Collections.unmodifiableSequencedCollection(SequencedCollection<? extends T> c):返回指定SequencedCollection的一个不可修改的视图。它允许对底层集合进行只读访问,如果试图修改,则抛出UnsupportedOperationException。
  • Collections.unmodifiableSequencedSet(SequencedSet<? extends T> s):返回指定SequencedSet的一个不可修改的视图,提供对该集合的只读访问,同时不允许修改。
  • Collections.unmodifiableSequencedMap(SequencedMap<? extends K, ? extends V> m):与前面的方法类似,它返回指定SequencedMap的不可修改的视图,允许只读操作。

SequencedCollection和SequencedMap是Java 21中引入的新接口,分别保留了元素和条目的顺序。

HashMap和HashSet的改进:
Java 21为创建HashMap和HashSet实例引入了方便的工厂方法。

  • Collections.newHashMap(int numMappings):创建一个新的、空的、适合预期映射数量的HashMap。该地图使用默认的负载因子0.75,其初始容量通常大到足以容纳预期的映射数量而不需要调整大小。
  • Collections.newHashSet(int numElements):创建一个新的、空的、适合预期元素数量的HashSet。该集合也使用默认的负载因子0.75,初始容量可以有效地容纳预期的元素数量。

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK