合并具有空值的2个对象的数组列表

时间:2021-10-04 07:09:26

I'm trying to concatenate 2 array lists of objects into one but i can't figure out how to do it. I've tried with addAll and add but those methods won't really do what i want. Basically, i have one array list with values like this:

我正在尝试将2个对象的数组列表连接成一个,但我无法弄清楚如何做到这一点。我已尝试使用addAll并添加,但这些方法不会真正做我想要的。基本上,我有一个数组列表,其值如下:

SearchResult1 [title=null, url=null, price=19 690 EUR]

And another one with values like this:

还有一个像这样的值:

SearchResult2 [title=Ford Car, url=http://www.something.com, price=null]

How can i combine those 2 arrays into one with values like this:

如何将这两个数组合并为一个具有以下值的数组:

SearchResult3 [title=Ford Car, url=http://www.something.com, price=19 690 EUR]

This is the code so far:

这是到目前为止的代码:

  public List searchMethod() {
                try {
                    final String query = "ford";

                    final Document page = Jsoup.connect("link" + URLEncoder.encode(query, "UTF-8")).userAgent(USER_AGENT).get();

                     List<SearchResult> resultList1 = new ArrayList<SearchResult>();
                     List<SearchResult> resultList2 = new ArrayList<SearchResult>();
                     List<SearchResult> resultList3 = new ArrayList<SearchResult>();


                    for(Element searchResult : page.select(".offer-price")) {
                         String price = searchResult.text();
                         resultList1.add(new SearchResult(price));
                    }

                    for(Element searchResult : page.select(".offer-title__link")) {
                         String title = searchResult.text();
                         String url = searchResult.attr("href");
                         resultList2.add(new SearchResult(title, url));
                    }
                    resultList3.addAll(resultList1);
                    resultList3.addAll(resultList2);

                    return resultList3;

                }catch(Exception e) {
                    e.printStackTrace();
                }

                return Collections.emptyList();

        }

The values that i put in those arrays are extracted from a web page Thanks for helping!

我放在这些数组中的值是从网页中提取的,感谢您的帮助!

2 个解决方案

#1


1  

From the comment, you have said that you just want to correlate/merge the objects from both lists by each index.

从评论中,您已经说过,您只想通过每个索引关联/合并两个列表中的对象。

You can simply loop through the list, constructing a new SearchResult (assuming you have getters for the fields)

你可以简单地遍历列表,构建一个新的SearchResult(假设你有字段的getter)

for(int i = 0; i < resultList1.size(); i++) {
    resultList3.add(new SearchResult(resultList1.get(i).getPrice(),
                   resultList2.get(i).getTitle(),
                   resultList2.get(i).getUrl()));
}

You may have to change the order of the passed arguments to the SearchResult constructor taking price, title and url as you haven't shown it.

您可能必须更改传递给SearchResult构造函数的参数的顺序,因为您没有显示它的价格,标题和网址。

#2


1  

why don't you do it in one shot?

你为什么不一次性做?

List<SearchResult> resultList1 = new ArrayList<SearchResult>();
for(Element searchResult : page.select(".offer-title__link")) {
    String title = searchResult.text();
    String url = searchResult.attr("href");
    resultList1.add(new SearchResult(title, url));
}
int index = 0;
for(Element searchResult : page.select(".offer-price")) {
    String price = searchResult.text();
    //since you have already assumed 
    //that price will come in the same order and title and url.
    resultList1.get(index++).setPrice(price);
}
return resultList1;

#1


1  

From the comment, you have said that you just want to correlate/merge the objects from both lists by each index.

从评论中,您已经说过,您只想通过每个索引关联/合并两个列表中的对象。

You can simply loop through the list, constructing a new SearchResult (assuming you have getters for the fields)

你可以简单地遍历列表,构建一个新的SearchResult(假设你有字段的getter)

for(int i = 0; i < resultList1.size(); i++) {
    resultList3.add(new SearchResult(resultList1.get(i).getPrice(),
                   resultList2.get(i).getTitle(),
                   resultList2.get(i).getUrl()));
}

You may have to change the order of the passed arguments to the SearchResult constructor taking price, title and url as you haven't shown it.

您可能必须更改传递给SearchResult构造函数的参数的顺序,因为您没有显示它的价格,标题和网址。

#2


1  

why don't you do it in one shot?

你为什么不一次性做?

List<SearchResult> resultList1 = new ArrayList<SearchResult>();
for(Element searchResult : page.select(".offer-title__link")) {
    String title = searchResult.text();
    String url = searchResult.attr("href");
    resultList1.add(new SearchResult(title, url));
}
int index = 0;
for(Element searchResult : page.select(".offer-price")) {
    String price = searchResult.text();
    //since you have already assumed 
    //that price will come in the same order and title and url.
    resultList1.get(index++).setPrice(price);
}
return resultList1;