copyright技术支持 东莞网站建设郑州同济医院收费高吗
news/
2025/9/28 7:24:20/
文章来源:
copyright技术支持 东莞网站建设,郑州同济医院收费高吗,wordpress底部的横线,南皮哪有做网站的zipkin 自定义采样率在第一篇文章的后续部分#xff0c;这一次我们将编写一些更有用的自定义收集器#xff1a;用于按给定的标准进行分组#xff0c;采样输入#xff0c;批量处理以及在固定大小的窗口上滑动。 分组#xff08;计数事件#xff0c;直方图#xff09; 假… zipkin 自定义采样率 在第一篇文章的后续部分这一次我们将编写一些更有用的自定义收集器用于按给定的标准进行分组采样输入批量处理以及在固定大小的窗口上滑动。 分组计数事件直方图 假设您有一些项目的集合并且想要计算每个项目相对于equals() 出现在此集合中的次数。 这可以使用Apache Commons Collections中的CollectionUtils.getCardinalityMap()来实现。 此方法采用IterableT并返回MapT, Integer 计算每个项目出现在集合中的次数。 但是有时我们不使用equals()而是按输入T的任意属性分组。 例如假设我们有一个Person对象列表我们想计算男性与女性的数量即MapSex, Integer 或年龄分布。 有一个内置的收集器Collectors.groupingBy(FunctionT, K classifier) –但是它从键返回一个映射到映射到该键的所有项。 看到 import static java.util.stream.Collectors.groupingBy;//...final ListPerson people //...
final MapSex, ListPerson bySex people.stream().collect(groupingBy(Person::getSex)); 这很有价值但是在我们的例子中不必要地构建了两个ListPerson 。 我只想知道人数。 没有内置的这种收集器但是我们可以用一种非常简单的方式来组成它 import static java.util.stream.Collectors.counting;
import static java.util.stream.Collectors.groupingBy;//...final MapSex, Long bySex people.stream().collect(groupingBy(Person::getSex, HashMap::new, counting())); 这个重载版本的groupingBy()具有三个参数。 如前所述第一个是键 分类器 功能。 第二个参数创建了一个新地图我们很快就会看到它为什么有用的原因。 counting()是一个嵌套的收集器它将所有同性的人带到一起并将它们组合在一起-在我们的示例中它们只是在到达时对其进行计数。 能够选择地图实现非常有用例如在构建年龄直方图时。 我们想知道在给定年龄下有多少人-但年龄值应排序 final TreeMapInteger, Long byAge people.stream().collect(groupingBy(Person::getAge, TreeMap::new, counting()));byAge.forEach((age, count) -System.out.println(age :\t count)); 我们最终得到了一个从年龄已排序到具有该年龄的人数的TreeMap 。 采样批处理和滑动窗口 Scala中的IterableLike.sliding()方法允许通过固定大小的滑动窗口查看集合。 该窗口从开始处开始在每次迭代中移动给定数量的项目。 Java 8中缺少的这种功能允许使用多种有用的运算符例如计算移动平均值 将大集合分成批处理与Guava中的Lists.partition()比较或每第n个元素进行采样。 我们将为Java 8实现具有类似行为的收集器。 让我们从单元测试开始它应该简要描述我们想要实现的目标 import static com.nurkiewicz.CustomCollectors.slidingUnroll
class CustomCollectorsSpec extends Specification {def Sliding window of #input with size #size and step of 1 is #output() {expect:input.stream().collect(sliding(size)) outputwhere:input | size | output[] | 5 | [][1] | 1 | [[1]][1, 2] | 1 | [[1], [2]][1, 2] | 2 | [[1, 2]][1, 2] | 3 | [[1, 2]]1..3 | 3 | [[1, 2, 3]]1..4 | 2 | [[1, 2], [2, 3], [3, 4]]1..4 | 3 | [[1, 2, 3], [2, 3, 4]]1..7 | 3 | [[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6], [5, 6, 7]]1..7 | 6 | [1..6, 2..7]}def Sliding window of #input with size #size and no overlapping is #output() {expect:input.stream().collect(sliding(size, size)) outputwhere:input | size | output[] | 5 | []1..3 | 2 | [[1, 2], [3]]1..4 | 4 | [1..4]1..4 | 5 | [1..4]1..7 | 3 | [1..3, 4..6, [7]]1..6 | 2 | [[1, 2], [3, 4], [5, 6]]}def Sliding window of #input with size #size and some overlapping is #output() {expect:input.stream().collect(sliding(size, 2)) outputwhere:input | size | output[] | 5 | []1..4 | 5 | [[1, 2, 3, 4]]1..7 | 3 | [1..3, 3..5, 5..7]1..6 | 4 | [1..4, 3..6]1..9 | 4 | [1..4, 3..6, 5..8, 7..9]1..10 | 4 | [1..4, 3..6, 5..8, 7..10]1..11 | 4 | [1..4, 3..6, 5..8, 7..10, 9..11]}def Sliding window of #input with size #size and gap of #gap is #output() {expect:input.stream().collect(sliding(size, size gap)) outputwhere:input | size | gap | output[] | 5 | 1 | []1..9 | 4 | 2 | [1..4, 7..9]1..10 | 4 | 2 | [1..4, 7..10]1..11 | 4 | 2 | [1..4, 7..10]1..12 | 4 | 2 | [1..4, 7..10]1..13 | 4 | 2 | [1..4, 7..10, [13]]1..13 | 5 | 1 | [1..5, 7..11, [13]]1..12 | 5 | 3 | [1..5, 9..12]1..13 | 5 | 3 | [1..5, 9..13]}def Sampling #input taking every #nth th element is #output() {expect:input.stream().collect(sliding(1, nth)) outputwhere:input | nth | output[] | 1 | [][] | 5 | []1..3 | 5 | [[1]]1..6 | 2 | [[1], [3], [5]]1..10 | 5 | [[1], [6]]1..100 | 30 | [[1], [31], [61], [91]]}
} 在Spock中使用数据驱动的测试我成功地立即编写了将近40个测试用例简洁地描述了所有需求。 我希望这些对您来说都是清楚的即使您以前从未看过这种语法。 我已经假设存在方便的工厂方法 public class CustomCollectors {public static T CollectorT, ?, ListListT sliding(int size) {return new SlidingCollector(size, 1);}public static T CollectorT, ?, ListListT sliding(int size, int step) {return new SlidingCollector(size, step);}} 收藏家接连收到物品的事实使工作更加困难。 当然首先收集整个列表并在列表上滑动会比较容易但是却很浪费。 让我们迭代构建结果。 我什至不假装通常可以并行执行此任务所以我将不实现combiner() public class SlidingCollectorT implements CollectorT, ListListT, ListListT {private final int size;private final int step;private final int window;private final QueueT buffer new ArrayDeque();private int totalIn 0;public SlidingCollector(int size, int step) {this.size size;this.step step;this.window max(size, step);}Overridepublic SupplierListListT supplier() {return ArrayList::new;}Overridepublic BiConsumerListListT, T accumulator() {return (lists, t) - {buffer.offer(t);totalIn;if (buffer.size() window) {dumpCurrent(lists);shiftBy(step);}};}Overridepublic FunctionListListT, ListListT finisher() {return lists - {if (!buffer.isEmpty()) {final int totalOut estimateTotalOut();if (totalOut lists.size()) {dumpCurrent(lists);}}return lists;};}private int estimateTotalOut() {return max(0, (totalIn step - size - 1) / step) 1;}private void dumpCurrent(ListListT lists) {final ListT batch buffer.stream().limit(size).collect(toList());lists.add(batch);}private void shiftBy(int by) {for (int i 0; i by; i) {buffer.remove();}}Overridepublic BinaryOperatorListListT combiner() {return (l1, l2) - {throw new UnsupportedOperationException(Combining not possible);};}Overridepublic SetCharacteristics characteristics() {return EnumSet.noneOf(Characteristics.class);}} 我花了很多时间来编写此实现尤其是正确的finisher()所以请不要害怕。 关键部分是一个buffer 它可以收集项目直到可以形成一个滑动窗口为止。 然后丢弃“最旧”的物品并step向前滑动窗口。 我对这种实现并不特别满意但是测试正在通过。 sliding(N) 与sliding(N, 1)同义词将允许计算N项目的移动平均值。 sliding(N, N)将输入分成大小为N批次。 sliding(1, N)获取第N个元素样本。 希望您会发现这个收藏家有用喜欢 翻译自: https://www.javacodegeeks.com/2014/07/grouping-sampling-and-batching-custom-collectors-in-java-8.htmlzipkin 自定义采样率
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/920316.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!