import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
Stream.of(
"https://qiita.com/",
"Neko",
"CAT",
"https://www.amazon.co.jp/",
"https://ja.wikipedia.org/wiki/Neko_(%E3%82%BD%E3%83%95%E3%83%88%E3%82%A6%E3%82%A7%E3%82%A2)"
)
.filter(s -> s.startsWith("https://"))
.forEach(System.out::println);
/*
https://qiita.com/
https://www.amazon.co.jp/
https://ja.wikipedia.org/wiki/Neko_(%E3%82%BD%E3%83%95%E3%83%88%E3%82%A6%E3%82%A7%E3%82%A2)
*/
}
}
String <String>
, et après avoir traversé la méthode map, il devient String <Integer>
.import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
Stream.of("Munchkin", "Siamese", "Persian", "Scottish Fold", "Tama")
.map(s -> s.length())
.forEach(System.out::println); //8 7 7 13 4
}
}
IntStream
est généré à la place de String <Integer>
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
Stream.of("Munchkin", "Siamese", "Persian", "Scottish Fold", "Tama")
.mapToInt(s -> s.length())
.forEach(System.out::println); //8 7 7 13 4
}
}
//Convertir une liste de tableaux à deux dimensions en un tableau à une dimension
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
var list = new String[][] {
{ "neko", "nekko", "nekkko" },
{ "inu", "innu" },
{ "tori", "torri" }
};
//Passer un tableau imbriqué avec flatMap et Arrays.Diffuser avec la méthode de flux
//Aplatir en un tableau unidimensionnel en se connectant à flatMap
Arrays.stream(list)
.flatMap(v -> Arrays.stream(v))
.forEach(System.out::println);
// neko nekko nekkko inu innu tori torri
}
}
//Extraire l'acronyme lors de l'aplatissement d'un tableau à deux dimensions
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
var list = new String[][] {
{ "neko", "nekko", "nekkko" },
{ "inu", "innu" },
{ "tori", "torri" }
};
Arrays.stream(list)
.flatMap(v -> Arrays.stream(v).map(str -> str.substring(0, 1)))
.forEach(System.out::println);
//n n n i i t t
}
}
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
Stream.of("Munchkin", "Siamese", "Persian", "Scottish Fold", "Tama")
.sorted()
.forEach(System.out::println);
// Munchkin
// Persian
// Scottish Fold
// Siamese
// Tama
}
}
import java.util.stream.Stream;
import java.util.Comparator;
public class Main {
public static void main(String[] args) {
Stream.of("Munchkin", "Siamese", "Persian", "Scottish Fold", "Tama")
.sorted((str1, str2) -> str1.length() - str2.length())
.forEach(System.out::println);
// Tama
// Siamese
// Persian
// Munchkin
// Scottish Fold
/*Ordre inverse du dictionnaire dans l'interface du comparateur
Stream.of("Munchkin", "Siamese", "Persian", "Scottish Fold", "Tama")
.sorted(Comparator.reverseOrder())
.forEach(System.out::println);
*/
}
}
//Utilisez skip pour ignorer 4 éléments et limit pour en extraire 10 éléments.
import java.util.stream.IntStream;
public class StreamLimit {
public static void main(String[] args) {
IntStream.range(1, 20)
.skip(4)
.limit(10)
.forEach(System.out::println); //5,6,7,8,9,10,11,12,13,14
}
}
import java.util.stream.IntStream;
public class StreamDrop {
public static void main(String[] args) {
IntStream.of(-2, -5, 0, 3, -1, 2)
.dropWhile(i -> i < 0)
.forEach(System.out::println); //0,3,-1,2
}
}
import java.util.stream.IntStream;
public class StreamTake {
public static void main(String[] args) {
IntStream.of(-2, -5, 0, 3, -1, 2)
.takeWhile(i -> i < 0)
.forEach(System.out::println); //-2,-5
}
}
//Contenu du flux de sortie avant et après le tri
import java.util.stream.Stream;
public class StreamPeek {
public static void main(String[] args) {
Stream.of("Poisson", "Crasse", "écho", "Kinmokusei")
.peek(System.out::println)
.sorted()
.forEach(System.out::println);
}
}
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
Stream.of("Chat", "Chat", "Nekko", "Chat", "ChatChat")
.distinct()
.forEach(System.out::println);
//Chat
//Chat
//Nekko
//chat Chat
}
}
import java.util.HashSet;
import java.util.stream.Stream;
public class StreamDistinctObj {
public static void main(String[] args) {
var set = new HashSet<String>();
Stream.of(
new Person("Yamada", 40),
new Person("Takano", 30),
new Person("Okawa", 35),
new Person("Yamada", 45)
)
//Ajouter de la valeur à HashSet dans la méthode de filtrage
//HashSet.La méthode add renvoie false si la valeur n'a pas pu être ajoutée (dupliquer)
.filter(p -> set.add(p.name))
.forEach(System.out::println);
}
}
public class Person {
public String name;
public int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return String.format("%s(%d ans)", this.name, this.age);
}
}
//Conversion d'IntStream vers DoubleStream
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
IntStream.range(1, 5)
.asDoubleStream()
.forEach(System.out::println);
//1.0 2.0 3.0 4.0
}
}
//IntStream pour diffuser<Integer>Conversion en
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
IntStream.range(1, 5)
.boxed()
.forEach(System.out::println);
//1 2 3 4
IntStream.range(1, 5)
.mapToObj(Integer::valueOf)
.forEach(System.out::println);
//1 2 3 4
}
}
//Stream<Integer>Convertir en IntStream
import java.util.stream.Stream;
public class StreamUnboxed {
public static void main(String[] args) {
Stream.of(1, 2, 3, 4)
.mapToInt(i -> i) //Unboxing Integer → int
.forEach(System.out::println); //1 2 3 4
}
}
Recommended Posts