나는 이와 같은 코드가 있습니다. JSoap 라이브러리를 사용합니다. 토렌트 사이트에서 제목, 자석, 씨앗, 거머리 (이 두 가지는 TorrentStats로 함께)를 얻고 있습니다. 이제 하나의 List 안에 병합하고 싶습니다. 물론 표준 for 루프에서 수행하는 것이 매우 쉽지만 스트림에서 매핑하거나 평면 매핑하는 방법이 있습니까?
Document html = Jsoup.connect(SEARCH_URL + phrase.replaceAll("\\s+", "%20")).get();
Elements elements1 = html.select(".detLink");
Elements elements2 = html.select("td > a[href~=magnet:]");
Elements elements3 = html.select("table[id~=searchResult] tr td[align~=right]");
List < String > titles = elements1.stream()
.map(Element::text)
.collect(Collectors.toList());
List < String > magnets = elements2.stream()
.map(e - > e.attr("href"))
.collect(Collectors.toList());
List < TorrentStats > torrentStats = IntStream.iterate(0, i - > i + 2)
.limit(elements3.size() / 2)
.mapToObj(i - > new TorrentStats(Integer.parseInt(elements3.get(i).text()),
Integer.parseInt(elements3.get(i + 1).text())))
.collect(Collectors.toList());
//is there any way to use map or flatmap to connect these 3 list into this one?
List < Torrent > torrents = new ArrayList < > ();
for (int i = 0; i < titles.size(); i++) {
torrents.add(new Torrent(titles.get(i), magnets.get(i), torrentStats.get(i)));
}
IntStream.range
인덱스를 반복 하는 데 사용할 수 있습니다 .
List<Torrent> torrents = IntStream.range(0, titles.size())
.mapToObj(i -> new Torrent(titles.get(i), magnets.get(i), torrentStats.get(i)))
.collect(Collectors.toList());
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다