【问题标题】:Compare and merging 2 arrays of type myClass in one array of the same type在一个相同类型的数组中比较并合并 2 个 myClass 类型的数组
【发布时间】:2018-08-15 23:34:03
【问题描述】:

我正在尝试合并 2 个已获取 json URL 数据的数组,这 2 个 url 有 1 个名为“bay_id”的共享字段,该字段匹配 3080 个项目。 URL 1 有 3080 个数据,URL 2 有 7100 个数据。我确实比较了 URL 1 中的“bay_id”是否在 URL 2 中具有相同的“bay_id”然后合并。我的 for 循环遇到的问题是,它为所有索引设置了相同的“bay_id”。除了名为 Post 的 myClass 之外,还有所有需要的字段的 setter 和 getter 方法。 我的代码是:

for (int i = 0; i < url_1.length; i++) {

                for (int j = 0; j < url_2.length; j++) {

                    if (url_2[j].getBayId_2().equals(url_1[i].getBayId_1()) ) {

                        for (int k = 0; k < mergedURLs.length; k++) {

                            mergedURLs[k].setBayId_1(url_1[i].getBayId_1());
                            mergedURLs[k].setStatus(url_1[i].getStatus());
                            mergedURLs[k].setLat(url_1[i].getLat());
                            mergedURLs[k].setLon(url_1[i].getLon());
                            mergedURLs[k].setBayId_2(url_2[j].getBayId_2());
                           //and so on for all the fields 
                    }//-------End if statment 
                }//------End for loop url_2
            }//-------end for loop url_1
        }//-------end for loop 


        for (int z = 0; z < mergedURLs.length; z++) {
            System.out.println("New bay_id" + mergedURLs[z].getBayId_1());
        }

【问题讨论】:

  • 你认为最内层循环的作用是什么?哦,是的,它使用相同的值更新 ALL mergedURLs 条目。 --- 也许您的意思是 添加 一个新对象,其值从 url_1[i]url_2[j] 合并到 mergedURLs 数组中的 next 槽?
  • 正确我知道循环是将所有索引分配给相同的值,我的意思是添加 url_1 中的所有字段,并且新数组中 url_2 中的所有字段如下:索引 0 具有字段来自 url_1 和来自 url_2 索引 1 的字段具有来自 url_1 的字段,来自 url_2 索引 2 的字段具有来自 url_1 的字段和来自 url_2 的字段等等......
  • 不会使用Set(如果订单必须保留LinkedHashSet)更容易吗?从您的第一个数组中创建集合,然后添加第二个数组的 url(仅在不重复时才成功),如果需要将其转换回数组。 (或者如果您确实需要合并对象,请改用Map)。

标签: java arrays json


【解决方案1】:

这是一种方法。我创建了一个名为MyUrl 的测试类,它代表数组(urls1urls2)和合并数组中的对象。这个MyUrl 类实现了java.lang.Comparable 接口——这是为了对url1 数组进行排序(更多细节如下)。

合并的过程: 我假设urls2 数组比urls1 有更多的数据。首先,我创建了一个空的ArrayList&lt;MyUrl&gt;,它将包含所有 url - 合并的。然后排序 urls1 数组 - 这是为了通过 bayIdurls2 中搜索 urls1 数组 - 使用 Arrays.binarySearch 方法。

现在,在 for 循环中迭代 urls2 并在 urls1 中搜索每个 bayIdurls2)的匹配项。一旦找到匹配的bayId,数据就会合并并存储在ArrayList 中,否则只存储urls2 数据。在 for 循环结束时,ArrayList 将拥有合并的数据。

下面的示例程序可以编译运行。查看输入数组中的数据和合并的ArrayList 的打印输出。匹配和合并的MyUrls 有一个“MERGED”标志。

示例代码:

import java.util.*;

public class CompareAndMerge {

    // Some test data: two sets of url data. One with lesser urls1 and
    // the other usrls2. Note there are some matching bayIds in both arrays.
    private static MyUrl [] urls1 =  {new MyUrl("abc", "yes"), new MyUrl("xyz", "no"),
                    new MyUrl("url", "true"), new MyUrl("lmn", "false")};
    private static MyUrl [] urls2 =  {new MyUrl("abc", "yes"), new MyUrl("xyz", "no"),
                    new MyUrl("cat", "else"), new MyUrl("rat", "if"),
                    new MyUrl("url", "true"), new MyUrl("lmn", "false")};

    public static void main(String [] args) {

        // The urls1 array is sorted by bayId to allow a binary search -
        // the searching of array by bayId.
        // Also, see the method getMatchingUrl()
        Arrays.sort(urls1, null); // null indicates the elements implement comparable

        // The list is to store the merged urls from bouth the arrays
        List<MyUrl> merged = new ArrayList<>();

        // Iterate over the urls2 (the array with more elements) and store
        // the merged data in the list. The data is merged when there
        // is a match with bayId otherwise just the urls2 data is stored.
        for (MyUrl url2 : urls2) {
            MyUrl url1 = getMatchingUrl(url2);
            if (url1 == null) {
                // no bayId match found
                // store the url2 to merged urls
                merged.add(url2);
            }
            else {
                // found a matching bayId in urls1
                // merge the url1 to url2 data
                // for example: url2.setBayId2(url1.getBayId2()); etc.
                url2.setMerged("MERGED");
                merged.add(url2);
            }
        }

        // Result shows matching bayId's urls are merged.
        // The output has all the urls from both the arrays.
        System.out.println(merged);
    }

    private static MyUrl getMatchingUrl(MyUrl url2) {
        int urls1Index = Arrays.binarySearch(urls1, url2, null); // null indicates comparable elements
        if (urls1Index < 0) {
            return null;
        }
        return urls1[urls1Index];
    }   
}

class MyUrl implements Comparable<MyUrl> {
    private String bayId;
    private String status;
    private String merged;
    public MyUrl(String s1, String s2) {
        bayId = s1;
        status = s2;
    }
    public void setBayId(String s) {
        bayId = s;
    }
    public String getBayId() {
        return bayId;
    }
    public void setStatus(String s) {
        status = s;
    }
    public String getStatus() {
        return status;
    }
    public void setMerged(String s) {
        merged = s;
    }
    @Override public String toString() {
        return bayId + ":" + status + ":" + merged;
    }

    /*
     * Implemented method of java.lang.Comparable
     */
    @Override public int compareTo(MyUrl url) {
        return this.bayId.compareTo(url.getBayId());
    }
}

【讨论】:

  • 感谢您的帮助,我很感激。
猜你喜欢
  • 1970-01-01
  • 2018-09-21
  • 1970-01-01
  • 1970-01-01
  • 2011-11-26
  • 1970-01-01
  • 2020-04-21
  • 2014-10-01
  • 1970-01-01
相关资源
最近更新 更多