Java中的持久数据结构 [英] Persistent data structures in Java

查看:116
本文介绍了Java中的持久数据结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道一个库或者至少有一些关于在Java中创建和使用持久数据结构的研究?我不将持久性称为长期存储,而是在不变性方面保持持久性(请参阅维基百科条目 )。



我正在探索不同的方式为持久性结构建立一个api。使用构建器似乎是一个有趣的解决方案:

  //创建持久化实例
Person p = Builder.create(Person
.withName(Joe)
.withAddress(Builder.create(Address.class)
.withCity(paris)
.build())
.build();

//更改持久性实例,即创建一个新的
Person p2 = Builder.update(p).withName(Jack);

个人p3 = Builder.update(p)
.withAddress(Builder.update(p.address())
.withCity(Berlin)
。 build)
.build();

但这仍然感觉有点夸张。任何想法?

解决方案

我想明显的选择是:



o切换到临时数据结构(构建器)以进行更新。这是很正常的例如 String StringBuilder 。作为您的例子。

  Person p3 = 
Builder.update(p)
.withAddress(
Builder.update(p.address())
.withCity(Berlin)
.build()

.build();

o始终使用持久化结构。虽然似乎有很多的复制,你实际上应该分享几乎所有的状态,所以它没有任何地方近似乎看起来不错。

  final Person p3 = p 
.withAddress(
p.address()。withCity(Berlin)
);

o将数据结构分解成许多变量,并与一个巨大而混乱的构造函数进行重组。 >

  final Person p3 = Person.of(
p.name(),
Address.of(
p.house(),p.street(),Berlin,p.country()
),
px(),
py(),
pz )
);

o使用回拨接口提供新数据。更多的样板。

  final Person p3 = Person.of(new PersonInfo(
public String name(){return p.name();)
public Address address(){return Address.of(new AddressInfo(){
private final Address a = p.address();
public String house ){return a.house();}
public String street(){return a.street();}
public String city(){returnBerlin;}
public String country(){return a.country();}
})),
public Xxx x(){return px(); }
public Yyy y(){return p.y(); }
public Zzz z(){return p.z(); }
});

o使用讨厌的黑客使字段暂时可用于代码。

  final Person p3 = new PersonExploder(p){{
a = new AddressExploder(a){{
city =Berlin;
}}。get();
}}。get();

(有趣的是,我刚放下了克里斯·冈崎的纯功能数据结构副本。)


Does anyone know a library or some at least some research on creating and using persistent data structures in Java? I don't refer to persistence as long term storage but persistence in terms of immutability (see Wikipedia entry).

I'm currently exploring different ways to model an api for persistent structures. Using builders seems to be a interesting solution:

// create persistent instance
Person p = Builder.create(Person.class)
             .withName("Joe")
             .withAddress(Builder.create(Address.class)
                 .withCity("paris")
                 .build())
              .build();

// change persistent instance, i.e. create a new one 
Person p2 = Builder.update(p).withName("Jack");

Person p3 = Builder.update(p)
              .withAddress(Builder.update(p.address())
                .withCity("Berlin")
                .build)
            .build();

But this still feels somewhat boilerplated. Any ideas?

解决方案

I guess the obvious choices are:

o Switch to a transient data structure (builder) for the update. This is quite normal. StringBuilder for String manipulation for example. As your example.

Person p3 =
    Builder.update(p)
    .withAddress(
        Builder.update(p.address())
       .withCity("Berlin")
       .build()
    )
    .build();

o Always use persistent structures. Although there appears to be lots of copying, you should actually be sharing almost all state, so it is nowhere near as bad as it looks.

final Person p3 = p
    .withAddress(
        p.address().withCity("Berlin")
    );

o Explode the data structure into lots of variables and recombine with one huge and confusing constructor.

final Person p3 = Person.of(
    p.name(),
    Address.of(
       p.house(), p.street(), "Berlin", p.country()
    ),
    p.x(),
    p.y(),
    p.z()
 );

o Use call back interfaces to provide the new data. Even more boilerplate.

final Person p3 = Person.of(new PersonInfo(
    public String  name   () { return p.name(); )
    public Address address() { return Address.of(new AddressInfo() {
       private final Address a = p.address();
       public String house  () { return a.house()  ; }
       public String street () { return a.street() ; }
       public String city   () { return "Berlin"   ; }
       public String country() { return a.country(); }
    })),
    public Xxx     x() { return p.x(); }
    public Yyy     y() { return p.y(); }
    public Zzz     z() { return p.z(); }
 });

o Use nasty hacks to make fields transiently available to code.

final Person p3 = new PersonExploder(p) {{
    a = new AddressExploder(a) {{
        city = "Berlin";
    }}.get();
}}.get();

(Funnily enough I was just put down a copy of Purely Functional Data Structures by Chris Okasaki.)

这篇关于Java中的持久数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆