Skip to content

Instantly share code, notes, and snippets.

@sunxboy
Created August 10, 2018 06:37
Show Gist options
  • Save sunxboy/a783ceaaf82769d8205cfabee2770b19 to your computer and use it in GitHub Desktop.
Save sunxboy/a783ceaaf82769d8205cfabee2770b19 to your computer and use it in GitHub Desktop.
java8 每100行读取文件
split(Paths.get("file"), 100).forEach(this::sendRequest);
void sendRequest(List<String> each) {
// then you must send the rest request in parallel here
}
Stream<List<String>> split(Path path, int limit) throws IOException {
// skip the remaining lines if its size < limit
return split(Files.lines(path), limit, true);
}
<T> Stream<List<T>> split(Stream<T> source,
int limit, boolean skipRemainingElements) {
//variables just for printing purpose
Spliterator<T> it = source.spliterator();
long size = it.estimateSize();
int c = it.characteristics();// characteristics
return stream(new AbstractSpliterator<List<T>>(size, c) {
private int thresholds = skipRemainingElements ? limit : 1;
@Override
@SuppressWarnings("StatementWithEmptyBody")
public boolean tryAdvance(Consumer<? super List<T>> action) {
List<T> each = new ArrayList<>(limit);
while (each.size() < limit && it.tryAdvance(each::add)) ;
if (each.size() < thresholds) return false;
action.accept(each);
return true;
}
}, false).onClose(source::close);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment