Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #37745 > unrolled thread

Re: Can I put a filter into a lambda?

Started byEric Sosman <esosman@comcast-dot-net.invalid> (Eric Sosman)
First post2017-12-01 16:28 +1200
Last post2017-12-04 08:20 +1200
Articles 3 — 2 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Re: Can I put a filter into a lambda? Eric Sosman <esosman@comcast-dot-net.invalid> (Eric Sosman) - 2017-12-01 16:28 +1200
    Re: Can I put a filter into a lambda? Eric Douglas <e.d.programmer@gmail.com> (Eric Douglas) - 2017-12-04 05:03 +1200
      Re: Can I put a filter into a lambda? Eric Sosman <esosman@comcast-dot-net.invalid> (Eric Sosman) - 2017-12-04 08:20 +1200

#37745 — Re: Can I put a filter into a lambda?

FromEric Sosman <esosman@comcast-dot-net.invalid> (Eric Sosman)
Date2017-12-01 16:28 +1200
SubjectRe: Can I put a filter into a lambda?
Message-ID<282294394@f38.n261.z1.binkp.net>
On 12/1/2017 3:52 PM, Eric Douglas wrote:
> I haven't gotten around to mastering the lambda syntax.
> I have a JTable with a custom row sorter building an index of all model rows:
>
> rowToModelIndex.clear();
> if (modelRowCount > 0) {
>       if (getRowFilter() != null) {
>            for (int r = 0; r < modelRowCount; r++) {
>                 if (include(r)) {
>                      rowToModelIndex.add(r);
>                 }
>            }
>       } else {
>            rowToModelIndex.addAll(Stream.iterate(firstRow, n -> n +
1).limit(modelRowCount).collect(Collectors.toList()));
>       }
> }
>
> If it's a straight up series it's a lambda.  Can the filter loop be also?  I
would guess that would be more efficient.

     It's not clear what your filter loop is actually doing:
the RowFilter seems never to be used, and I can't find any one-argument
include() method in any Java SE (8 or 9) class. (You also haven't shown what
rowToModelIndex is, but I'll guess it's a Collection<Integer>, possibly
List<Integer>). So I'll restate the problem without the table trappings:

     1) If getRowFilter() returns null, you want a List of
        all the values 0 through modelRowCount-1, in order.

     2) If getRowFilter() returns non-null, you want a List
        of the same values, excluding those for which the
        boolean-valued include(int) method returns false.

     If that's not the goal, read no further.  But if it is
(or is close enough for jazz), both cases can be written more simply:

        // Case 1:
        rowModelIndex = IntStream.range(0, modelRowCount)
            .mapToObj(Integer::valueOf)
            .collect(Collectors.toList());

        // Case 2:
        rowModelIndex = IntStream.range(0, modelRowCount)
            .filter((value) -> include(value))
            .mapToObj(Integer::valueOf)
            .collect(Collectors.toList());

     Final point: If you can't stop worrying about efficiency,
at the very least stop *guessing* about it!

--
esosman@comcast-dot-net.invalid
Eleven hundred forty-six days to go.

[toc] | [next] | [standalone]


#37748

FromEric Douglas <e.d.programmer@gmail.com> (Eric Douglas)
Date2017-12-04 05:03 +1200
Message-ID<4175562947@f38.n261.z1.binkp.net>
In reply to#37745
On Friday, December 1, 2017 at 4:28:26 PM UTC-5, Eric Sosman wrote:
>      It's not clear what your filter loop is actually doing:
> the RowFilter seems never to be used, and I can't find any
> one-argument include() method in any Java SE (8 or 9) class.
> (You also haven't shown what rowToModelIndex is, but I'll
> guess it's a Collection<Integer>, possibly List<Integer>).
> So I'll restate the problem without the table trappings:
>
>      1) If getRowFilter() returns null, you want a List of
>         all the values 0 through modelRowCount-1, in order.
>
>      2) If getRowFilter() returns non-null, you want a List
>         of the same values, excluding those for which the
>         boolean-valued include(int) method returns false.
>
>      If that's not the goal, read no further.  But if it is
> (or is close enough for jazz), both cases can be written
> more simply:
>
>       // Case 1:
>       rowModelIndex = IntStream.range(0, modelRowCount)
>           .mapToObj(Integer::valueOf)
>           .collect(Collectors.toList());
>
>       // Case 2:
>       rowModelIndex = IntStream.range(0, modelRowCount)
>           .filter((value) -> include(value))
>           .mapToObj(Integer::valueOf)
>           .collect(Collectors.toList());
>
>      Final point: If you can't stop worrying about efficiency,
> at the very least stop *guessing* about it!
>
rowToModelIndex is an ArrayList<Integer>.

Yes the include method says if the row filter is null then include every row
number, if not null then include rows based on the boolean value from the row
filter include method.

Milliseconds do count in a row filtering method which executes thousands of
times.

[toc] | [prev] | [next] | [standalone]


#37749

FromEric Sosman <esosman@comcast-dot-net.invalid> (Eric Sosman)
Date2017-12-04 08:20 +1200
Message-ID<163292229@f38.n261.z1.binkp.net>
In reply to#37748
On 12/4/2017 8:03 AM, Eric Douglas wrote:
> On Friday, December 1, 2017 at 4:28:26 PM UTC-5, Eric Sosman wrote:
>>       It's not clear what your filter loop is actually doing:
>> the RowFilter seems never to be used, and I can't find any
>> one-argument include() method in any Java SE (8 or 9) class.
>> (You also haven't shown what rowToModelIndex is, but I'll
>> guess it's a Collection<Integer>, possibly List<Integer>).
>> So I'll restate the problem without the table trappings:
>>
>>       1) If getRowFilter() returns null, you want a List of
>>          all the values 0 through modelRowCount-1, in order.
>>
>>       2) If getRowFilter() returns non-null, you want a List
>>          of the same values, excluding those for which the
>>          boolean-valued include(int) method returns false.
>>
>>       If that's not the goal, read no further.  But if it is
>> (or is close enough for jazz), both cases can be written
>> more simply:
>>
>>      // Case 1:
>>      rowModelIndex = IntStream.range(0, modelRowCount)
>>          .mapToObj(Integer::valueOf)
>>          .collect(Collectors.toList());
>>
>>      // Case 2:
>>      rowModelIndex = IntStream.range(0, modelRowCount)
>>          .filter((value) -> include(value))
>>          .mapToObj(Integer::valueOf)
>>          .collect(Collectors.toList());
>>
>>       Final point: If you can't stop worrying about efficiency,
>> at the very least stop *guessing* about it!
>>
> rowToModelIndex is an ArrayList<Integer>.

     If it *must* be an ArrayList (and not some other kind of List),
you might want to change the last bit to

        .collect(Collectors.toCollection(ArrayList::new));

Although the toList() method appears to use ArrayList anyhow, it doesn't
guarantee to do so -- meaning that it might use some other List in a future
Java, or in circumstances I haven't studies (e.g., it might behave differently
with a parallel stream).

> Milliseconds do count in a row filtering method which executes thousands of
times.

     If performance is important, measure.  Don't guess.

--
esosman@comcast-dot-net.invalid
Eleven hundred forty-three days to go.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web