sort words by the number of characters which occurs in the input cs character array. - Java Lambda Stream

Java examples for Lambda Stream:Lambda

Description

sort words by the number of characters which occurs in the input cs character array.

Demo Code


import java.util.Collections;
import java.util.List;

public class Main {
  /**/*ww  w .  jav  a  2s.  co  m*/
   * sort words by the number of characters which occurs in the input cs character
   * array.
   * 
   * @return
   */
  public static List<String> sortWordsByNumberOfChar(List<String> nWords, char... cs) {

    // sort non-null nWords IN PLACE using Collections.sort
    // Put your code here! ...
    nWords.sort((o1, o2) -> {
      int o1c = 0;
      int o2c = 0;
      for (char c : cs) {
        o1c += o1.chars().filter(ch -> ch == c).count();
        o2c += o2.chars().filter(ch -> ch == c).count();
      }
      return o1c - o2c;
    });
    return nWords;
  }

}

Related Tutorials