TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

JEP 431: Sequenced Collections

141 点作者 ludovicianul超过 2 年前

14 条评论

jerven超过 2 年前
I like JEPs such as this one. They show a strength of the current Java platform development community. We see multiple people spending significant thoughts and effort on what looks like a small feature. Which when implemented aims to feel so natural as if it was always there and no longer worth thinking about. Which in the makes so many people happier and more productive.
评论 #33203448 未加载
redditor98654超过 2 年前
I really admire Stuart Marks and his work, talks and writing. His work in Java APIs are much more approachable for laypersons like me who will be quick to pounce on various suggestions, problems and feature requests.<p>But I find his responses very well thought out and reasoned even though I personally may not like the outcome I can always see that the choice he made is perhaps the least worst option. This is no mean feat for a language still trying to evolve, even rapidly, after 25 years of cruft. This is unlike Brian Goetz’s work in Valhalla etc where you need more expertise to make meaningful suggestions.<p>Needless to say, very happy with this JEP. My only wish is make these updates come sooner but perhaps you need time to prepare and reason about new stuff instead of adding every little feature that may seem useful at first glance.
thesuperbigfrog超过 2 年前
JEP 431: Sequenced Collections feels like it is inspired by Clojure&#x27;s Seq interface &#x2F; abstraction:<p><a href="https:&#x2F;&#x2F;clojure.org&#x2F;reference&#x2F;sequences" rel="nofollow">https:&#x2F;&#x2F;clojure.org&#x2F;reference&#x2F;sequences</a><p>Sequenced Collections: &quot;Introduce new interfaces to represent collections with a defined encounter order. Each such collection has a well-defined first element, second element, and so forth, up to the last element. It also provides uniform APIs for accessing its first and last elements, and for processing its elements in reverse order.&quot;<p>Clojure Seq: &quot;Clojure uses the ISeq interface to allow many data structures to provide access to their elements as sequences. The seq function yields an implementation of ISeq appropriate to the collection. Seqs differ from iterators in that they are persistent and immutable, not stateful cursors into a collection. As such, they are useful for much more than foreach - functions can consume and produce seqs, they are thread safe, they can share structure etc.&quot;
评论 #33206427 未加载
kevincox超过 2 年前
The &quot;optional&quot; write methods look like awful design choices. If my method needs a writable sequenced collection I have no way to express that. I just need to hope that you give me one or you will get exceptions.<p>Why not break it into two interfaces? One for immutable and one with the mutable operations so that this is naturally expressible and checked by the type system?
评论 #33202930 未加载
评论 #33201850 未加载
评论 #33204853 未加载
评论 #33202812 未加载
评论 #33204064 未加载
评论 #33205949 未加载
评论 #33202197 未加载
benrow超过 2 年前
At first glance, one might think Java already has collection types with defined encounter order - List (defined iteration order based on insertion order) and SortedSet&#x2F;SortedMap (defined iteration order based on elements implementing the Comparable interface).<p>Reading more carefully, the proposal aims to lift the sequenced type higher up the hierarchy, so client code doesn&#x27;t need to code to specific implementations.<p>So far, operations available on collection types are influenced by the implementation. So what happens if I call the proposed addFirst() on an ArrayList, rather than a LinkedList? For the former case, I doubt all the elements will get shifted up by one, so would it create a new underlying array?<p>(Edit: of course it would, the underlying array is recreated for resize operations already. But it would involve a slow array copy operation for what may be a common case operation). I guess my point here is, if you lift operations into a common super-type some of those operations may have implementation challenges for some subtypes.<p>(Edit 2: turns out it&#x27;s already possible to insert an element at the head of an ArrayList using add(0, foo) so there&#x27;s no change to supported methods, just an abstraction of existing APIs, as far as I can tell).
评论 #33201471 未加载
ravirajx7超过 2 年前
Just signifies how good default method has revolutionized Java.
liquid153超过 2 年前
I think the benefits of this change for everyday development will far outweigh the complaints of language purists.
darksaints超过 2 年前
This is long overdue, and brilliant and well thought out work. But I can&#x27;t help but feel like this is shoehorning a useful concept into a primitive type system that only knows inheritance as an abstraction. This sort of thing would be dead simple with Impl in Rust or Trait in Scala.
nomercy400超过 2 年前
Doesn&#x27;t this break every existing implementation of List&#x2F;Deque&#x2F;SortedSet, as now every implementation is missing the new interface methods?<p>So suddenly updating from Java N to Java N+1 will no longer compile your code.
评论 #33200574 未加载
评论 #33200563 未加载
评论 #33200578 未加载
tadfisher超过 2 年前
In addition to this, Java should eliminate unordered collections entirely and alias them to their ordered equivalents; for example, HashMap -&gt; LinkedHashMap, HashSet -&gt; LinkedHashSet. See [0].<p>[0]: <a href="https:&#x2F;&#x2F;publicobject.com&#x2F;2016&#x2F;02&#x2F;08&#x2F;linkedhashmap-is-always-better-than-hashmap&#x2F;" rel="nofollow">https:&#x2F;&#x2F;publicobject.com&#x2F;2016&#x2F;02&#x2F;08&#x2F;linkedhashmap-is-always-...</a>
评论 #33207217 未加载
JensRantil超过 2 年前
I find it hilarious that, after all these years, Java is still not adhering to the Interface Segregation principle - working with _small_ interfaces should be a thing. Having SequencedCollection inherit Collection goes against that.
评论 #33201728 未加载
twic超过 2 年前
Oh good, a chance to rant about one of my pet peeves. Sorted sets and maps are really useful for all sorts of things, but in Java, they are very much second-class citizens. This JEP touches on a few ways in which they lack lustre. But take a look at even creating them in the first place!<p>Firstly, assume we have a string lying around:<p><pre><code> String string = &quot;mississippi&quot;; </code></pre> Let&#x27;s create some unsorted collections, from a literal (ish) expression, collecting a stream to a set, and collecting a stream to a map:<p><pre><code> Set&lt;Integer&gt; literal = Set.of(1, 2, 3); Set&lt;Integer&gt; collected = string.chars().boxed() .collect(Collectors.toSet()); Map&lt;Integer, Character&gt; map = IntStream.range(0, string.length()).boxed() .collect(Collectors.toMap(Function.identity(), string::charAt)); </code></pre> Pretty easy.<p>Now if we want them to be sorted:<p><pre><code> SortedSet&lt;Integer&gt; literal = new TreeSet&lt;&gt;(Set.of(1, 2, 3)); SortedSet&lt;Integer&gt; collected = string.chars().boxed() .collect(Collectors.toCollection(TreeSet::new)); SortedMap&lt;Integer, Character&gt; map = IntStream.range(0, string.length()).boxed() .collect(Collectors.toMap(Function.identity(), string::charAt, (a, b) -&gt; { throw new UnsupportedOperationException(); }, TreeMap::new)); </code></pre> Firstly, there is no SortedSet.of, so we have to explicitly wrap a literal set in a concrete implementation of SortedSet; as well as being a little less pretty, this means there&#x27;s no chance for the JDK to use efficient specialised implementations of small constant sets, as it can for unsorted sets, and lists. Secondly, there is no toSortedSet collector, so we have to use the generic collection collector, again with a concrete implementation of SortedSet. Thirdly, there is no toSortedMap collector, so we have to use the generic toMap collector which again takes a concrete map implementation, but also requires us to handle key collisions ourself; this last point is particularly bad, because it is impossible to write a handler which is as good as the one used by the default toMap, because the handler doesn&#x27;t get to see the colliding key!<p>What if we want them to be sorted in reverse?<p><pre><code> SortedSet&lt;Integer&gt; literal = new TreeSet&lt;&gt;(Comparator.reverseOrder()); literal.addAll(Set.of(1, 2, 3)); SortedSet&lt;Integer&gt; collected = string.chars().boxed() .collect(Collectors.toCollection(() -&gt; new TreeSet&lt;&gt;(Comparator.reverseOrder()))); SortedMap&lt;Integer, Character&gt; map = IntStream.range(0, string.length()).boxed() .collect(Collectors.toMap(Function.identity(), string::charAt, (a, b) -&gt; { throw new UnsupportedOperationException(); }, () -&gt; new TreeMap&lt;&gt;(Comparator.reverseOrder()))); </code></pre> Where we use a collector, we have to expand the map implementation constructor method reference to a lambda, so we can pass in a comparator; mildly annoying but not too bad. But for the literal, there is no constructor which takes both a comparator and elements, so we have to create the set with the comparator, and then add the elements! It becomes impossible to do this in a single expression, or to use an immutable collection, and it&#x27;s several times the volume of code as the unsorted case.<p>If you want to write the reverse sorted literal as an expression, you can do this:<p><pre><code> SortedSet&lt;Integer&gt; literal = Stream.of(1, 2, 3) .collect(Collectors.toCollection(() -&gt; new TreeSet&lt;&gt;(Comparator.reverseOrder()))); </code></pre> Which is still embarrassing.
cutler超过 2 年前
How does C# handle this?
评论 #33201311 未加载
TedDoesntTalk超过 2 年前
Long overdue!