Skip to content

Instantly share code, notes, and snippets.

@lorenzoongithub
Created June 3, 2016 21:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lorenzoongithub/5aa2f94967d261a447457500a7536f90 to your computer and use it in GitHub Desktop.
Save lorenzoongithub/5aa2f94967d261a447457500a7536f90 to your computer and use it in GitHub Desktop.
//
// Using Lambda in java 8
// Inspired by https://blogs.oracle.com/nashorn/entry/nashorn_and_lambda_what_the
//
var copyright =
'/* \n'+
' * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. \n'+
' * \n'+
' * Redistribution and use in source and binary forms, with or without \n'+
' * modification, are permitted provided that the following conditions \n'+
' * are met: \n'+
' * \n'+
' * - Redistributions of source code must retain the above copyright \n'+
' * notice, this list of conditions and the following disclaimer. \n'+
' * \n'+
' * - Redistributions in binary form must reproduce the above copyright \n'+
' * notice, this list of conditions and the following disclaimer in the \n'+
' * documentation and/or other materials provided with the distribution. \n'+
' * \n'+
' * - Neither the name of Oracle nor the names of its \n'+
' * contributors may be used to endorse or promote products derived \n'+
' * from this software without specific prior written permission. \n'+
' * \n'+
' * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS \n'+
' * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, \n'+
' * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR \n'+
' * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR \n'+
' * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, \n'+
' * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, \n'+
' * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR \n'+
' * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF \n'+
' * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING \n'+
' * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS \n'+
' * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \n'+
' */';
var Collectors = java.util.stream.Collectors;
// Break the copyright into tokens.
var tokens = copyright.split(/\s+/);
// Convert to ArrayList.
var list = new java.util.ArrayList();
tokens.map(function(e) list.add(e));
// The JavaScript collection for the result.
var result = [];
// Parallelize some of the activity.
list.parallelStream().
// Select only words.
filter(function(t) t.match(/^[A-Za-z]+$/)).
// Make case comparable.
map(function(t) t.toLowerCase()).
// Fold duplicates.
collect(Collectors.groupingBy(function(t) t)).
// Move results to JavaScript collection.
forEach(function(t) result.push(t));
// Sort the result.
result.sort();
// display
result.join('<br>');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment