简体   繁体   English

用于存储对象数组列表的二维数组列表或数组列表的映射?

[英]2D arraylist or map of arraylist for storing arraylist of objects?

I'm making a simple bookkeeping program for my dad's art business to help him out a bit with their workflow(a gallery with a bunch of artists that sell their work through one register), and I'm a bit stumped on the direction I should go for storing the data I have.我正在为我父亲的艺术业务制作一个简单的簿记程序,以帮助他完成他们的工作流程(一个画廊,有一群艺术家,通过一个寄存器出售他们的作品),而且我对我的方向有点困惑应该去存储我拥有的数据。 I currently have an arraylist of objects that store each sale every artist makes.我目前有一个对象数组列表,用于存储每个艺术家所做的每次销售。 So it's a few hundred elements long and each element has information about the sale(it's not artist specific, just every sale that's been through the register in 2 weeks).所以它有几百个元素,每个元素都有关于销售的信息(它不是特定于艺术家的,只是在 2 周内通过注册的每个销售)。 It's sorted by name, but ideally I want to split it up into individual artists so they all belong to their own list for better access and control.它按名称排序,但理想情况下,我想将其拆分为各个艺术家,以便他们都属于自己的列表,以便更好地访问和控制。

So now I'm stuck on the best method for this, both basically being a map.所以现在我被困在最好的方法上,基本上都是一张地图。 I was thinking a 2D arrayList, first dimension being a string assigned for the specific artist for easy accessibility and the second being a list with all of their sales that I could add to whenever information is entered.我在想一个 2D arrayList,第一维是为特定艺术家分配的字符串,以便于访问,第二维是一个列表,其中包含他们所有的销售额,我可以在输入信息时添加到其中。

Or I could use a map, but from what I've read a key can only be assigned one value, and I don't really want to make another class just for storing a bunch of objects when an Arraylist does that fine.或者我可以使用地图,但是从我读取的内容来看,一个键只能分配一个值,而且当 Arraylist 做得很好时,我真的不想创建另一个类来存储一堆对象。 Can I make a map with lists as it's elements?我可以用列表作为元素制作地图吗? I haven't messed around with them at all.我根本没有和他们混过。

Or is there just a better way to go about all of this?或者有没有更好的方法来解决所有这些问题? I have yet to take Data Structures so I'm basically using my OOP knowledge the best I can.我还没有学习数据结构,所以我基本上是在尽我所能地使用我的 OOP 知识。 I also plan on serializing the data so it can be used on a rolling basis(They get excel reports bi monthly and Im parsing using Apache) if that changes anything.我还计划序列化数据,以便它可以在滚动的基础上使用(他们每月获得 excel 报告并使用 Apache 进行解析),如果这有任何改变。

Thanks!谢谢!

This is the implementation of what I said in the comments:这是我在评论中所说的实现:

Class Art:课堂艺术:

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class Art implements Serializable {
    public String author;
    public String mainColor;
    public String field3;

    public Art(String author, String mainColor, String field3) {
        this.author = author;
        this.mainColor = mainColor;
        this.field3 = field3;
    }

    private void writeObject(ObjectOutputStream out) throws IOException {
        out.writeObject(author);
        out.writeObject(mainColor);
        out.writeObject(field3);
    }

    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
        author = (String)in.readObject();
        mainColor = (String)in.readObject();
        field3 = (String)in.readObject();
    }
}

class ArtCollection:艺术收藏类:

import java.io.*;
import java.util.*;

public class ArtCollection {
    private final File file;
    private final List<Art> arts = new ArrayList<>();
    private final Map<String,List<Art>> byAuthor = new HashMap<>();
    private final Map<String,List<Art>> byMainColor = new HashMap<>();
    private final Map<String,List<Art>> byField3 = new HashMap<>();

    public ArtCollection(File file) throws ArtException {
        this.file = file;
        try (var in = new ObjectInputStream(new FileInputStream(file))) {
            for (Object art : (Iterable<?>)in.readObject())
                addArt((Art)art);
        } catch (Exception e) {
            throw new ArtException(e);
        }
    }

    public void save() throws ArtException {
        try (var out = new ObjectOutputStream(new FileOutputStream(file))) {
            out.writeObject(arts);
        } catch (Exception e) {
            throw new ArtException(e);
        }
    }

    private void addArt(Art art) {
        arts.add(art);
        byAuthor.computeIfAbsent(art.author, arg->new ArrayList<>()).add(art);
        byMainColor.computeIfAbsent(art.mainColor, arg->new ArrayList<>()).add(art);
        byField3.computeIfAbsent(art.field3, arg->new ArrayList<>()).add(art);
    }

    private void removeArt(Art art) {
        if (arts.remove(art)) {
            byAuthor.get(art.author).remove(art);
            byMainColor.get(art.mainColor).remove(art);
            byField3.get(art.field3).remove(art);
        }
    }

    public List<Art> getByAuthor(String author) {
        return get(author, byAuthor);
    }

    public List<Art> getByMainColor(String mainColor) {
        return get(mainColor, byMainColor);
    }

    public List<Art> getByField3(String field3) {
        return get(field3, byField3);
    }

    private static List<Art> get(String key, Map<String,List<Art>> map) {
        List<Art> arts = map.get(key);
        return arts == null ? Collections.emptyList() : Collections.unmodifiableList(arts);
    }

    public static class ArtException extends RuntimeException {
        public ArtException(Throwable cause) {
            super(cause);
        }
    }
}

Everything is O(1), except for the constructor, save, and removeArt一切都是 O(1),除了构造函数、save 和 removeArt

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

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