草庐IT

ImmutableCollections

全部标签

Java 9、Set.of() 和 Map.of() 可变参数重载

这个问题在这里已经有了答案:WhatisthepointofoverloadedConvenienceFactoryMethodsforCollectionsinJava9(6个回答)关闭5年前。我正在研究Immutable的工厂方法Collection品。我看到Set.of()方法有10个可变参数重载(Map.of()相同)。我真的不明白为什么会有这么多。最后函数ImmutableCollections.SetN(elements)无论如何都会被调用。在文档中我发现了这个:WhilethisintroducessomeclutterintheAPI,itavoidsarrayallo

Java 9、Set.of() 和 Map.of() 可变参数重载

这个问题在这里已经有了答案:WhatisthepointofoverloadedConvenienceFactoryMethodsforCollectionsinJava9(6个回答)关闭5年前。我正在研究Immutable的工厂方法Collection品。我看到Set.of()方法有10个可变参数重载(Map.of()相同)。我真的不明白为什么会有这么多。最后函数ImmutableCollections.SetN(elements)无论如何都会被调用。在文档中我发现了这个:WhilethisintroducessomeclutterintheAPI,itavoidsarrayallo

java - ImmutableCollections SetN 实现细节

我很难理解java-9ImmutableCollections.SetN的实现细节;特别是为什么需要将内部数组增加两次。假设你这样做:Set.of(1,2,3,4)//4elements,butinternalarrayis8更确切地说,我完全理解为什么在HashMap的情况下这样做(双重扩展)——您永远(几乎)不希望load_factor是一个。例如,!=1的值可以缩短搜索时间,因为条目可以更好地分散到桶中。但如果是不可变集合-我真的说不准。特别是因为选择内部数组索引的方式。让我提供一些细节。首先索引是如何搜索的:intidx=Math.floorMod(pe.hashCode()^