This is the third day of my participation in Gwen Challenge

Micro blog to see a set of javascript operation function explanation, very interesting.

[s, s, s, s] map (s - low) = > [low, low, low,] [s,, s, bring about] the filter (s - >true) = > [s, s] [s, s, bring]. Find (low - >true) = > low [s,, s, bring about] findIndex (low - >true) = >1[s, s, bring about] the fill (low) = > [low, low, low,] [s,, s, bring]. Some (low - >true) = >true[s, s, bring about] every (low - >true) = >false 
Copy the code

Stream operations are divided into intermediate operations and termination operations.

Each intermediate operation transforms the Stream in some way (mapping, filtering, and so on), converting one Stream to another with the same or different element types.

The terminate operation performs a final calculation, such as printing, saving to a collection, or returning an element, on the Stream produced by the last intermediate operation.

Stream pipe operations are usually lazy and do not begin to evaluate until the termination operation is called, and data elements that are not needed to complete the termination operation will never be evaluated. This makes infinite Stream possible.

An example of an infinite Stream:

/** * infinite Stream. * This method returns an infinite prime Stream *@return* /
static Stream<BigInteger> primes(a) {
    return Stream.iterate(BigInteger.ONE.add(BigInteger.ONE), BigInteger::nextProbablePrime);
}

/** * prints out the first 20 primes */
primes().limit(20).forEach(System.out::println);

Copy the code

Use streams with caution. Abusing streams can make program code difficult to read and maintain.

Simply put, is the person reading your code used to using Stream? Does the method name you use in the flow convey your intent well? Wherever you use Stream, you can do it iteratively.

Stream supports object references and ints, longs, and doubles. Char is not supported. It is best to avoid using Stream to process char.

/** * Effective Java 3nd example (P174) **/
"Hello World".chars().forEach(System.out::print);
//output: 721011081081113287111114108100
Copy the code

Stream is suitable for:

  1. Unifies the sequence of transformation elements
  2. Filter the sequence of elements
  3. The order in which elements are merged using a single operation, such as add, join, or compute its minimum
  4. To store sequences of elements in a collection, such as grouping by some common attribute
  5. Searches for sequences of elements that meet certain criteria

Similar to JavaScript at the beginning of this article, let’s list some operations on Stream:

// java stream[s, s, bring]. Distinct () = > [noil, and bring about] [s,, s, bring]. Skip (2) = > [s, bring about] [s, s, bring]. Limit (2) => [■,●]. Count () =>4[s, s, bring about] findFirst (). OrElse,null). The get () = > s [s, s, s, s] map (s - low) = > [low, low, low,] [[s, s], [s, s]]. FlatMap ((s, s) - > stream ((s, s))) = > [s, s, s, S] [s, s, s, s] forEach (s - low) = > [low, low, low,] [s,, s, bring about] the filter (s - >true) = > [s, s] [4.5.2.1].sorted() => [1.2.4.5] [s, s, bring about] the filter (s - >true) = > [s, s] [s, s, bring]. Collect (groupingBy (type)) = > [[s, s], [], [bring]] [...] .collect(Collectors.toList())// Various aggregation operations. For details, see Collectors


// If there are elements, instead of randomly finding elements[s, s, bring about] findAny (). OrElse,null). The get () = > s// Welcome to add


Copy the code

Both lambda and Stream are designed to make code more readable. Code is ultimately written for people to read.

The code for June 4 is added below

                // Assist initialization
		List<String> list = new ArrayList<String>();
		list.add("a");
		list.add("b");
		String[] array = new String[] {"a"."b"."c"};
		
		
		/** * Stream initializes */
		/ / air flow
		Stream<String> emptyStream = Stream.empty(); / / []
		// Collection(List, etc.), Array (Array) to Stream
		Stream<String> listToStream = list.stream(); // [a, b]
		Stream<String> arrayToStream = Stream.of(array); // [a, b, c]
		Stream<String> arrayToStream2 = Stream.of("a"."b"."c"); // [a, b, c]
		// Take part of the array, [1, 3]
		Stream<String> arrayToStream3 = Arrays.stream(array, 1.3); // [b, c]
		/ / builder () to create
		Stream<String> streamBuilder = Stream.<String>builder().add("a").add("b").add("c").build(); // [a, b, c]
		// Generate (func); generate(func); generate(func); generate(func)
		Stream<String> streamGenrated = Stream.<String>generate(() -> {
			Random random = new Random();
			return "" + random.nextInt(100);
		}).limit(10); // [45, 44, 51, 60, 62, 3, 57, 48, 80, 27]
		Iterate (Element(1), Element(n) -> Element(n+1))), iterate(Element(1), Element(n) -> Element(n+1))
		Stream<String> streamIterate = Stream.<String>iterate("a", e -> {
			return (char) (e.charAt(e.length()-1) + 1) + "";
		}).limit(26); // [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z]
		// Int,long,double have their own well-wrapped streams IntStream, LongStream, DoubleStream
		IntStream intStream = IntStream.rangeClosed(1.10);// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
		LongStream longStream = LongStream.range(1.5); // [1, 2, 3, 4]
		Random random = new Random();
		DoubleStream doubleStream = random.doubles(3); / / [0.8078472890889652, 0.33210715318105766, 0.9277914692743904]
		DoubleStream doubleStream2 = DoubleStream.iterate(1l, n -> n + 1l).limit(10); / / [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]
		
		System.out.println(emptyStream.collect(Collectors.toList()));
		System.out.println(listToStream.collect(Collectors.toList()));
		/ / the Stream cannot reuse, one line of code below complains: Java. Lang. An IllegalStateException: the Stream has already had been operated upon the or closed
		// System.out.println(listToStream.collect(Collectors.toList()));
		System.out.println(arrayToStream.collect(Collectors.toList()));
		System.out.println(arrayToStream2.collect(Collectors.toList()));
		System.out.println(arrayToStream3.collect(Collectors.toList()));
		System.out.println(streamBuilder.collect(Collectors.toList()));
		System.out.println(streamGenrated.collect(Collectors.toList()));
		System.out.println(streamIterate.collect(Collectors.toList()));
		System.out.println(intStream.boxed().collect(Collectors.toList()));
		System.out.println(longStream.boxed().collect(Collectors.toList()));
		System.out.println(doubleStream.boxed().collect(Collectors.toList()));
		System.out.println(doubleStream2.boxed().collect(Collectors.toList()));
Copy the code