Esempio n. 1
0
  public static void main(String[] args) throws Exception {
    List<Integer> currentLocation = Arrays.asList(0, 0);

    Stream houses =
        Files.lines(new File("input.txt").toPath())
            .flatMap(line -> Stream.of(line.split("")))
            .map(
                s -> {
                  matchPath(s, currentLocation);
                  return new ArrayList<>(currentLocation);
                })
            .distinct();
    System.out.println(houses.count());

    List<Integer> santa = Arrays.asList(0, 0);
    List<Integer> robot = Arrays.asList(0, 0);
    List<String> paths =
        Files.lines(new File("input.txt").toPath())
            .flatMap(line -> Stream.of(line.split("")))
            .collect(Collectors.toList());
    Stream houses2 =
        IntStream.range(0, paths.size())
            .mapToObj(
                i -> {
                  if (i % 2 == 0) {
                    matchPath(paths.get(i), santa);
                  } else {
                    matchPath(paths.get(i), robot);
                  }
                  return Arrays.asList(santa, robot);
                })
            .flatMap(lists -> Stream.of(lists.get(0), lists.get(1)))
            .distinct();
    System.out.println(houses2.count());
  }
Esempio n. 2
0
  public void mainCollectorExample() {
    Stream songsStream = getSongStream().map(s -> s.toUpperCase());
    System.out.println(songsStream.count());

    Set<String> setSongs = getSongStream().filter(s -> s.startsWith("it")).collect(toSet());
    System.out.println("Set count:" + setSongs.size());
    songsStream.close();

    int sum = getSongStream().mapToInt(song -> song.toString().length()).sum();
    System.out.println("sum is :" + sum);

    // lets understand the difference between lazy and eager evaluation
    getSongStream()
        .filter(
            song -> {
              System.out.println("coming song in lazy:" + song);
              return song.startsWith("al");
            });

    getSongStream()
        .filter(
            song -> {
              System.out.println("coming song in eager:" + song);
              return song.startsWith("al");
            })
        .count();

    Stream<String> letters = Stream.of("b", "c", "d", null).filter(letter -> letter.isEmpty());

    // Optional<String> firstLetter = Optional.of(letters.findFirst());

  }
Esempio n. 3
0
  @Test
  public void _04_서브스트림_추출() {
    // limit
    Stream<Double> randoms = Stream.generate(Math::random).limit(100);

    assertThat(randoms.count(), is(100L));

    // skip
    Stream<String> words = Stream.of(contents.split("[\\P{L}]+")).skip(1);
    assertThat(words.findFirst().get(), is("was"));
  }
  public static Matrix loadFromFile(File file) throws IOException {
    BufferedReader reader = new BufferedReader(new FileReader(file));
    int dimensionSize = 0;
    Stream<String> lines = reader.lines();
    int objectCount = (int) lines.count();
    reader.close();

    reader = new BufferedReader(new FileReader(file));
    String[] temp = reader.readLine().split(" ");
    double[][] objects = new double[objectCount][temp.length];

    for (int i = 0; i < objectCount; i++) {
      if (i != 0) temp = reader.readLine().split(" ");

      for (int j = 0; j < temp.length; j++) {
        objects[i][j] = Double.parseDouble(temp[j]);
      }
    }

    reader.close();
    return new Matrix(objects);
  }
Esempio n. 5
0
  @Test
  public void _04_스트림_결합() {
    Stream<Character> combined = Stream.concat(characterStream("Hello"), characterStream("World"));

    assertThat(combined.count(), is(10L));
  }