p****d 发帖数: 2183 | 1 Unix下管道很方便,可以grep 或者干点别的然后最后sort出来,
这个是pipe & filter 结构吗?
那位达人给个java 实现管道排序的例子啊? |
g*****g 发帖数: 34805 | 2 while((line=reader.readline())!=null) {
if(line.matches(pattern) {
list.add(line);
}
}
Collections.sort(list);
【在 p****d 的大作中提到】 : Unix下管道很方便,可以grep 或者干点别的然后最后sort出来, : 这个是pipe & filter 结构吗? : 那位达人给个java 实现管道排序的例子啊?
|
p****d 发帖数: 2183 | 3 这就不是pipe and filter了把
Colloections做的sort已经是batch sequential的概念了, 它等所有都读完了才处理的
正常pipe & filter是处理一行就输出一行的吧
我看Unix下的sort进程好像也是和前面的管道进程同步运行的啊...
咋搞的
【在 g*****g 的大作中提到】 : while((line=reader.readline())!=null) { : if(line.matches(pattern) { : list.add(line); : } : } : Collections.sort(list);
|
g*****g 发帖数: 34805 | 4 You must be kidding, how do you sort if you don't
read everything into memory first.
【在 p****d 的大作中提到】 : 这就不是pipe and filter了把 : Colloections做的sort已经是batch sequential的概念了, 它等所有都读完了才处理的 : 正常pipe & filter是处理一行就输出一行的吧 : 我看Unix下的sort进程好像也是和前面的管道进程同步运行的啊... : 咋搞的
|
A**o 发帖数: 1550 | 5 magic?
【在 g*****g 的大作中提到】 : You must be kidding, how do you sort if you don't : read everything into memory first.
|
p****d 发帖数: 2183 | 6 呵呵,我就是想知道Unix下是怎么实现的
多谢指点
【在 g*****g 的大作中提到】 : You must be kidding, how do you sort if you don't : read everything into memory first.
|
m******t 发帖数: 2416 | 7
Well, I suppose we could use binary insert _while_ each line is read in, and
output all of them when the EOF comes.
Granted we would still have to have everything in memory at one point though
. There is no getting around that.
【在 g*****g 的大作中提到】 : You must be kidding, how do you sort if you don't : read everything into memory first.
|
m******t 发帖数: 2416 | 8 Something like this I guess:
SortedSet set = new TreeSet();
while((line=reader.readline())!=null) {
if(line.matches(pattern) {
set.add(line);
}
}
【在 g*****g 的大作中提到】 : while((line=reader.readline())!=null) { : if(line.matches(pattern) { : list.add(line); : } : } : Collections.sort(list);
|
g*****g 发帖数: 34805 | 9 I don't think it makes much difference.
Collections.sort is a modified mergesort with guaranteed nlog(n) performance,
You can mix IO and sorting, but performance is equivalent.
【在 m******t 的大作中提到】 : Something like this I guess: : SortedSet set = new TreeSet(); : while((line=reader.readline())!=null) { : if(line.matches(pattern) { : set.add(line); : } : }
|
m******t 发帖数: 2416 | 10
performance,
I agree. The overall response time _might_ be better though, because the
sorting is done concurrently as the lines come in.
【在 g*****g 的大作中提到】 : I don't think it makes much difference. : Collections.sort is a modified mergesort with guaranteed nlog(n) performance, : You can mix IO and sorting, but performance is equivalent. :
|