Apache Commons CollectionUtils addAll(Collection集合,Object[]元



简要描述:

基本上,在我的代码中,我想将List <String>添加到ListValuedMap <String, List<String>>中的每个<key>,作为<value>。我用对创建的ListValuedMapspreadNormal进行了一些测试

System.out.println(spreadNormal.keySet());
System.out.println(spreadNormal.values());

它告诉我,键在映射中(未排序(,但相应的值是空的
在每次循环后使用Collections.addAll(String, List<String>)list.clear()后,我将删除插入的List <String>

我本以为这些值的副本确实会保留在我的ListValuedMap中,但我的结果是:

[Agios Pharmaceuticals Inc., Vicor Corp., EDP Renov�veis S.A., Envista Holdings Corp., JENOPTIK AG,...]
[[], [], [], [], [], ...]

我的预期结果更像这样:

[Agios Pharmaceuticals Inc., ...] =
[["US00847X1046, "30,60", "30,80", "0,65"], ....]

你能对此提供一些解释吗?Collections.addAll方法的默认行为是将引用复制到对象,而不是对象本身吗?

相应的代码部分用// ++++++++++突出显示

完整代码示例(Eclipse(:

import java.io.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import java.util.*;
import org.apache.commons.collections4.ListValuedMap;
import org.apache.commons.collections4.MultiSet;
import org.apache.commons.collections4.multimap.ArrayListValuedHashMap;

public class Webdata {

public static void main(String[] args) throws IOException
{
long start = System.currentTimeMillis();

parseData();

System.out.println(secondsElapsed(start)+" seconds processing time");

};

private static void parseData() throws IOException
{

List <String> subdirectories = new ArrayList<>();
String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

String errorMessage1 = "String formatting problem";
String errorMessage2 = "String object non existent";

for (int i=0; i< chars.length(); i++)
{
subdirectories.add("https://www.tradegate.de/indizes.php?buchstabe="+chars.charAt(i));
}


List <String> stockMetadata = new ArrayList<>();

ListValuedMap <String, List<String>> nonError = new ArrayListValuedHashMap<>();
ListValuedMap <String, List<String>> numFormatError = new ArrayListValuedHashMap<>();
ListValuedMap <String, List<String>> nullPointerError = new ArrayListValuedHashMap<>();

ListValuedMap <String, List<String>> spreadTooHigh = new ArrayListValuedHashMap<>();
ListValuedMap <String, List<String>> spreadNormal = new ArrayListValuedHashMap<>();

int cap1 = 44;
int cap2 = 56;

for (int suffix = 0; suffix <chars.length(); suffix++) 
{   
Document doc = Jsoup.connect(subdirectories.get(suffix).toString()).get();
Elements htmlTableRows = doc.getElementById("kursliste_abc").select("tr");

htmlTableRows.forEach( tr-> 
{
String stockName    = tr.child(0).text();
String bid_price    = tr.child(1).text();
String ask_price    = tr.child(2).text();
String isin         = tr.child(0).selectFirst("a").absUrl("href").substring(cap1,cap2);

//     +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
try 
{
if (calcSpread(bid_price, ask_price) < 5) 
{
Collections.addAll(stockMetadata, isin, bid_price, ask_price, calcSpread(bid_price, ask_price).toString());
spreadNormal.put(stockName,stockMetadata);  

}

else if (calcSpread(bid_price, ask_price) > 5)
{
Collections.addAll(stockMetadata, isin, bid_price, ask_price, calcSpread(bid_price, ask_price).toString());
spreadTooHigh.put(stockName,stockMetadata); 

}
stockMetadata.clear();

}
catch (NumberFormatException e) 
{
Collections.addAll(stockMetadata, e.getMessage());
numFormatError.put(stockName, stockMetadata);
stockMetadata.clear();

}

catch (NullPointerException Ne) 
{
Collections.addAll(stockMetadata, Ne.getMessage());
nullPointerError.put(stockName, stockMetadata);
stockMetadata.clear();

}   //end of try-catch
//     +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

});        //end of for-each loop htmlTableRows
}               //end of JSOUP method


System.out.println(spreadNormal.keySet());
System.out.println(spreadNormal.values());

}                   //end of parseData()


public static Float calcSpread (String arg1, String arg2) 
{
try 
{
Float bid = Float.parseFloat(arg1.replace("," , "."));
Float ask = Float.parseFloat(arg2.replace("," , "."));
Float spread = ((ask-bid)/ask)*100;

return spread;
}
catch (NumberFormatException e)
{
return null;
}
}

public static Long secondsElapsed(Long start) {

Long startTime = start;
Long endTime = System.currentTimeMillis();
Long timeDifference = endTime - startTime;

return timeDifference / 1000;
}


}  //end of class

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>TEST</groupId>
<artifactId>TEST</artifactId>
<version>0.0.1-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.3</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.0</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>18</release>
</configuration>
</plugin>
</plugins>
</build>
</project>

Collections.addAll没有任何问题。

我相信你希望你所有的地图都是ListValuedMap<String, String>,基本上是Map<String, List<String>>。因此,您的ListValuedMap<String, List<String>>就是Map<String, List<List<String>>>

只需将每个地图更新为如下所示,即可将每个密钥映射到List<String>:

ListValuedMap<String, String> nonError = new ArrayListValuedHashMap<>();
ListValuedMap<String, String> numFormatError = new ArrayListValuedHashMap<>();
ListValuedMap<String, String> nullPointerError = new ArrayListValuedHashMap<>();
ListValuedMap<String, String> spreadTooHigh = new ArrayListValuedHashMap<>();
ListValuedMap<String, String> spreadNormal = new ArrayListValuedHashMap<>();

然后,不使用ListValuedMap.put(K key, V value),您必须像这样使用ListValuedMap.putAll(K key, Iterable<? extends V> values)

spreadNormal.putAll(stockName, stockMetadata);
spreadTooHigh.putAll(stockName, stockMetadata);
numFormatError.putAll(stockName, stockMetadata);
nullPointerError.putAll(stockName, stockMetadata);

putAll方法将对stockMetadata可迭代项进行迭代,并将元素逐个添加到map的底层列表中。

相关内容

最新更新