Is it possible for a method in Java 8 to determine simply whether an Iterable<Integer> Object provides a PrimitiveIterator.OfInt iterator? -


this first activity here, hope behave correctly! problem: trying write collection of functions (static methods in class) accept 1 or several iterable<integer> , obtains primitiveiterator.ofint, providing function constructs yet iterator (i.e. functions meant compose iterators in various ways).

here simple example of have in mind:

public static filteriteratorint filtor(iterable<integer> iter, intpredicate filter) {     return new filteriteratorint((primitiveiterator.ofint)iter.iterator(),filter); }    

to me, not quite clear whether can work @ all, because iterator returned iterable<integer> object might not of type primitiveiterator.ofint.

in order overcome potential difficulty, have been looking solution, might either interface e.g. primitiveiterable.ofint or other way find out whether iterator in fact primitive. have been searching quite while, while browsing answers of question, time had register here ask question directly.

this construction meant avoid boxing/unboxing orgies, since want new iterators reasonably fast.

so here 3 questions:

  1. is there way find out whether iterator obtained iterable in fact primitive iterator (such function can distinguish , act accordingly) or there way obtain one?
  2. is possibly useless try improve performance way? i.e. (jit or java)compilers optimize anyway, or boxing/unboxing unavoidable anyway under hood? here hope learn something.
  3. can show me different , better solution serve same purpose (i.e. composition of primitive iterators, or of iterators, while @ it)?

update: due holgers answer, boils down following question nr. 4: if next() called on primitiveinteger.ofint, invoke nextint() method, or in other words: automatically end returning pure int? or still cause boxing , unboxing sequence?

from answer below assume latter, , mean better deal nextint() explicitly.

assuming correct (please tell me if wrong), have used instanceof method below , explicitly wrap if required.

well, use iterator instanceof primitiveiterator.ofint test, when intended operation foreachremaining, need both, intconsumer passed primitiveiterator.ofint efficient processing , consumer<integer> handle iterators not instances of primitiveiterator.ofint , if implement both within 1 class, don’t need perform test @ all, iterator you:

public static void main(string[] args) {     system.out.println("with collection (of integer boxes)");     filterandprint(arrays.aslist(1, 2, 3), -> i>2);     system.out.println("with intstream (using primitive int values)");     filterandprint(() -> intstream.range(1, 4).iterator(), -> i>2); } interface loggingunboxingintconsumer extends intconsumer, consumer<integer> {     @override default void accept(integer t) {         system.out.println("  unboxing " + t);             accept(t.intvalue());     } } public static void filterandprint(iterable<integer> i, intpredicate p) {     i.iterator().foreachremaining((loggingunboxingintconsumer) (int value) -> {         if(p.test(value)) system.out.println("  value "+value+" matches");     }); } 
with collection (of integer boxes)   unboxing 1   unboxing 2   unboxing 3   value 3 matches intstream (using primitive int values)   value 3 matches 

this demonstrates boxing operations avoided when possible. part of the contract of primitiveiterator.ofint.foreachremaining(consumer<? super integer>):

implementation requirements:

if action instance of intconsumer cast intconsumer , passed foreachremaining(java.util.function.intconsumer); otherwise action adapted instance of intconsumer, boxing argument of intconsumer, , passed foreachremaining(java.util.function.intconsumer).

this not apply single element processing via hasnext()/next(), since code supposed perform composition of primitiveiterable.ofint only, initial step place adaptation has done anyway

public static primitiveiterator.ofint adapt(iterator<integer> it) {     return instanceof primitiveiterator.ofint? (primitiveiterator.ofint)it:       new primitiveiterator.ofint() {         public int nextint() { return it.next(); }         public boolean hasnext() { return it.hasnext(); }         public integer next() { return it.next(); }       }; } 

after having created method once, may use @ places accept iterable, e.g.

public static filteriteratorint filter(iterable<integer> iter, intpredicate filter) {     return new filteriteratorint(adapt(iter.iterator()), filter); } 

but note “iterator composition” looks reinvention of stream api (or intstream api specifically)…


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -