Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在循环中整合冗余信息_Java_Arrays_Arraylist - Fatal编程技术网

Java 在循环中整合冗余信息

Java 在循环中整合冗余信息,java,arrays,arraylist,Java,Arrays,Arraylist,在Java中,我有一个数组,其中包含一长串有时是冗余的键值对,我想将这些键值对合并到一个只有唯一键的较短的arraylist中。我如何编辑下面的代码来实现这一点 以下是outputarr数组中的数据示例: key value 448 Ethanol 448 Alcohol 448 Alcohol 448 Ethanol 448 Ethanol 448 Alcohol 448 Ethanol 448 Alcohol 448 Ethyl alcohol 我想用以下数据将上述内容整合到一个数组中

在Java中,我有一个数组,其中包含一长串有时是冗余的键值对,我想将这些键值对合并到一个只有唯一键的较短的arraylist中。我如何编辑下面的代码来实现这一点

以下是
outputarr
数组中的数据示例:

key value
448 Ethanol
448 Alcohol
448 Alcohol
448 Ethanol
448 Ethanol
448 Alcohol
448 Ethanol
448 Alcohol
448 Ethyl alcohol  
我想用以下数据将上述内容整合到一个数组中:

key value
448 Ethanol; Alcohol; Ethyl alcohol
因此,数组中的9行合并为arraylist中的一行。arraylist中的值是一个串联字符串
“乙醇;乙醇;乙醇”
。但是每个数字键都有不同数量的值,需要以这种方式连接这些值,以便生成一个值字符串,列出与数组中数字键关联的唯一名称

这是我到目前为止的代码,我如何编辑它以使它实现我所描述的

private static ArrayList<ArrayList<String>> twoDimArrList = new ArrayList<ArrayList<String>>();
public void someMethod(){
    int len = 1000;
    String[][] outputarr = new String[len][2];
    // ommitting code that populates outputarr because the next for loop is what needs help

    for(int r=0;r<len;r++){
        ArrayList<String> temp = new ArrayList<String>();
        if(outputarr[r][0]==outputarr[r+1][0]){
            if(outputarr[r][1].equalsIgnoreCase(outputarr[r+1][1])){
                temp.add(outputarr[r][0]);
                temp.add(outputarr[r][1]);
                twoDimArrList.add(temp);
                r+=1;
            }
        }
    }
}  
private static ArrayList twoDimArrList=new ArrayList();
公共方法(){
int len=1000;
字符串[][]outputarr=新字符串[len][2];
//OMMatting填充outputarr的代码,因为下一个for循环需要帮助

对于(int r=0;r有很多方法可以做到这一点。一种方法是使用
Map
保存数据,这将把键映射到值集

然后,当您读取数据时:

Map<Integer,Set<String>> data = new HashMap<Integer,Set<String>>();

for each item in the file {

    Integer key = ...; // parsed 
    String value = ...; // parsed

    // retrieve set if it exists, create if it doesn't.
    Set<String> values = data.get(key);
    if (values == null) { 
        values = new HashSet<String>();
        data.put(key, values);
    }

    // add value to set
    values.add(value);

}

您在当前输出格式化尝试中犯的错误是依赖
Set
s default
toString()
实现生成输出。默认的
toString()
无法真正猜测您的具体需求,您需要迭代数据并生成满足您需求的输出。

您必须使用2d数组吗?我会创建一个对象,使用名为key的int和名为value的arraylist。这样,您就有了一个对象的arraylist,如果您匹配一个key,则添加value你可以使用guava,它有一个HashMultiMap,正好可以这样做。否则,
Map
也可以工作。你可以使用java中的标准HashMap,它的循环体如下:if(Map.get(key)==null){Map.put(key,value)}else{value=Map.get(key);updatevalue;)@assylias SetMultimap(第二个
m
为小写)@j、 con我上面省略了大约40行代码,这些代码处理字符串值以删除无关内容和拼写/大小写错误。如果我可以将其插入到您的对象方法中,那么我就可以使用您的对象方法。结果需要发送到一个txt文件中,该文件将被导入到数据库中。您可以显示co吗de?最好使用Set而不是LIST。此外,当性能非常重要时,请确保不要使用装箱原语…不可能使用“标准”Java集合,但有一些方法可以克服它。@SargeBorsch在这种情况下,读取和解析文件以及操作映射/集的成本将使装箱/取消装箱的成本可以忽略不计。还可以回忆一下
Integer.valueOf()
返回一个已经包装好的类型,这样在加载过程中可能会有所帮助。适当的哈希函数会对性能产生更大的影响——如果性能要求没有得到满足的话。实现一个实际有效的高级算法是首要任务。如果你不想任人摆布的话对于
Map.toString()
(您很少这样做,默认字符串转换无法猜测数据的格式),您必须手动迭代映射中的每个条目(请参见
.entrySet()
),然后手动迭代值集中的每个条目,并以您认为合适的格式写出数据。如果您有特定的订购要求,您可以使用不同的容器,例如
TreeMap
/
TreeSet
将按升序迭代,
LinkedHashMap
/
LinkedHashSet
将保留insert离子顺序。你的电话。@CodeMed我在回答中添加了一些细节。
for (Map.Entry<Integer,Set<String>> entry : data.entrySet()) {
    Integer key = entry.getKey();
    // ...
    for (String value : entry.getValue()) {
       // ...
    }
}