我们正在将一些业务逻辑从Clojure迁移到Java。目前,我正在研究两种方法,但是第二种方法存在问题。
第一个是-我相信-将地图转换为矢量,但仅包括我们提供键值的值(并删除空值)。
(defn- spec-vec [tags-with-classification & tag-types]
(into [] (remove nil? (map tags-with-classification tag-types))))
这就是我将其翻译为Java代码的方式:
private List<String> specVec(Map<String, String> tagsWithClassification, List<String> tagTypes) {
List<String> list = new ArrayList<>();
for (String tagType : tagTypes) {
String value = tagsWithClassification.get(tagType);
if (value != null) {
list.add(value);
}
}
return list;
}
我正在发布此消息,因为它是在另一种方法中使用的,但是我可能会误解它的作用。
另一个具有以字符串向量作为键,字符串作为值的映射,但是还有一些与过滤键有关的逻辑,但是我已经理解了这一部分-我认为。
这是我到目前为止获得的Java代码,但是我仍然停留在如何以一种很好的方式将此映射转换为HashMap的建议(因为ArrayList / LinkedList在HashMap中用作键不是一个很好的建议)。还是我应该使用完全不同的方法/不同的结构?在此先感谢:)
请注意:仅返回5个可能的值(4个不同的字符串和null)。
第二个Java方法:
private String bookingModelFor(Map<String, String> tagsWithClassification, List<String> tagTypes) {
List<String> tagNames = new ArrayList<>(Arrays.asList("event", "product", "client-trade-direction", "r-factor-direction"));
List<String> filteredTagValues = specVec(tagsWithClassification, tagNames);
filteredTagValues.addAll(tagTypes);
/*
missing part
*/
return null;
}
第二个Clojure函数:
(defn booking-model-for [tags-with-classification & tag-types]
(get {[:trade :goal :increase :vanilla] "Astrid"
[:trade :goal :decrease :vanilla] "Maja"
[:trade :bloc :increase :vanilla] "Isabelle"
[:trade :bloc :decrease :vanilla] "Luna"
[:trade :perles-plus :increase :call] "Astrid"
[:trade :perles-plus :increase :put] "Luna"
[:trade :perles-plus :increase :kickout] "Astrid"
[:trade :perles-plus :decrease :call] "Maja"
[:trade :perles-plus :decrease :put] "Isabelle"
[:trade :perles-plus :decrease :kickout] "Maja"
[:trade :reverse-perles-plus :increase :call] "Astrid"
[:trade :reverse-perles-plus :increase :put] "Luna"
[:trade :reverse-perles-plus :increase :kickout] "Astrid"
[:trade :reverse-perles-plus :decrease :call] "Maja"
[:trade :reverse-perles-plus :decrease :put] "Isabelle"
[:trade :reverse-perles-plus :decrease :kickout] "Maja"
[:trade :kig :increase :put] "Astrid"
[:trade :kig :increase :kickout] "Maja"
[:trade :kig :decrease :put] "Maja"
[:trade :kig :decrease :kickout] "Astrid"
[:expire :goal :decrease :vanilla] "Maja"
[:expire :bloc :decrease :vanilla] "Luna"
[:expire :kig :decrease :put] "Maja"
[:expire :kig :decrease :kickout] "Astrid"
[:expire :perles-plus :increase :put] "Maja"
[:expire :perles-plus :increase :call] "Isabelle"
[:expire :perles-plus :increase :kickout] "Isabelle"
[:expire :reverse-perles-plus :increase :put] "Maja"
[:expire :reverse-perles-plus :increase :call] "Isabelle"
[:expire :reverse-perles-plus :increase :kickout] "Isabelle"
[:barrier-breach :kig :kickout] "Astrid"
[:barrier-breach :perles-plus :kickout] "Astrid"
[:share-adjustment :goal :r-factor>=1 :vanilla] "Maja"
[:share-adjustment :goal :r-factor<1 :vanilla] "Astrid"
[:share-adjustment :bloc :r-factor>=1 :vanilla] "Luna"
[:share-adjustment :bloc :r-factor<1 :vanilla] "Isabelle"
[:share-adjustment :kig :r-factor>=1 :put] "Maja"
[:share-adjustment :kig :r-factor>=1 :kickout] "Astrid"
[:share-adjustment :kig :r-factor<1 :put] "Astrid"
[:share-adjustment :kig :r-factor<1 :kickout] "Maja"
[:share-adjustment :perles-plus :r-factor>=1 :call] "Astrid"
[:share-adjustment :perles-plus :r-factor>=1 :put] "Luna"
[:share-adjustment :perles-plus :r-factor>=1 :kickout] "Astrid"
[:share-adjustment :perles-plus :r-factor<1 :call] "Maja"
[:share-adjustment :perles-plus :r-factor<1 :put] "Isabelle"
[:share-adjustment :perles-plus :r-factor<1 :kickout] "Maja"
[:share-adjustment :reverse-perles-plus :r-factor>=1 :call] "Astrid"
[:share-adjustment :reverse-perles-plus :r-factor>=1 :put] "Luna"
[:share-adjustment :reverse-perles-plus :r-factor>=1 :kickout] "Astrid"
[:share-adjustment :reverse-perles-plus :r-factor<1 :call] "Maja"
[:share-adjustment :reverse-perles-plus :r-factor<1 :put] "Isabelle"
[:share-adjustment :reverse-perles-plus :r-factor<1 :kickout] "Maja"}
(into (spec-vec tags-with-classification :event :product :client-trade-direction :r-factor-direction) tag-types)))
以下一些测试案例可以帮助我进行迁移:
测试第一个功能:
(deftest spec-vec-test
(is (= [] (#'ksm.build.transform.neds/spec-vec nil)))
(is (= [:trade :goal 1] (#'ksm.build.transform.neds/spec-vec {:a :trade :b :goal :c :decrease :d :vanilla :e 1} :a :b :e))))
测试第二个功能
(deftest booking-model-for-test
(is (nil? (#'ns/booking-model-for {})))
(is (nil? (#'ns/booking-model-for {:a 1 :b 2 :c 3} :d :e)))
(is (= "Maja"
(#'ns/booking-model-for {:event :trade :product :goal :client-trade-direction :decrease :r-factor-direction :vanilla})))
(is (= "Isabelle"
(#'ns/booking-model-for {:event :expire :product :reverse-perles-plus :client-trade-direction :increase :r-factor-direction :kickout})))
(is (= "Luna"
(#'ns/booking-model-for {:event :share-adjustment :product :reverse-perles-plus :client-trade-direction :r-factor>=1 :r-factor-direction :put}))))
答案 0 :(得分:1)
我认为这些标签的顺序并不重要。在这种情况下,您不能做得比传统代码好得多。看到这个
https://cs.stackexchange.com/questions/14208/set-combination-data-structure-and-storage-complexity
您可以将关键字替换为常规字符串,并且可以使用Set而不是List作为键。另外,由于这是恒定数据,因此将查找表存储在static
成员中,以避免在每次调用时重新创建该表。例如
class A {
static Map<Set<String>, String> lut = new HashMap<Set<String>, String>() {{
put(new HashSet<String>(Arrays.asList("x", "y")), "foo");
put(new HashSet<String>(Arrays.asList("x", "y", "z")), "bar");
}};
public static void main(String[] args) {
System.out.println(lut.get(new HashSet<String>(Arrays.asList("x", "y"))));
}
}