代码之家  ›  专栏  ›  技术社区  ›  Atalia.d

如何获取parallelStream()当前索引

  •  0
  • Atalia.d  · 技术社区  · 8 年前

    Jsoup parsing - parsing multiple links simultaneously .

     public static void createPageListByObject(String urlsFileName, int Y) throws IOException {
          //List<String> URLs = new ArrayList<>();
          int indx = 1;
    
          URLs.parallelStream().forEach(URL-> {
            try {
                Page page = Page.Generate(URL, Y);
                FileUtils.writePageToFile(page, indx++);
            }catch (Exception e){
                System.out.println(e.getMessage() + ". Skipping to next url");
            }
        });
    
      public static Page Generate(String URL, int Y) throws IOException, InstantiationException, IllegalAccessException, NoSuchFieldException, URISyntaxException {
        Connection.Response res = Jsoup.connect(URL).userAgent("Chrome/5.0").timeout(10 * 1000).execute();
        Page tutorialPage = new Page(URL);
        return tutorialPage;
    }
    
     public static void writePageToFile(Page page, int i) throws IOException{
        String directoryName = getDirectory(page.vectorXY().Y);
        ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(directoryName + "//page" + i));
    
        os.writeObject(page);
        os.close();
    }
    

    1 回复  |  直到 8 年前
        1
  •  0
  •   shams.kool    8 年前

    Java迭代器实现隐藏了当前索引。实际上,迭代器用于无索引的迭代。

    class UrlObject {
      private String url;
      private Integer index;
      public UrlObject(String url, Integer index){
        .....
      }
      // getter and setter
    }
    

    因此,当您将项目添加到列表时,请使用

    List<UrlObject> URLs = new ArrayList<>();
    URLS.add(new URLObject("url here", <index here>));
    
    URLs.parallelStream().forEach(url-> {
      // code here url.getUrl() and url.getIndex()
    });
    

    或者你可以使用任何其他方法。