Queries provide a powerful abstraction for dealing with sets of objects, allowing the query engine to take care of the implementation details. This allows for shorter, clearer code, and permits the query engine to dynamically optimize query evaluation strategies as the runtime context changes.
Queries can also be cached and that cache incrementally maintained - this greatly increases their efficiency, and can offer improved performance for many common collection operations.
A brief example!
Say we're building a crossword puzzle. We've got a list of candidate words for our puzzle, and a list of the lengths of the gaps we need to fill:ArrayList<String> words = dict.getWords(Puzzle.MEDIUM);
ArrayList<Integer> gaplengths = puzzle.getGapLengths();
Now we've got a truly marvelous algorithm for building a crossword puzzle (that this webpage is too narrow to contain), which relies on having a list of pairs of [length, word].
Using a JQL query, we can build this list with ease:
List<Object[]> matches = selectAll(String w : words,
Integer i : gaplengths |
w.length() == i);
List<Object[]> matches = new ArrayList<Object[]>();
for(String w : words){
for(Integer i : gaplengths){
if(w.length() == i)
matches.add(new Object[i,w]);
}
}
下載地址:http://homepages.mcs.vuw.ac.nz/~djp/jql/download.html