如何通过其枚举值分组对象?
我想编写一个程序,该程序分析附近房屋的房屋。为此,我的程序中有3个枚举,其中包括不同属性,即房屋的大小,颜色和形状。
这些房屋的定义是以下方式:
public House(Size size, Color color, Shape shape) {
this.size = size;
this.color = color;
this.shape = shape;
}
现在我有一个带有几百个不同房屋的阵列列表,我想计算每个房屋的财产组合的组合(例如,“多少房屋是大,蓝色和矩形?”) 。如果我为这些属性的每种可能组合创建一个新的arraylist,我将在代码中有30多个列表和大量的if-loops。
是否有一种更简单的方法可以将具有相同枚举价值的房屋分组在一起?
非常感谢您。
I would like to write a program which analyses properties of houses in a neighborhood. For this I have 3 enums in my program which include the different properties, i.e. size, color and shape of the houses.
The houses are defined the following way:
public House(Size size, Color color, Shape shape) {
this.size = size;
this.color = color;
this.shape = shape;
}
Now I have an ArrayList with a few hundred different houses and I would like to count how many houses each have which combination of properties (for example "how many houses are big, blue and rectangular?"). If I would create a new ArrayList for each possible combination of these properties I would have more than 30 lists and tons of if-loops in my code.
Is there an easier way to group houses with the same enum values together?
Thank you very much in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 java stream api api api a>要过滤您的列表,然后产生计数;
You can use the Java Stream API to filter your list then produce a count, for example;
首先,让我们获取一些示例枚举值。
我会重新定义该类
house
为记录 。此外,跟踪真实属性需要的其他字段超出了尺寸,颜色和形状。为了我们的目的,我将添加a uuid 以识别每个房屋。
一些示例数据。
如果您只想搜索列表中的特定属性组合,则可以使用流进行过滤如 idan elhalwani 。
对于多个标准(颜色和形状以及大小),请使用在另一个答案中看到的
&&
语法。您的问题有些困惑。您似乎在询问所有可能的标准组合将所有房屋分组。如果您进行许多母马查询,那将是一个好主意。提前进行分组是很多工作,但是如果您做很多疑问,那值得。
要进行分组,让我们定义另一个记录,
houseprofile
仅包含大小,颜色和形状的三个字段。我们将使用循环实例化所有可能组合的对象。我们可以很容易地确定所有可能的组合。在枚举上调用
值
返回该枚举上所有命名对象的数组。然后,我们可以在该数组上循环。我们为您的3个枚举中的每个枚举中的每一个:
size
,color
和shape> shape
。当我们浏览这些内容时,我们过滤了匹配每个枚举对象的房屋列表。我们最终得到了与枚举对象组合组合相匹配的房屋列表。该清单可能是空的,或者可能有房屋。我们将每个列表放入MAP
,一个键值配对的集合中。我们使用houseprofile
作为我们地图的钥匙,导致list
typehouse
的。首先定义我们的地图,以保持结果。
然后循环每个枚举的所有值。
转储到控制台。
如果您阅读输出,您会发现三个样本房中的每一个。请注意,对于
houseprofile [size =大,color = brown,shape = round]
我们有两个house
带有IDS的列表-6B601E50B31E“& “ C675F75E-335E-418E-B805-D6563C2C6E0F”。让我们使用该地图来获得房屋的数量标准的组合。
First, let's get some example enum values.
I would redefine that class
House
as a record for brevity and simplicity.Also, tracking real properties requires additional fields beyond just size, color, and shape. For our purposes here, I will add a UUID to identify each individual house.
Some sample data.
If you simply want to search the list for a particular combination of properties, you can use streams to filter as shown in Answer by Idan Elhalwani.
For multiple criteria (color and shape as well as size), use the
&&
syntax seen in the other Answer.Your Question is somewhat confused. You seem to be asking about grouping all houses by all possible combinations of criteria. Doing so would be a good idea if you are doing many mare queries than data changes. Doing the grouping ahead of time is much work, but is worth it if you are doing many queries.
To do the grouping, let's define another record,
HouseProfile
, to contain only the three fields of Size, Color, and Shape. We will use loops to instantiate objects of all possible combinations.We can determine all possible combinations quite easily. Calling
values
on an enum returns an array of all the named objects on that enum. We can then loop on that array.We do this for each of your 3 enums:
Size
,Color
, andShape
. As we go through these, we filter the list of houses that match each enum object. We end up with a list of houses that match each combination of enum objects. That list might be empty, or might have houses. We put each list in aMap
, a collection of key-value pairings. We useHouseProfile
as the key to our map, leading to aList
of typeHouse
.First define our map, to hold results.
Then loop all values of each enum.
Dump to console.
If you read through the output, you will find each of three sample houses. Notice that for
HouseProfile[size=LARGE, color=BROWN, shape=ROUND]
we have a list of twoHouse
objects with IDs of "e0e7d84c-8500-4372-a6ac-6b601e50b31e" & "c675f75e-335e-418e-b805-d6563c2c6e0f".Let’s use that map to get a count of houses meeting a combination of criteria.