什么是最好的数据结构? java的

时间:2022-09-16 13:39:45

I have a GUI where people can enter data and I'm not sure what the best way to store the data would be. This is how I would visualize the data:

我有一个人们可以输入数据的GUI,我不确定存储数据的最佳方式是什么。这就是我可视化数据的方式:

Cats: Cheetah, lion, puma
Dogs: husky, lab

The user could add a type of animal, like a fox, and then add types of foxes. They could also add new types of cats and dogs to the existing lists of them. What would be the best data-structure to use to store this?

用户可以添加一种动物,如狐狸,然后添加各种类型的狐狸。他们还可以在现有的列表中添加新类型的猫和狗。用于存储此数据的最佳数据结构是什么?

4 个解决方案

#1


0  

You could use a HashMap<String, Set<String>>.

您可以使用HashMap >。 ,set>

Example:

例:

Map<String, Set<String>> animals = new HashMap<>();

// To add a new category of animal
animals.put("Cat", new HashSet<>());
animals.put("Dog", new HashSet<>());

// To add a new animal to an existing category
animals.get("Cat").add("Puma");
animals.get("Cat").add("Tiger");
animals.get("Dog").add("Labrador");

// prints {Cat=[Tiger, Puma], Dog=[Labrador]}
System.out.println(animals);

#2


1  

Map<Family,Set<Species>>

地图 <家庭,集<物种> >

For concrete classes, I might use TreeMap and TreeSet, making sure that my Family and Species types implement comparable (or these types could be replaced by String). These types are sorted, so iterating over them would display data in a sensible order.

对于具体的类,我可能会使用TreeMap和TreeSet,确保我的Family和Species类型实现可比较(或者这些类型可以用String替换)。这些类型是排序的,因此迭代它们将以合理的顺序显示数据。

#3


0  

You could simply use a

你可以简单地使用一个

HashMap<String,ArrayList<String>> container

Where the first String refers to the animal type name and the arraylist holds animal names under that type.

第一个String指的是动物类型名称,而arraylist包含该类型的动物名称。

#4


0  

You can use Guava's MultiMap to manage something like:

您可以使用Guava的MultiMap来管理以下内容:

Map<Key, CollectionOfValues>

I would think about the data model a bit more and see if you can create a wrapper class for the members of each group. This will make the code more readable in the long run. Perhaps:

我会更多地考虑数据模型,看看你是否可以为每个组的成员创建一个包装类。从长远来看,这将使代码更具可读性。也许:

Map<Genus, Members>

#1


0  

You could use a HashMap<String, Set<String>>.

您可以使用HashMap >。 ,set>

Example:

例:

Map<String, Set<String>> animals = new HashMap<>();

// To add a new category of animal
animals.put("Cat", new HashSet<>());
animals.put("Dog", new HashSet<>());

// To add a new animal to an existing category
animals.get("Cat").add("Puma");
animals.get("Cat").add("Tiger");
animals.get("Dog").add("Labrador");

// prints {Cat=[Tiger, Puma], Dog=[Labrador]}
System.out.println(animals);

#2


1  

Map<Family,Set<Species>>

地图 <家庭,集<物种> >

For concrete classes, I might use TreeMap and TreeSet, making sure that my Family and Species types implement comparable (or these types could be replaced by String). These types are sorted, so iterating over them would display data in a sensible order.

对于具体的类,我可能会使用TreeMap和TreeSet,确保我的Family和Species类型实现可比较(或者这些类型可以用String替换)。这些类型是排序的,因此迭代它们将以合理的顺序显示数据。

#3


0  

You could simply use a

你可以简单地使用一个

HashMap<String,ArrayList<String>> container

Where the first String refers to the animal type name and the arraylist holds animal names under that type.

第一个String指的是动物类型名称,而arraylist包含该类型的动物名称。

#4


0  

You can use Guava's MultiMap to manage something like:

您可以使用Guava的MultiMap来管理以下内容:

Map<Key, CollectionOfValues>

I would think about the data model a bit more and see if you can create a wrapper class for the members of each group. This will make the code more readable in the long run. Perhaps:

我会更多地考虑数据模型,看看你是否可以为每个组的成员创建一个包装类。从长远来看,这将使代码更具可读性。也许:

Map<Genus, Members>