Last active
February 13, 2017 04:27
-
-
Save anuraaga/40e9f5c35a31c870afe4ee4a8c6f5170 to your computer and use it in GitHub Desktop.
Stream vs iterator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2017 LINE Corporation | |
* | |
* LINE Corporation licenses this file to you under the Apache License, | |
* version 2.0 (the "License"); you may not use this file except in compliance | |
* with the License. You may obtain a copy of the License at: | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | |
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | |
* License for the specific language governing permissions and limitations | |
* under the License. | |
*/ | |
package com.linecorp.armeria.server.http.dynamic; | |
import java.util.List; | |
import org.openjdk.jmh.annotations.Benchmark; | |
import org.openjdk.jmh.infra.Blackhole; | |
import com.google.common.collect.ImmutableList; | |
import com.google.common.collect.Streams; | |
public class StreamVsIteratorBenchmark { | |
private static final List<String> REQUIRED = ImmutableList.of("foo", "bar", "cat", "dog"); | |
private static final List<String> ACTUAL = ImmutableList.of("foo", "bar", "cat", "dog"); | |
@Benchmark | |
public void testIterator(Blackhole bh) { | |
bh.consume(containsAllRequiredValues(REQUIRED, ACTUAL)); | |
} | |
@Benchmark | |
public void testStream(Blackhole bh) { | |
bh.consume(containsAllRequiredValuesStream(REQUIRED, ACTUAL)); | |
} | |
private static boolean containsAllRequiredValuesStream( | |
Iterable<String> requiredValues, Iterable<String> actualValues) { | |
return Streams.stream(requiredValues).allMatch(s -> containsRequiredValueStream(s, actualValues)); | |
} | |
private static boolean containsRequiredValueStream(String requiredValue, Iterable<String> actualValues) { | |
return Streams.stream(actualValues).anyMatch(requiredValue::equalsIgnoreCase); | |
} | |
private static boolean containsAllRequiredValues(Iterable<String> requiredValues, | |
Iterable<String> actualValues) { | |
for (String requiredValue : requiredValues) { | |
if (!containsRequiredValue(requiredValue, actualValues)) { | |
return false; | |
} | |
} | |
return true; | |
} | |
private static boolean containsRequiredValue(String requiredValue, Iterable<String> actualValues) { | |
for (String actualValue : actualValues) { | |
if (requiredValue.equalsIgnoreCase(actualValue)) { | |
return true; | |
} | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment