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