[External] : Re: Could we add headList and tailList methods to j.u.List?
Stuart Marks
stuart.marks at oracle.com
Wed Dec 3 05:43:11 UTC 2025
(Picking this up after the US holiday weekend)
I wanted to ask you more specifically about the use cases. It's interesting to hear
about the problem spaces you're working on (Pathfinding and NLP) but I'm more
interested in specific cases about how you would use subList APIs. I can imagine how
one would use subLists for such applications, but what I imagine might very well be
different from what you're actually doing! So I'd like to hear about what you're
actually doing. What would an enhanced subList API look like? What indexes,
identifying which portions of the underlying List?
If you created utility methods for your own use, it might be helpful to share what
they do. Not necessarily as a proposal for what to add to the JDK, but rather to let
me (and others) know concretely how you're using subLists.
Thanks.
s'marks
On 11/19/25 3:54 AM, David Alayachew wrote:
> Thanks for the response @Stuart Marks <mailto:stuart.marks at oracle.com>!
>
> > A third concern is that people come along and ask whether we can
> > have something in Java that's rather like Python's slices.
>
> Hah, this email started out as a request for Python slices lol. But I came to the
> same conclusion as you.
>
> I am quite curious about what Project Amber has to say about this. Range patterns,
> for example.
>
> > You had asked about List, not String, but I think similar issues apply.
> > I'd guess that String and substring operations are a lot more frequent
> > than subList operations. But maybe you have some subList use cases in
> > mind. Could you give more details about what you're thinking of?
>
> I definitely agree that there is heavy overlap in the problem spaces of subList and
> substring.
>
> As for use cases, mostly Path-Finding algorithms and NLP <https://urldefense.com/v3/
> __https://en.wikipedia.org/wiki/Natural_language_processing__;!!ACWV5N9M2RV99hQ!
> KWNVun1_iat8b30He5DRLBmNvnYa_gGWluOmusyxN8_0clx5nF5tUN8UmCyUTGhACrhYXSP-
> ojovuKpFopr9DPvjAw$> work. The PFA is mostly self explanatory, but for NLP, I splice
> up Strings in a bunch of different ways. You have to cycle through different
> permutations of word groupings to anchor a phrase to a concept (so I get to use both
> substring and subList lol). And each concept has an associated type for it -- a
> permitted subtype of a sealed type (which may itself be another permitted subtype of
> another).
>
> Since most of this is me trimming from the front or end, it became way more
> manageable to make headList and subList for myself. So, I think that they could be
> useful for others too.
>
> I will say -- zooming out (while thinking of the substring/subList duality), String
> and List are just Sequences of X. Maybe there is value in making something more
> broad here? Maybe some Sequence-like interface where we expose some helpful range
> methods? It would all devolve to the statement forms you mentioned earlier. And in
> an ideal world, Project Amber could latch onto that if/when range patterns go live.
> I remember watching this video by Brian Goetz about Growing the Language <https://
> urldefense.com/v3/__https://www.youtube.com/watch?v=Gz7Or9C0TpM__;!!ACWV5N9M2RV99hQ!
> KWNVun1_iat8b30He5DRLBmNvnYa_gGWluOmusyxN8_0clx5nF5tUN8UmCyUTGhACrhYXSP-
> ojovuKpFopqA6Cw60w$>. It kind of feels like an interface like this would be very
> amenable, though I might also be thinking too far ahead lol.
>
> On Wed, Nov 19, 2025 at 12:48 AM Stuart Marks <stuart.marks at oracle.com
> <mailto:stuart.marks at oracle.com>> wrote:
>
>
>
> On 11/16/25 10:51 AM, David Alayachew wrote:
> > Could we add headList(int) and tailList(int) to j.u.List? I searched JBS and
> found
> > nothing.
> >
> > It's commonly what people want when doing subList(int, int), so this should be
> > pretty well received.
>
> Maybe. :-)
>
> There's a nexus of diffuse concerns here.
>
> One concern is subranges of things in general, such as Strings, CharSequences,
> Lists, and arrays. Subranges are all expressed as methods with (start, end)
> parameters. These are powerful enough to do anything you need to do, but they're
> sometimes inconvenient.
>
> One reason these can be inconvenient is related to the second concern, which is
> that
> sometimes the APIs require the creation of a local variable, which in turn
> forces an
> expression to turn into a statement. For example, consider a task of getting a
> String from somewhere, taking the first three characters, and passing that
> elsewhere
> to perform further work. One can write that as
>
> furtherWork(getString().substring(0, 3));
>
> But if you want to take the *last* three characters and pass them along, you
> have to
> do this:
>
> var tmp = getString();
> furtherWork(tmp.substring(tmp.length() - 3));
>
> This isn't terrible, but sometimes it disrupts an expression to declare a local
> variable.
>
> At least there is a one-arg substring method that takes characters to the end of
> the
> string. If this method weren't there, you'd have to call length() twice (or store
> the length in another local variable). Again, not terrible, but yet another thing
> that adds friction.
>
> A third concern is that people come along and ask whether we can have something in
> Java that's rather like Python's slices. For a variety of reasons I don't think we
> should accept negative indexes or step values other than 1, but there are some
> things that probably occur frequently. For example, to get the last three
> characters
> of a string, one can write s[-3:]. That's pretty nice.
>
> You had asked about List, not String, but I think similar issues apply. I'd guess
> that String and substring operations are a lot more frequent than subList
> operations. But maybe you have some subList use cases in mind. Could you give more
> details about what you're thinking of?
>
> > In that case, would be nice if we could add an entry to JBS with a Won't Fix, to
> > make it easier for those looking to see why not. Maybe even link this thread for
> > further reading.
>
> Heh, yeah I've done that a couple times -- filed a bug just to close it out with an
> explanation why. I was wondering whether anybody would find that useful. I think
> this topic is worth some discussion, though, so I won't do that quite yet. :-)
>
> s'marks
>
More information about the core-libs-dev
mailing list