gpt4 book ai didi

java - 带有字符串键和值集的映射

转载 作者:太空宇宙 更新时间:2023-11-04 09:21:42 27 4
gpt4 key购买 nike

我有一个值列表:

{("1", "3"),("1", "5"),("3", "13")("5", "4"),("1", "7"),("8", "9"),("9", "1"),("1", "23"),("5", "7")}

以及必须应用于每个元素“1:3,3:13,5:-”的过滤器。我需要获取另一个列表,其中包含与过滤器匹配的值。在这种情况下,结果应该是:

值:1 3值:3 13值:5 4值:5 7

在过滤器中,此值 5:- 表示它匹配第一个元素,并且不考虑第二个值,因此任何以 5 开头的元素都可以。

import java.util.*;
import java.util.Set;
import java.util.stream.Collectors;

class Filter {
private String valueFilter1;
private String valueFilter2;

public Filter(String valueFilter1, String valueFilter2) {
this.valueFilter1 = valueFilter1;
this.valueFilter2 = valueFilter2;
}

public Filter(String fil) {
String[] filResult = fil.split(":");
this.valueFilter1 = filResult[0];
this.valueFilter2 = filResult[1];
}

public String getValueFilter1() {
return valueFilter1;
}

public String getValueFilter2() { return valueFilter2; }

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Filter that = (Filter) o;
return Objects.equals(valueFilter1, that.valueFilter1) &&
Objects.equals(valueFilter2, that.valueFilter2);
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((valueFilter1 == null) ? 0 : valueFilter1.hashCode());
result = prime * result + ((valueFilter2 == null) ? 0 : valueFilter2.hashCode());
return result;
}

}


public class FilterUtils {
private static void filterMessages(List<Filter> eventsParam, String type) {

Set<Filter> filter1 = new HashSet<>();
Set<String> filter2 = new HashSet<>();

Arrays.stream(type.split(","))
.forEach(f -> {
if (f.contains("-"))
filter2.add(f.split(":")[0]);
else
filter1.add(new Filter(f));
});

// This following logic is in another method called processMessage().
// So I'm trying to return both filters as in processMessage I have something like this:
// .stream()
// .filter(e -> filterMessages(eventsParam).contains(new Filter(e.getValueFilter1(), e.getValueFilter2()))
// .collect(Collectors.toList());
// the real method filterMessages only receives List<Filter> eventsParam as argument
// I was thinking of returning a map of Set like this Map<String, Set<Object>> or Map<String, Set<T>> but it didn't work
// map.put("filter1", filter1);
// map.put("filter2", filter2);
// Not sure how to make it work

List<Filter> result = eventsParam
.stream()
.filter(e -> filter1.contains(new Filter(e.getValueFilter1(), e.getValueFilter2())) |
filter2.contains(e.getValueFilter1()))
.collect(Collectors.toList());

for (Filter r: result) {
System.out.println("Values: " + r.getValueFilter1() + " " + r.getValueFilter2());
}
}



public static void main(String[] args) {
List events = new ArrayList<Filter>();
events.add(new Filter("1", "3"));
events.add(new Filter("1", "5"));
events.add(new Filter("3", "13"));
events.add(new Filter("5", "4"));
events.add(new Filter("1", "7"));
events.add(new Filter("8", "9"));
events.add(new Filter("9", "1"));
events.add(new Filter("1", "23"));
events.add(new Filter("5", "7"));

filterMessages(events, "1:3,3:13,5:-");
}
}

到目前为止,我拥有的代码可以工作,但我认为这种方法有点复杂,而且不允许我将其适应我当前拥有的现有代码。任何想法?或者面对这个更好的方法?谢谢

最佳答案

我建议您在代码审查网站上提出这个问题:https://codereview.stackexchange.com/ .

简单地说:

1) 构造函数 Filter(String valueFilter1, String valueFilter2) 不是必需的。

2) 尽管代码有效,但 equals() 的这种定义在其他情况下可能会导致错误:

Filter a = new Filter("1", "2");
Filter b = new Filter("-", "2");
Filter c = new Filter("5", "2");
System.out.println(a.equals(b)); // true
System.out.println(b.equals(c)); // true
System.out.println(a.equals(c)); // false

按照常识,如果a = b且b = c,那么一定是a = c。但就你而言,事实并非如此。在实际应用程序中,这可能会导致难以发现的错误。

关于java - 带有字符串键和值集<T>的映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58252074/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com