找到两个列表中的所有共同元素

huangapple go评论91阅读模式
英文:

Find all common elements in two lists

问题

我正试图找出我的 ArrayList "list1" 中的共同元素。

List<LocalDate> list1 = new ArrayList<> ();
for (LocalDate d = dateFrom; !d.isAfter(dateTo); d = d.plusDays(1)) {
    dates.add(d);

其中包含了从 "dateFrom" 到 "dateTo" 之间的所有日期,以及我的列表 "list2"。

List<LocalDate> list2 = Arrays.asList(dateTime);

其中 dateTime 是一个变量,仅存储来自具有以下结构的文本文件的日期:

1946-01-12;07:00:00;-1.3;G
1946-01-12;13:00:00;0.3;G
1946-01-12;18:00:00;-2.8;G
1946-01-13;07:00:00;-6.2;G
1946-01-13;13:00:00;-4.7;G
1946-01-13;18:00:00;-4.3;G

我尝试使用 list2.retainAll(list1); 来查找列表2中也在列表1中的所有日期,但我认为问题在于列表2不是 ArrayList。我该如何修复这个问题?

private static List<WeatherDataHandler> weatherData = new ArrayList<>();
public void loadData(String filePath) throws IOException {
// 读取所有数据
    List<String> fileData = Files.readAllLines(Paths.get("filePath"));

    Map<String, Long> frequencyMap = new LinkedHashMap<>();
    
    LocalDate dateFrom = LocalDate.of(1946, 01, 12);
    LocalDate dateTo = LocalDate.of(1946, 01, 14);

    List<LocalDate> list1 = new ArrayList<>();
    for (LocalDate d = dateFrom; !d.isAfter(dateTo); d = d.plusDays(1)) {
        dates.add(d);
    }

    for (String str : fileData) {
        List<String> parsed = parseData(str);
        LocalDate dateTime = LocalDate.parse(parsed.get(0));
        LocalTime Time = LocalTime.parse(parsed.get(1));
        double temperature = Double.parseDouble(parsed.get(2));
        String tag = parsed.get(3);

        WeatherDataHandler weather = new WeatherDataHandler(dateTime, Time, temperature, tag);
        weatherData.add(weather);

        List<LocalDate> list2 = Arrays.asList(dateTime);

        list2.retainAll(list1); 
        
        String strDate = list2.toString();
        frequencyMap.compute(strDate,
                (date, count) -> count == null ? 1 : count + 1);
    }

    for (Map.Entry<String, Long> entry : frequencyMap.entrySet()) {
        if (entry.getValue() < 24) {
            System.out.println(
                    entry.getKey() + ": " + (24 - entry.getValue()));
        }
    }
}
英文:

I am trying to find the common elements in my ArrayList "list1"

List&lt;LocalDate&gt; list1 = new ArrayList&lt;&gt; ();
for (LocalDate d = dateFrom; !d.isAfter(dateTo); d = d.plusDays(1)) {
      dates.add(d);

that consists of all dates between "dateFrom" and "dateTo" and my list "list2"

List&lt;LocalDate&gt; list2 = Arrays.asList(dateTime);

where dateTime is a variable that stores only the dates from a textfile with the following structure:

1946-01-12;07:00:00;-1.3;G
1946-01-12;13:00:00;0.3;G
1946-01-12;18:00:00;-2.8;G
1946-01-13;07:00:00;-6.2;G
1946-01-13;13:00:00;-4.7;G
1946-01-13;18:00:00;-4.3;G

I tried using list2.retainAll(list1); in order to find all the dates in list2 that is also in list1, but I think my problem is that list2 is not an arraylist. How can I fix that?

private static List&lt;WeatherDataHandler&gt; weatherData = new ArrayList&lt;&gt;();
public void loadData(String filePath) throws IOException {
//Read all data
	List&lt;String&gt; fileData = Files.readAllLines(Paths.get(&quot;filePath&quot;));

	Map&lt;String, Long&gt; frequencyMap = new LinkedHashMap&lt;&gt;();
	
	LocalDate dateFrom = LocalDate.of(1946,01,12);
	LocalDate dateTo = LocalDate.of(1946, 01, 14);

    List&lt;LocalDate&gt; list1 = new ArrayList&lt;&gt; ();
    for (LocalDate d = dateFrom; !d.isAfter(dateTo); d = d.plusDays(1)) {
      dates.add(d);
     
    }

    for(String str : fileData) {
        List&lt;String&gt; parsed = parseData(str);
        LocalDate dateTime = LocalDate.parse(parsed.get(0));
        LocalTime Time = LocalTime.parse(parsed.get(1));
        double temperature = Double.parseDouble(parsed.get(2));
        String tag = parsed.get(3);

        WeatherDataHandler weather = new WeatherDataHandler(dateTime, Time, temperature, tag);
        weatherData.add(weather);

        List&lt;LocalDate&gt; list2 = Arrays.asList(dateTime);

        list2.retainAll(list1); 
        
        String strDate = list2.toString();
        frequencyMap.compute(strDate,
                (date, count) -&gt; count == null ? 1 : count + 1);
    }

    for (Map.Entry&lt;String, Long&gt; entry : frequencyMap
            .entrySet()) {
    	if (entry.getValue() &lt; 24){
        System.out.println(
                entry.getKey() + &quot;: &quot; + (24-entry.getValue()));
    }}

答案1

得分: 0

你可以按照以下方式进行操作:

List<LocalDate> commonList = new ArrayList<LocalDate>();
for (LocalDate date : list1) {
    if(list2.contains(date)) {
        commonList.add(date);
    }
}
System.out.println("Common elements: " + commonList);

[更新]

在你的代码中将

if (entry.getValue() < 24)

替换为

if (entry.getValue() < 24 && !entry.getKey().equals("[]"))

以消除类似 []: 23 的内容。

英文:

You can do it as follows:

List&lt;LocalDate&gt; commonList = new ArrayList&lt;LocalDate&gt;();
for (LocalDate date : list1) {
	if(list2.contains(date)) {
		commonList.add(date);
	}
}
System.out.println(&quot;Common elements: &quot; + commonList)

[Update]

Replace

if (entry.getValue() &lt; 24)

with

if (entry.getValue() &lt; 24 &amp;&amp; !entry.getKey().equals(&quot;[]&quot;))

in your code to get rid of something like []: 23.

huangapple
  • 本文由 发表于 2020年3月4日 03:41:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/60514392.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定