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

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

Find all common elements in two lists

问题

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

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

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

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

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

  1. 1946-01-12;07:00:00;-1.3;G
  2. 1946-01-12;13:00:00;0.3;G
  3. 1946-01-12;18:00:00;-2.8;G
  4. 1946-01-13;07:00:00;-6.2;G
  5. 1946-01-13;13:00:00;-4.7;G
  6. 1946-01-13;18:00:00;-4.3;G

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

  1. private static List<WeatherDataHandler> weatherData = new ArrayList<>();
  2. public void loadData(String filePath) throws IOException {
  3. // 读取所有数据
  4. List<String> fileData = Files.readAllLines(Paths.get("filePath"));
  5. Map<String, Long> frequencyMap = new LinkedHashMap<>();
  6. LocalDate dateFrom = LocalDate.of(1946, 01, 12);
  7. LocalDate dateTo = LocalDate.of(1946, 01, 14);
  8. List<LocalDate> list1 = new ArrayList<>();
  9. for (LocalDate d = dateFrom; !d.isAfter(dateTo); d = d.plusDays(1)) {
  10. dates.add(d);
  11. }
  12. for (String str : fileData) {
  13. List<String> parsed = parseData(str);
  14. LocalDate dateTime = LocalDate.parse(parsed.get(0));
  15. LocalTime Time = LocalTime.parse(parsed.get(1));
  16. double temperature = Double.parseDouble(parsed.get(2));
  17. String tag = parsed.get(3);
  18. WeatherDataHandler weather = new WeatherDataHandler(dateTime, Time, temperature, tag);
  19. weatherData.add(weather);
  20. List<LocalDate> list2 = Arrays.asList(dateTime);
  21. list2.retainAll(list1);
  22. String strDate = list2.toString();
  23. frequencyMap.compute(strDate,
  24. (date, count) -> count == null ? 1 : count + 1);
  25. }
  26. for (Map.Entry<String, Long> entry : frequencyMap.entrySet()) {
  27. if (entry.getValue() < 24) {
  28. System.out.println(
  29. entry.getKey() + ": " + (24 - entry.getValue()));
  30. }
  31. }
  32. }
英文:

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

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

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

  1. 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:

  1. 1946-01-12;07:00:00;-1.3;G
  2. 1946-01-12;13:00:00;0.3;G
  3. 1946-01-12;18:00:00;-2.8;G
  4. 1946-01-13;07:00:00;-6.2;G
  5. 1946-01-13;13:00:00;-4.7;G
  6. 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?

  1. private static List&lt;WeatherDataHandler&gt; weatherData = new ArrayList&lt;&gt;();
  2. public void loadData(String filePath) throws IOException {
  3. //Read all data
  4. List&lt;String&gt; fileData = Files.readAllLines(Paths.get(&quot;filePath&quot;));
  5. Map&lt;String, Long&gt; frequencyMap = new LinkedHashMap&lt;&gt;();
  6. LocalDate dateFrom = LocalDate.of(1946,01,12);
  7. LocalDate dateTo = LocalDate.of(1946, 01, 14);
  8. List&lt;LocalDate&gt; list1 = new ArrayList&lt;&gt; ();
  9. for (LocalDate d = dateFrom; !d.isAfter(dateTo); d = d.plusDays(1)) {
  10. dates.add(d);
  11. }
  12. for(String str : fileData) {
  13. List&lt;String&gt; parsed = parseData(str);
  14. LocalDate dateTime = LocalDate.parse(parsed.get(0));
  15. LocalTime Time = LocalTime.parse(parsed.get(1));
  16. double temperature = Double.parseDouble(parsed.get(2));
  17. String tag = parsed.get(3);
  18. WeatherDataHandler weather = new WeatherDataHandler(dateTime, Time, temperature, tag);
  19. weatherData.add(weather);
  20. List&lt;LocalDate&gt; list2 = Arrays.asList(dateTime);
  21. list2.retainAll(list1);
  22. String strDate = list2.toString();
  23. frequencyMap.compute(strDate,
  24. (date, count) -&gt; count == null ? 1 : count + 1);
  25. }
  26. for (Map.Entry&lt;String, Long&gt; entry : frequencyMap
  27. .entrySet()) {
  28. if (entry.getValue() &lt; 24){
  29. System.out.println(
  30. entry.getKey() + &quot;: &quot; + (24-entry.getValue()));
  31. }}

答案1

得分: 0

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

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

[更新]

在你的代码中将

  1. if (entry.getValue() < 24)

替换为

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

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

英文:

You can do it as follows:

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

[Update]

Replace

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

with

  1. 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:

确定