简体   繁体   English

使用更多维度在java中存储数据的最佳方法?

[英]The best way to store data in java with more dimensions?

I have some input like : 我有一些输入,如:

  package 1 : org.orchestr.something  version= 5.1.3

      uses = org.do.resource   ; version= 1.2.1
      uses = org.test.summer   ; version= 1.5.2


  package 2 : fr.test.something  version= 5.1.3

      uses = com.java.rest     ; version= 1.0.1
      uses = org.osgi.summer   ; version= 1.4.2

  ....

So in this data I have set of packages defined by their version and bunch others packages that they use within a specific version, and i want to know with is the best way/ practical way to store this kind of data. 所以在这个数据中,我有一组由他们的版本定义的包,并且他们在特定版本中使用了其他包,我想知道的是存储这种数据的最佳方式/实用方法。

It sounds like you intend to represent some graph like structure. 听起来你打算代表一些图形结构。

A first naive implementation could/would be using specific classes, like Package and Entry where a Package probably has a list/set of of Entry objects (and an Entry has a name + version string). 第一个天真的实现可以/将使用特定的类,例如PackageEntry ,其中Package可能具有Entry对象的列表/集合(并且Entry具有名称+版本字符串)。

Of course, as you are in the end intend to create some sort of graph, you probably should look into solutions such as graphql , too. 当然,因为你最终打算创建某种图形,你可能也应该研究诸如graphql之类的解决方案。

Something like this might work, assuming there's one-level of nested dependency in your case: 这样的事情可能有用,假设在你的情况下有一级嵌套依赖:

A base Artifact class: 基础Artifact类:

  class Artifact {
    protected String name;
    protected String version;

    public Artifact(String name, String version) {
      this.name = name;
      this.version = version;
    }

    public String getName() {
      return name;
    }

    public void setName(String name) {
      this.name = name;
    }

    public String getVersion() {
      return version;
    }

    public void setVersion(String version) {
      this.version = version;
    }
  }

A Package class that extends Artifact and adds another parameter uses : 扩展Artifact并添加另一个参数的Packageuses

class Package extends Artifact {

    public Package(String name, String version, List<Artifact> uses) {
      super(name, version);
      this.uses = uses;
    }

    private List<Artifact> uses;

    public List<Artifact> getUses() {
      return uses;
    }

    public void setUses(List<Artifact> uses) {
      this.uses = uses;
    }
  }

You can then store it as a List or Map as needed. 然后,您可以根据需要将其存储为ListMap

Example of using in a Map : Map中使用的示例:

    Map<String, Package> map = new HashMap<>();
    map.put("package1", new Package("org.orchestr.something", "5.1.3", Arrays.asList(
        new Artifact("org.do.resource", "1.2.1"),
        new Artifact("org.test.summer", "1.5.2")))
    );

    map.put("package2", new Package("fr.test.something", "5.1.3", Arrays.asList(
        new Artifact("com.java.rest", "1.0.1"),
        new Artifact("org.osgi.summer", "1.4.2")))
    );

The package and uses data look equal. 包和使用数据看起来相同。 So i would use a map: 所以我会用一张地图:

class Uses { private path, version; }
Map<Uses, Set<Uses>> data;

The packages are the keys, the used dependencies are elements of the value. 包是键,使用的依赖项是值的元素。

You could model it as a map of artifacts : 您可以将其建模为工件图:

import java.util.Objects;

public class Artifact {
    private String name;
    private String version;

    public Artifact(String name, String version) {
        this.name = name;
        this.version = version;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Artifact that = (Artifact) o;
        return version.equals(that.version) &&
                name.equals(that.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(version, name);
    }
}

And here would be a usage of this : 这将是一个用法:

    public static void main(String[] args) {
        Map<Artifact, Set<Artifact>>  map = new HashMap<>();


        Set<Artifact> dependencies = new HashSet<>();
        dependencies.add(new Artifact("org.do.resource ", "1.2.1"));
        dependencies.add(new Artifact("org.test.summer ", "1.5.2"));

        map.put(new Artifact("org.orchestr.something", "5.1.3"), dependencies);
    }

I had to override equals and hashcode methods for Artifact class because objects of this class will be used as keys in HashMap . 我不得不重写Artifact类的equalshashcode方法,因为这个类的对象将用作HashMap键。 If you want to also support package version just add such field to Artifact class and add apropriate constructor, getters, setters and modify equals and hashcode methods accordingly. 如果您还想支持包版本,只需将此字段添加到Artifact类,并相应地添加适当的构造函数,getter,setter和修改equalshashcode方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM