楼主: ReneeBK
2532 27

[Ken Kousen]Modern Java Recipes [推广有奖]

11
Nicolle(未真实交易用户) 学生认证  发表于 2017-8-7 03:38:21
提示: 作者被禁止或删除 内容自动屏蔽

12
Nicolle(未真实交易用户) 学生认证  发表于 2017-8-7 03:41:02
提示: 作者被禁止或删除 内容自动屏蔽

13
Nicolle(未真实交易用户) 学生认证  发表于 2017-8-7 03:42:23
提示: 作者被禁止或删除 内容自动屏蔽

14
Lisrelchen(未真实交易用户) 发表于 2017-8-7 03:49:02
  1. Example 4-8. Converting a stream of strings to a list
  2. List<String> strings = Stream.of("this", "is", "a", "list", "of"
  3. .collect(Collectors.toList());
复制代码

15
Lisrelchen(未真实交易用户) 发表于 2017-8-7 03:49:55
  1. Example 4-9. Converting a stream of int to a list of Integer (DOES NOT COMPILE)
  2. IntStream.of(3, 1, 4, 1, 5, 9)
  3. .collect(Collectors.toList()); // does not compile
  4. You have three alternatives available as work-arounds. First, use the boxed
  5. method on Stream to convert the IntStream to a Stream<Integer>.
复制代码

16
Lisrelchen(未真实交易用户) 发表于 2017-8-7 03:51:21
  1. Example 4-10. Using the boxed method
  2. List<Integer> ints = IntStream.of(3, 1, 4, 1, 5, 9)
  3. .boxed()
  4. .collect(Collectors.toList());
  5. Converts int to Integer
复制代码

17
Lisrelchen(未真实交易用户) 发表于 2017-8-7 03:53:11
  1. Example 4-11. Using the mapToObj method
  2. List<Integer> ints = IntStream.of(3, 1, 4, 1, 5, 9)
  3. .mapToObj(Integer::valueOf)
  4. .collect(Collectors.toList())
  5. Just as mapToInt, mapToLong, and mapToDouble parses streams of objects
  6. into the associated primitives, the mapToObj method from IntStream,
  7. LongStream, and DoubleStream converts primitives to instances of the
  8. associated wrapper classes. The argument to mapToObj in this example uses
  9. the Integer constructor.
复制代码

18
Lisrelchen(未真实交易用户) 发表于 2017-8-7 03:56:27
  1. Example 4-12. Using the three-argument version of collect
  2. List<Integer> ints = IntStream.of(3, 1, 4, 1, 5, 9)
  3. .collect(ArrayList<Integer>::new, ArrayList::add, ArrayList:
  4. In this version of collect, the supplier is the constructor for
  5. ArrayList<Integer>, the accumulator is the add method, which represents
  6. how to add a single element to a list, and the combiner (which is only used
  7. during parallel operations) is addAll, which combines two lists into one.
  8. Using the three-argument version of collect is not very common, but
  9. understanding how it works is a useful skill.
复制代码

19
ReneeBK(未真实交易用户) 发表于 2017-8-7 04:02:48
  1. Example 4-13. Convert an IntStream to an int array
  2. int[] intArray = IntStream.of(3, 1, 4, 1, 5, 9).toArray();
  3. // or
  4. int[] intArray = IntStream.of(3, 1, 4, 1, 5, 9).toArray(int[]::
  5. The first demo uses the default form of toArray, which returns Object[]. The
  6. second uses an IntFunction<int[]> as a generator, which creates an int[]
  7. of the proper size and populates it.
  8. The fact that any of these approaches is necessary is yet another consequence
  9. of the original decision in Java to treat primitives differently from objects,
  10. complicated by the introduction of generics. Still, using boxed or mapToObj is
  11. easy enough once you know to look for them.
复制代码

20
ReneeBK(未真实交易用户) 发表于 2017-8-7 04:05:59
  1. Example 4-14. Summary statistics from doubles
  2. DoubleSummaryStatistics stats = DoubleStream.generate(Math::random
  3. .limit(1_000_000)
  4. .summaryStatistics();
  5. System.out.println(stats);
  6. Note
  7. A cool feature introduced in Java 7 is that you can use an underscore inside
  8. numeric literals as a readable delimiter which is ignored by the compiler.
  9. The DoubleSummaryStatistics class has methods to return the count, sum,
  10. average, max, and min values individually
复制代码

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2026-1-22 08:23