V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
andybest
V2EX  ›  问与答

这个代码逻辑怎么优化?

  •  
  •   andybest · 2014-08-03 11:10:46 +08:00 · 2740 次点击
    这是一个创建于 3768 天前的主题,其中的信息可能已经有所发展或是发生改变。
    一个 iterator 每次产生一个 deal,我打算攒 10 个 deal 进行一次处理,现在的问题是因为无法判断最后一次next() 导致处理程序要写两遍:

    List<Deal> pendingDeals=new ArrayList<Deal>();
    while(iterator.hasNext()){
      pendingDeals.add(iterator.next());
      if(!pendingDeals.isEmpty()pendingDeals.size()%10==0){
        process(pendingDeals);
      }
    }
    //这里非常纠结,是否可以优化?
    if(!pendingDeals.isEmpty()){
      process(pendingDeals);
    }
    6 条回复    2014-08-03 19:52:22 +08:00
    kalasoo
        1
    kalasoo  
       2014-08-03 12:14:25 +08:00
    ```
    while(true) {
    if (iterator.hasNext()) {
    pendingDeals.add(iterator.next());
    if (!pendingDeals.isEmpty() && pendingDeals.size() == 10) {
    process(pendingDeals);
    }
    } else {
    if (!pendingDeals.isEmpty()) {
    process(pendingDeals);
    }
    break;
    }
    }
    ```
    或者

    ```
    var end = false; //对不起啊,我Java写的不好,我就把逻辑先放在这里
    while(true){
    end = !iterator.hasNext();
    if ((end || pendingDeals.size() == 10) && !pendingDeals.isEmpty()) {
    process(pendingDeals);
    }
    if (end) {
    break;
    } else {
    pendingDeals.add(iterator.next());
    }
    }
    ```
    deepsleep
        2
    deepsleep  
       2014-08-03 12:54:29 +08:00   ❤️ 1
    个人觉得不用再优化了,原来那样就可以了。
    incompatible
        3
    incompatible  
       2014-08-03 13:16:35 +08:00   ❤️ 1
    楼主这个没法优化了吧 有buffer就要有flush
    类比一下:你用OutputStream做了若干write操作,最后总得flush()一下


    ps:
    你得这么写才像java

    ```
    DealProcessor p = new DealProcessorImpl();

    while(iterator.hasNext()){
      p.process(iterator.next);
    }

    p.flush();
    ```


    ```
    interface DealProcessor {
    public void process(Deal deal);
    public void flush();
    }
    ```

    ```
    class DealProcessorImpl {
    private final static int BATCH_SIZE = 10;
    private List<Deal> dealList = new ArrayList<Deal>();

    public void process(Deal deal) {
    dealList.add(deal);
    if (dealList.size() == BATCH_SIZE) {
    process(dealList):
    dealList.clear();
    }
    }

    public void flush() {
    if (dealList.size() > 0) {
    process(dealList):
    dealList.clear();
    }
    }

    protected void process(List<Deal> batch) {
    // add your implementation here
    }
    ```
    coldear
        4
    coldear  
       2014-08-03 13:17:55 +08:00
    不用再优化+1
    andybest
        5
    andybest  
    OP
       2014-08-03 13:48:54 +08:00
    @incompatible 谢谢,这个 OOP 做的很好,自愧不如! :)
    watzds
        6
    watzds  
       2014-08-03 19:52:22 +08:00   ❤️ 1
    List<Deal> pendingDeals=new ArrayList<Deal>();
    while(iterator.hasNext()){
      pendingDeals.add(iterator.next());
      if(!pendingDeals.isEmpty() && (pendingDeals.size()%10==0 || !iterator.hasNext())){
        process(pendingDeals);
      }
    }
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1364 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 82ms · UTC 17:49 · PVG 01:49 · LAX 09:49 · JFK 12:49
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.