what's a good persistent collections framework for use in java?

前端 未结 13 1419
-上瘾入骨i
-上瘾入骨i 2020-12-01 06:39

By persistent collections I mean collections like those in clojure.

For example, I have a list with the elements (a,b,c). With a normal list, if I add d, my original

13条回答
  •  爱一瞬间的悲伤
    2020-12-01 06:50

    Just use the ones in Clojure directly. While obviously you might not want to use the language it's self, you can still use the persistent collections directly as they are all just Java classes.

    import clojure.lang.PersistentHashMap;
    import clojure.lang.IPersistentMap;
    
    IPersistentMap map = PersistentHashMap.create("key1", "value1");
    
    assert map.get("key1").equals("value1");
    IPersistentMap map2 = map.assoc("key1", "value1");
    
    assert map2 != map;
    assert map2.get("key1").equals("value1");
    

    (disclaimer: I haven't actually compiled that code :)

    the down side is that the collections aren't typed, i.e. there are no generics with them.

提交回复
热议问题