On Sun, Jul 27, 2025 at 12:46 PM Yokesh Chowdary <bollineniyok...@gmail.com> wrote:
> Hi Gary, > > Thanks for the quick reply! > > You're absolutely right, *ArrayUtils.subarray()* and > *StringUtils.substring() > *covers basic slicing. However, my proposal aims to enhance these methods > by introducing: > > 1) Support for step > ArrayUtils.slice(new int[]{1, 2, 3, 4, 5, 6}, 0, -1, 2); // returns [1, 3, > 5] > > 2) Support for negative indices > StringUtils.slice("abcdef", -4, -1); // returns "cde" > > These enhancements bring Python-like expressiveness to Java, making slicing > more intuitive and concise for developers, especially when dealing with > dynamic ranges or reverse slicing. > Would love to hear thoughts on whether extending existing utils or > introducing a SliceUtils would be preferable. > Hello Yokesh, I think you're going to need to take a Java POV here, not Python, because this: StringUtils.slice("abcdef", -4, -1); // returns "cde" feels nonsensical for this Commons component. In StringUtils, CharSequenceUtils, and ArrayUtils, negative indices are usually (always?) errors. I don't think we should adopt the naming convention ("slice/SliceUtils") of another language. Java and Commons Lang on top of it both have their own vocabulary of APIs. In this case, Java uses "substring", and we're reusing that term in StringUtils. In ArrayUtils, we derived that term into "subarray". Adopting Python naming would set the expectation that this would maintain the Python-ness behavior over time, and encourage further Python creep, which is not what Commons components are about. They are about providing Java-centric APIs, not "it works like X in language Y, so let's implement that". There are odd bits in Lang to be sure, but that's mostly because the code base has been around for a long time. WRT: ArrayUtils.slice(new int[]{1, 2, 3, 4, 5, 6}, 0, -1, 2); // returns [1, 3, 5] I have no idea how -1 makes sense or what these parameters mean. You'll want to provide actual descriptions of each parameter so we know what you're talking about. Assuming "it works like Python" is not a good idea. Think about the whole class and how a new API fits in. Saying this lives in a new class SliceUtils is not good IMO (see above on terminology). HTH, Gary > Regards, > Yokesh > > On Sun, Jul 27, 2025 at 10:02 PM Gary Gregory <garydgreg...@gmail.com> > wrote: > > > Hello Yokesh, > > > > This functionality already exists in ArrayUtils (subarray()) and > > StringUtils (substring()). > > > > Gary > > > > On Sun, Jul 27, 2025, 12:06 Yokesh Chowdary <bollineniyok...@gmail.com> > > wrote: > > > > > Hi All, > > > > > > I'd like to propose adding a new utility method slice() to Apache > Commons > > > Lang that brings Python-style slicing capabilities to Java for > > Collections, > > > arrays (primitive and wrapper types), and String. > > > The proposed method would follow the form: *slice(input, start, end, > > step)* > > > > > > This would be a highly reusable utility that simplifies what is > currently > > > verbose or error-prone logic in standard Java. It could live in a new > > class > > > like *SliceUtils *or be integrated into existing utility classes such > > > as *ArrayUtils > > > *and *StringUtils*. > > > > > > *Highlights:* > > > 1) Supports positive and negative indices for start,end and step > > > 2) Works on Collections, Arrays and Strings > > > > > > *Examples:* > > > List<String> names = Arrays.asList("Hello","Hi","Bye"); > > > SliceUtils.slice(names, 0,2); // returns ["Hello", "Hi"] > > > > > > String s = "abcdef"; > > > SliceUtils.slice(s, 1, 4); // returns "bcd" > > > > > > int[] numbers = {1, 2, 3, 4, 5, 6}; > > > SliceUtils.slice(numbers, 0, -1, 2); // returns [1, 3, 5] > > > > > > If the community agrees this is in scope for Commons Lang, I’d be happy > > to > > > implement the feature with full test coverage and documentation. > > > > > > Looking forward to your thoughts. > > > > > > Regards, > > > Yokesh > > > [bollineniyok...@gmail.com] > > > > > >