下面列出了java.util.stream.IntStream#of ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public Collection<Integer> getRtcWorkItemRange() {
String rangesString = props.getProperty(RTC_WORKITEM_ID_RANGE);
String[] ranges = rangesString.split(",");
IntStream intStream = IntStream.of();
for (String range : ranges) {
String[] splitted = range.split("\\.\\.");
int from = Integer.parseInt(splitted[0].trim());
if (splitted.length == 1) {
intStream = IntStream.concat(intStream, IntStream.rangeClosed(from, from));
} else {
int to = Integer.parseInt(splitted[1].trim());
intStream = IntStream.concat(intStream, IntStream.rangeClosed(from, to));
}
}
return intStream.boxed().collect(Collectors.toList());
}
public static IntStream toStream5(int[] arr) {
if (arr == null) {
throw new IllegalArgumentException("Inputs cannot be null");
}
return IntStream.of(arr);
}
/**
* If a value is present in the {@link OptionalInt}, returns an {@link IntStream}
* with the value as its source or else an empty stream.
*
* @since 3.0.0
*/
public static IntStream stream(final OptionalInt self) {
if (!self.isPresent()) {
return IntStream.empty();
}
return IntStream.of(self.getAsInt());
}
public String get() {
if (entriesToRemove.isEmpty()) {
return tweet.getText();
}
IntStream filteredIndexes = IntStream.empty();
for (TweetEntry tweetEntry : entriesToRemove) {
final IntStream nextFilter;
if (tweetEntry.getStart() == tweetEntry.getEnd()) {
nextFilter = IntStream.of(tweetEntry.getStart());
} else {
nextFilter = IntStream.range(tweetEntry.getStart(), tweetEntry.getEnd());
}
filteredIndexes = IntStream.concat(filteredIndexes, nextFilter);
}
final Set<Integer> indexesToFilterOut = filteredIndexes.boxed().collect(toCollection(TreeSet::new));
final int[] chars = tweet.getText().chars().toArray();
final int[] filteredChars = IntStream.range(0, chars.length)
.filter(i -> !indexesToFilterOut.contains(i))
.map(i -> chars[i])
.toArray();
return new String(filteredChars, 0, filteredChars.length)
.replaceAll(" *", " ")
.trim();
}
@Test
public void join_intstream() {
IntStream intStream1 = IntStream.of(1, 2);
IntStream intStream2 = IntStream.of(3, 4);
IntStream.concat(intStream1, intStream2).forEach(
e -> System.out.println(e));
}
@Test
public void sum_unique_values_intstream() {
IntStream stream1 = IntStream.of(1, 2, 3);
IntStream stream2 = IntStream.of(1, 2, 3);
int val = IntStream.concat(stream1, stream2).distinct().sum();
assertEquals(6, val);
// or
IntStream stream3 = IntStream.of(1, 2, 3);
IntStream stream4 = IntStream.of(1, 2, 3);
OptionalInt sum2 = IntStream.concat(stream3, stream4).distinct()
.reduce((a, b) -> a + b);
assertEquals(6, sum2.getAsInt());
}
private static IntStream getAvailableSlots(Inventory inventory_1, Direction direction_1) {
return inventory_1 instanceof SidedInventory ? IntStream.of(((SidedInventory)inventory_1).getInvAvailableSlots(direction_1)) : IntStream.range(0, inventory_1.getInvSize());
}
@Override
protected IntStream stimulus() {
return IntStream.of(25, 50, 75);
}
public static IntStream toIntStream(int[] numbers)
{
return IntStream.of(numbers);
}
@Override
protected IntStream stimulus() {
return IntStream.of(25, 50, 75);
}
protected IntStream stimulus() {
return IntStream.of(75, 100);
}
private static IntStream sparseRegion() {
return IntStream.of(createSorted16BitInts(ThreadLocalRandom.current().nextInt(1, 4096)));
}
static IntStream intProvider() {
return IntStream.of(0, 1);
}
@Override
public IntIterator getIidxUidxs(int iidx) {
return new StreamIntIterator(IntStream.of(18, 20, 100, 101, 102));
}
private static IntStream denseRegion() {
return IntStream.of(createSorted16BitInts(ThreadLocalRandom.current().nextInt(4096, 1 << 16)));
}
public static void main(String[] args) {
final IntStream intStream1 = IntStream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
System.out.println(
intStream1.takeWhile(n -> n <= 5).mapToObj(Integer::toString).collect(Collectors.joining(", ")));
final IntStream intStream2 = IntStream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
System.out.println(intStream2.dropWhile(n -> n < 5).sum());
final List<String> names = List.of("Tom", "Paul", "Mike", "Duke", "", "Michelle", "Anna");
names.stream().takeWhile(s -> !s.isEmpty()).forEach(System.out::println);
}
/**
* If a value is present, returns a sequential {@link IntStream} containing
* only that value, otherwise returns an empty {@code IntStream}.
*
* @apiNote
* This method can be used to transform a {@code Stream} of optional
* integers to an {@code IntStream} of present integers:
* <pre>{@code
* Stream<OptionalInt> os = ..
* IntStream s = os.flatMapToInt(OptionalInt::stream)
* }</pre>
*
* @return the optional value as an {@code IntStream}
* @since 9
*/
public IntStream stream() {
if (isPresent) {
return IntStream.of(value);
} else {
return IntStream.empty();
}
}
/**
* Returns a stream over the array values.
*
* @return a stream over the values in the array
*/
public IntStream stream() {
return IntStream.of(array);
}
public IntStream getFace(int t) { return IntStream.of(p0,t,p1,t,p2,t); }
public IntStream getFace(int t0, int t1, int t2) { return IntStream.of(p0,t0,p1,t1,p2,t2); }