Querying ArrayList

edited April 2017 in How To...

Hi just wondering if there's a way to query an array list short of iterating through all and flagging something?

I wanted to check if ALL objects in an array list had a flag set to true.. kind of like a reduce map or in C# .Any query?

Answers

  • edited April 2017

    Although PDE is using Java 8, the way it is currently set up, latest functional syntaxes aren't allowed! =((

    We'd need to write them on some other IDE for it. Otherwise we'd be allowed to use Collection::stream() & invoke Stream::allMatch() passing a Predicate as its argument using Java 8's lambda thin arrow operator ->: :-<

    1. http://docs.Oracle.com/javase/8/docs/api/java/util/Collection.html#stream--
    2. http://docs.Oracle.com/javase/8/docs/api/java/util/stream/Stream.html#allMatch-java.util.function.Predicate-
    3. http://docs.Oracle.com/javase/8/docs/api/java/util/function/Predicate.html

    Anyways, here's some archaic Java mock approach for it: :(|)

    // forum.Processing.org/two/discussion/21761/querying-arraylist#Item_1
    // GoToLoop 2017-Apr-02
    
    import java.util.Collection;
    import java.util.List;
    
    int qty = (int) random(1, 6);
    final List<Flag> flags = new ArrayList(qty);
    
    void setup() {
      for (int i = 0; i++ < qty; )  flags.add(new Flag());
      println(flags);
      println(areAllTrue(flags));
      exit();
    }
    
    static final boolean areAllTrue(final Collection<Flag> elems) {
      for (final Flag f : elems)  if (!f.flag)  return false;
      return true;
    }
    
    class Flag {
      boolean flag = random(1) > .2;
    
      @ Override String toString() {
        return str(flag);
      }
    }
    
  • edited December 2018

    But in the end, I've managed to use Stream::allMatch(), by instantiating my own Predicate & implementing its abstract method test(). \m/

    However, it's not as good as an actual lambda -> expression, which isn't bound to a specific datatype, escaping somehow from Java's brutal strongly typed system. O:-)

    // forum.Processing.org/two/discussion/21761/querying-arraylist#Item_2
    // GoToLoop 2017-Apr-02
    
    import java.util.List;
    import java.util.function.Predicate;
    
    static final Predicate<Flag> FLAG = new Predicate<Flag>() {
      @ Override final boolean test(final Flag f) {
        return f.flag;
      }
    };
    
    int qty = (int) random(1, 6);
    final List<Flag> flags = new ArrayList(qty);
    
    void setup() {
      for (int i = 0; i++ < qty; )  flags.add(new Flag());
      println(flags);
      println(flags.stream().allMatch(FLAG));
      exit();
    }
    
    class Flag {
      boolean flag = random(1) > .2;
    
      @ Override String toString() {
        return str(flag);
      }
    }
    

    Another approach, besides using another IDE for a full Java 8 syntax, is implementing a function under Java's builtin JS language called Nashorn, and invoking it from Java's side, passing your Collection container as its argument. :ar!

    After all in JS, all of its functions act like lambdas. And they can be passed freely as argument for another function, becoming callbacks for the receiving function! :-bd

Sign In or Register to comment.