Java Arrays.stream Examples

Java Arrays.stream - 30 examples found. These are the top rated real world Java examples of java.util.Arrays.stream extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
  @Override
  public Optional<Suggestion> requestPlayerSuggestion(Player player, Room room) {
    char userWantsToMakeSuggestion = '\0';

    while (userWantsToMakeSuggestion != 'Y' && userWantsToMakeSuggestion != 'N') {
      this.out.println("Do you want to make an suggestion (Y/N)?");
      this.out.println("Your cards are: " + player.cards);
      userWantsToMakeSuggestion = this.scanner.next().charAt(0);
    }

    if (userWantsToMakeSuggestion == 'Y') {
      this.out.printf("You suggest it was done in the %s, by: \n", room);

      Stream<String> suspects =
          Arrays.stream(CluedoCharacter.values()).map(CluedoCharacter::toString);
      CluedoCharacter suspect = CluedoCharacter.values()[this.selectOptionFromList(suspects)];

      this.out.println("with the ");

      Stream<String> weapons = Arrays.stream(Weapon.values()).map(Weapon::toString);
      Weapon weapon = Weapon.values()[this.selectOptionFromList(weapons)];

      return Optional.of(new Suggestion(suspect, weapon, room));

    } else {
      return Optional.empty();
    }
  }
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    Integer[] startTimeInfo =
        Arrays.stream(scanner.nextLine().split(":+"))
            .map(Integer::parseInt)
            .toArray(Integer[]::new);
    Integer[] endTimeInfo =
        Arrays.stream(scanner.nextLine().split(":+"))
            .map(Integer::parseInt)
            .toArray(Integer[]::new);

    Integer minutesDifferenceFromSeconds = 0;
    Integer seconds = startTimeInfo[2] - endTimeInfo[2];
    if (seconds < 0) {
      seconds = 60 + seconds;
      minutesDifferenceFromSeconds--;
    }

    Integer hoursDifferenceFromMinutes = 0;
    Integer minutes = (startTimeInfo[1] - endTimeInfo[1]) + minutesDifferenceFromSeconds;
    if (minutes < 0) {
      minutes = 60 + minutes;
      hoursDifferenceFromMinutes--;
    }

    Integer hours = (startTimeInfo[0] - endTimeInfo[0]) + hoursDifferenceFromMinutes;

    System.out.printf("%d:%02d:%02d%n", hours, minutes, seconds);
  }
Example #3
0
  public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    String[] numbers = sc.nextLine().split(" ");

    String sort = sc.nextLine();

    if (sort.equals("Ascending")) {
      List<Integer> output =
          Arrays.stream(numbers).map(Integer::parseInt).sorted().collect(Collectors.toList());

      for (Object items : output) {
        System.out.print(items + " ");
      }
    } else if (sort.equals("Descending")) {
      List<Integer> output =
          Arrays.stream(numbers)
              .map(Integer::parseInt)
              .sorted(Comparator.reverseOrder())
              .collect(Collectors.toList());

      for (Object items : output) {
        System.out.print(items + " ");
      }
    }
  }
  public static void main(String[] args) {
    AccessIdentifiers2 ac = new AccessIdentifiers2();
    System.out.println(ac.getClass().getName());
    System.out.println(
        Arrays.stream(ac.getClass().getDeclaredFields())
            .map(Field::getName)
            .collect(Collectors.joining(" || ")));
    ;
    System.out.println(
        Arrays.stream(ac.getClass().getDeclaredMethods())
            .map(Method::getName)
            .collect(Collectors.joining(" || ")));
    System.out.println(
        Arrays.stream(ac.getClass().getDeclaredMethods())
            .map(Method::getReturnType)
            .map(Class::getName)
            .collect(Collectors.joining(" || ")));
    System.out.println(
        Arrays.stream(ac.getClass().getInterfaces())
            .map(Class::getName)
            .collect(Collectors.joining(" || ")));

    System.out.println(ac.getClass().getDeclaredFields().length);
    System.out.println(ac.getClass().getDeclaredMethods().length);
  }
  public static void main(String[] args) {
    String[] strings = {"Red", "orange", "Yellow", "green", "Blue", "indigo", "Violet"};

    // display original strings
    System.out.printf("Original strings: %s%n", Arrays.asList(strings));

    // strings in uppercase
    System.out.printf(
        "strings in uppercase: %s%n",
        Arrays.stream(strings).map(String::toUpperCase).collect(Collectors.toList()));

    // strings less than "m" (case insensitive) sorted ascending
    System.out.printf(
        "strings greater than m sorted ascending: %s%n",
        Arrays.stream(strings)
            .filter(s -> s.compareToIgnoreCase("m") < 0)
            .sorted(String.CASE_INSENSITIVE_ORDER)
            .collect(Collectors.toList()));

    // strings less than "m" (case insensitive) sorted descending
    System.out.printf(
        "strings greater than m sorted descending: %s%n",
        Arrays.stream(strings)
            .filter(s -> s.compareToIgnoreCase("m") < 0)
            .sorted(String.CASE_INSENSITIVE_ORDER.reversed())
            .collect(Collectors.toList()));
  }
 @Override
 public DefaultDataBuffer write(ByteBuffer... byteBuffers) {
   Assert.notEmpty(byteBuffers, "'byteBuffers' must not be empty");
   int extraCapacity = Arrays.stream(byteBuffers).mapToInt(ByteBuffer::remaining).sum();
   ensureExtraCapacity(extraCapacity);
   Arrays.stream(byteBuffers)
       .forEach(byteBuffer -> writeInternal(buffer -> buffer.put(byteBuffer)));
   return this;
 }