如何通过其枚举值分组对象?

发布于 2025-02-02 00:05:40 字数 428 浏览 3 评论 0原文

我想编写一个程序,该程序分析附近房屋的房屋。为此,我的程序中有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原谅我要高飞 2025-02-09 00:05:40

您可以使用 java stream api api api a>要过滤您的列表,然后产生计数;

long smallRedRectangularHousesCount = houses
        .stream()
        .filter(h -> h.getSize() == Size.SMALL &&
                h.getColor() == Color.RED &&
                h.getShape() == Shape.RECTANGLE
        )
        .count();

You can use the Java Stream API to filter your list then produce a count, for example;

long smallRedRectangularHousesCount = houses
        .stream()
        .filter(h -> h.getSize() == Size.SMALL &&
                h.getColor() == Color.RED &&
                h.getShape() == Shape.RECTANGLE
        )
        .count();
傲世九天 2025-02-09 00:05:40

首先,让我们获取一些示例枚举值。

enum Size
{ TINY, SMALL, MEDIUM, LARGE, MANSION }

enum Color
{ BEIGE, BROWN, GREEN, GREY }

enum Shape
{ RECTANGLE, SQUARE, ROUND }

我会重新定义该类house记录

此外,跟踪真实属性需要的其他字段超出了尺寸,颜色和形状。为了我们的目的,我将添加a uuid 以识别每个房屋。

record House ( UUID id , Size size, Color color, Shape shape ) {}

一些示例数据。

List < House > houses =
        List.of(
                new House( UUID.fromString( "d23d3532-7e43-441a-ac49-d2bc37d884a2" ) , Size.MEDIUM , Color.BROWN , Shape.RECTANGLE ) ,
                new House( UUID.fromString( "e0e7d84c-8500-4372-a6ac-6b601e50b31e" ) , Size.LARGE , Color.BROWN , Shape.ROUND ) ,
                new House( UUID.fromString( "ab251020-fc2b-4f3a-ae92-4cd6da850ac0" ) , Size.MEDIUM , Color.GREEN , Shape.RECTANGLE ) ,
                new House( UUID.fromString( "c675f75e-335e-418e-b805-d6563c2c6e0f" ) , Size.LARGE , Color.BROWN , Shape.ROUND )
        );

如果您只想搜索列表中的特定属性组合,则可以使用流进行过滤如 idan elhalwani 。

long countBrown = houses.stream().filter( house -> house.color().equals( Color.BROWN ) ).count();

3

对于多个标准(颜色和形状以及大小),请使用在另一个答案中看到的&amp;&amp;语法。

您的问题有些困惑。您似乎在询问所有可能的标准组合将所有房屋分组。如果您进行许多母马查询,那将是一个好主意。提前进行分组是很多工作,但是如果您做很多疑问,那值得。

要进行分组,让我们定义另一个记录,houseprofile仅包含大小,颜色和形状的三个字段。我们将使用循环实例化所有可能组合的对象。

record HouseProfile( Size size , Color color , Shape shape ) { }

我们可以很容易地确定所有可能的组合。在枚举上调用返回该枚举上所有命名对象的数组。然后,我们可以在该数组上循环。

我们为您的3个枚举中的每个枚举中的每一个:sizecolorshape> shape。当我们浏览这些内容时,我们过滤了匹配每个枚举对象的房屋列表。我们最终得到了与枚举对象组合组合相匹配的房屋列表。该清单可能是空的,或者可能有房屋。我们将每个列表放入MAP,一个键值配对的集合中。我们使用houseprofile作为我们地图的钥匙,导致list type house的。

首先定义我们的地图,以保持结果。

Map < HouseProfile, List < House > > housesByProfile = new HashMap <>();

然后循环每个枚举的所有值。

for ( Size size : Size.values() )
{
    List < House > sized = houses.stream().filter( house -> house.size.equals( size ) ).toList();
    for ( Color color : Color.values() )
    {
        List < House > sizedAndColored = sized.stream().filter( house -> house.color.equals( color ) ).toList();
        for ( Shape shape : Shape.values() )
        {
            List < House > sizedAndColoredAndShaped = sizedAndColored.stream().filter( house -> house.shape.equals( shape ) ).toList();
            housesByProfile.put( new HouseProfile( size , color , shape ) , sizedAndColoredAndShaped );
        }
    }
}

转储到控制台。

System.out.println( "housesByProfile = " + housesByProfile );

如果您阅读输出,您会发现三个样本房中的每一个。请注意,对于houseprofile [size =大,color = brown,shape = round]我们有两个house带有IDS的列表-6B601E50B31E“&amp; “ C675F75E-335E-418E-B805-D6563C2C6E0F”。

housesbyprofile = {houseprofile [size =豪宅,颜色=绿色,shape =矩形] = [],houseprofile [size = mansion,color = green,shape = round = round = round = round] = [],houseprofile [size = sign棕色,形状=圆形] = [],houseprofile [size =大,颜色=米色,shape = square] = [],houseprofile [size =中,颜色,颜色=棕色,Shape =矩形] = [house [house [id = d235332-- 7E43-441A-AC49-D2BC37D884A2,尺寸=中=中,颜色,颜色=棕色,形状=矩形]],houseprofile [size = Mansion,Mansion,color = beige,color = beige,shape = rectangle = rectangle = [] shape = square] = [],houseprofile [size =中,颜色=米色,shape = round = round] = [],houseprofile [size =中,color,color,color =米色,shape = retctangle] = [],houseprofile [size = small,small,small,颜色=米色,shape = round] = [],houseprofile [size = small,color =灰色,形状=矩形] = [],houseprofile [size = tiny = tiny,color = green,shape = quart = square = square] = [],houseprofile [尺寸=中等,颜色=绿色,形状=矩形] = [house [id = ab2510202020202b-4f3a-ae92-4cd6da850ac0,size =中=中,颜色,颜色=绿色,形状=矩形=矩形]],houseprofile米色,shape = square] = [],houseprofile [size = small,color = brown,shape =矩形] = [],houseprofile [size = tiny = tiny,color =灰色,灰色,shape = round = round] = [],houseprofile [size = =小,颜色=绿色,形状=圆形] = [],houseprofile [size =豪宅,颜色=棕色,shape =矩形] = [],houseprofile [size =大,颜色= brown,brown,shape = square = square] = [], houseprofile [size =中等,颜色=绿色,形状= round] = [],houseprofile [size =中,颜色,颜色=灰色,shape = square = square] = [],houseprofile [size = size =大,颜色=灰色,灰色,shape =矩形] = [],houseprofile [size = tiny,color = brown,shape =矩形] = [],houseprofile [size =大,color,color = brown = brown,shape = round = round] = [house [id = e0e7d84c-8500-4372-4372- 6B601E50B31E,尺寸=大,颜色=棕色,形状=圆形],房屋[ID = C675F75E-335E-418E-B805-D6563C2C2C6E0F,尺寸=大,= brown = brown,Shape = round = round = round = round = round = round] =棕色,shape = square] = [],houseprofile [size =豪宅,颜色=棕色,shape = square] = [],houseprofile [size = small = small,color =灰色,灰色,shape = square = square = square] = [] =小,颜色=灰色,形状=圆形] = [],houseprofile [size =中,颜色,颜色=灰色,shape =矩形] = [],houseprofile [size =中,彩色,颜色=绿色,shape = square = square] = [] ,houseprofile [size =中,颜色=棕色,shape = round] = [],houseprofile [size = tiny,color =米色,米色,shape = rectangle] = [],houseprofile [size = size =大,颜色=米色,米色,shape = round =圆形=圆] = [],houseprofile [size = tiny,color =米色,shape = round] = [],houseprofile [size =大,color = green,shape = rectangle =矩形] = [],houseprofile [size = medive = mediad,color =米色,shape = square] = [],houseprofile [size = tiny,color = green,shape =矩形] = [],houseprofile [size =大,颜色= green,shape = round = round = round = round] = [],houseprofile [size =大=大,颜色=米色,shape =矩形] = [],houseprofile [size =豪宅,颜色=绿色,形状= square = square] = [],houseprofile [size = mansion,color =灰色=灰色,Shape =矩形] = [],houseprofile [size =豪宅,颜色=米色,shape = square] = [],houseprofile [size = tiny,color = brown,shape = round = round = round] = [],houseprofile [size =大,颜色,颜色=灰色,灰色,shape = square] = [],houseprofile [size =豪宅,颜色=灰色,形状=圆形] = [],houseprofile [size =大,颜色,颜色=棕色,形状=矩形] = [],houseprofile [size = size = edimed = edimen = nedim = brown = brown,shape,shape = square] = [],houseprofile [size =豪宅,颜色=棕色,shape = round] = [],houseprofile [size = small = small,color = green,shape = rectangle] = [],houseprofile [size = tiny,color =灰色,形状=矩形] = [],houseprofile [size =大,颜色=灰色,shape = round] = [],houseprofile [size = small,color = green,shape = square = square = square = square] = [],houseprofile [大小=小,颜色=米色,shape = square] = [],houseprofile [size = tiny,color = green,shape = round] = [],houseprofile [size = small = Small,color = beige,shape = rectangle] = [] ,houseprofile [size =中,颜色=灰色,形状=圆形] = [],houseprofile [size =豪宅,颜色=灰色=灰色,shape = square = square] = [],houseprofile [size = small,small,color = brown = brown = brown,shape = quart = square =方] = [],houseprofile [size =豪宅,颜色=米色,shape = round] = [],houseprofile [size = tiny,color =灰色=灰色,shape = quart = quart] = [] = []}

让我们使用该地图来获得房屋的数量标准的组合。

int countLargeBrownRound = housesByProfile.get( new HouseProfile( Size.LARGE , Color.BROWN , Shape.ROUND ) ).size() ;

2

First, let's get some example enum values.

enum Size
{ TINY, SMALL, MEDIUM, LARGE, MANSION }

enum Color
{ BEIGE, BROWN, GREEN, GREY }

enum Shape
{ RECTANGLE, SQUARE, ROUND }

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.

record House ( UUID id , Size size, Color color, Shape shape ) {}

Some sample data.

List < House > houses =
        List.of(
                new House( UUID.fromString( "d23d3532-7e43-441a-ac49-d2bc37d884a2" ) , Size.MEDIUM , Color.BROWN , Shape.RECTANGLE ) ,
                new House( UUID.fromString( "e0e7d84c-8500-4372-a6ac-6b601e50b31e" ) , Size.LARGE , Color.BROWN , Shape.ROUND ) ,
                new House( UUID.fromString( "ab251020-fc2b-4f3a-ae92-4cd6da850ac0" ) , Size.MEDIUM , Color.GREEN , Shape.RECTANGLE ) ,
                new House( UUID.fromString( "c675f75e-335e-418e-b805-d6563c2c6e0f" ) , Size.LARGE , Color.BROWN , Shape.ROUND )
        );

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.

long countBrown = houses.stream().filter( house -> house.color().equals( Color.BROWN ) ).count();

3

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.

record HouseProfile( Size size , Color color , Shape shape ) { }

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, and Shape. 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 a Map, a collection of key-value pairings. We use HouseProfile as the key to our map, leading to a List of type House.

First define our map, to hold results.

Map < HouseProfile, List < House > > housesByProfile = new HashMap <>();

Then loop all values of each enum.

for ( Size size : Size.values() )
{
    List < House > sized = houses.stream().filter( house -> house.size.equals( size ) ).toList();
    for ( Color color : Color.values() )
    {
        List < House > sizedAndColored = sized.stream().filter( house -> house.color.equals( color ) ).toList();
        for ( Shape shape : Shape.values() )
        {
            List < House > sizedAndColoredAndShaped = sizedAndColored.stream().filter( house -> house.shape.equals( shape ) ).toList();
            housesByProfile.put( new HouseProfile( size , color , shape ) , sizedAndColoredAndShaped );
        }
    }
}

Dump to console.

System.out.println( "housesByProfile = " + housesByProfile );

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 two House objects with IDs of "e0e7d84c-8500-4372-a6ac-6b601e50b31e" & "c675f75e-335e-418e-b805-d6563c2c6e0f".

housesByProfile = {HouseProfile[size=MANSION, color=GREEN, shape=RECTANGLE]=[], HouseProfile[size=MANSION, color=GREEN, shape=ROUND]=[], HouseProfile[size=SMALL, color=BROWN, shape=ROUND]=[], HouseProfile[size=LARGE, color=BEIGE, shape=SQUARE]=[], HouseProfile[size=MEDIUM, color=BROWN, shape=RECTANGLE]=[House[id=d23d3532-7e43-441a-ac49-d2bc37d884a2, size=MEDIUM, color=BROWN, shape=RECTANGLE]], HouseProfile[size=MANSION, color=BEIGE, shape=RECTANGLE]=[], HouseProfile[size=LARGE, color=GREEN, shape=SQUARE]=[], HouseProfile[size=MEDIUM, color=BEIGE, shape=ROUND]=[], HouseProfile[size=MEDIUM, color=BEIGE, shape=RECTANGLE]=[], HouseProfile[size=SMALL, color=BEIGE, shape=ROUND]=[], HouseProfile[size=SMALL, color=GREY, shape=RECTANGLE]=[], HouseProfile[size=TINY, color=GREEN, shape=SQUARE]=[], HouseProfile[size=MEDIUM, color=GREEN, shape=RECTANGLE]=[House[id=ab251020-fc2b-4f3a-ae92-4cd6da850ac0, size=MEDIUM, color=GREEN, shape=RECTANGLE]], HouseProfile[size=TINY, color=BEIGE, shape=SQUARE]=[], HouseProfile[size=SMALL, color=BROWN, shape=RECTANGLE]=[], HouseProfile[size=TINY, color=GREY, shape=ROUND]=[], HouseProfile[size=SMALL, color=GREEN, shape=ROUND]=[], HouseProfile[size=MANSION, color=BROWN, shape=RECTANGLE]=[], HouseProfile[size=LARGE, color=BROWN, shape=SQUARE]=[], HouseProfile[size=MEDIUM, color=GREEN, shape=ROUND]=[], HouseProfile[size=MEDIUM, color=GREY, shape=SQUARE]=[], HouseProfile[size=LARGE, color=GREY, shape=RECTANGLE]=[], HouseProfile[size=TINY, color=BROWN, shape=RECTANGLE]=[], HouseProfile[size=LARGE, color=BROWN, shape=ROUND]=[House[id=e0e7d84c-8500-4372-a6ac-6b601e50b31e, size=LARGE, color=BROWN, shape=ROUND], House[id=c675f75e-335e-418e-b805-d6563c2c6e0f, size=LARGE, color=BROWN, shape=ROUND]], HouseProfile[size=TINY, color=BROWN, shape=SQUARE]=[], HouseProfile[size=MANSION, color=BROWN, shape=SQUARE]=[], HouseProfile[size=SMALL, color=GREY, shape=SQUARE]=[], HouseProfile[size=SMALL, color=GREY, shape=ROUND]=[], HouseProfile[size=MEDIUM, color=GREY, shape=RECTANGLE]=[], HouseProfile[size=MEDIUM, color=GREEN, shape=SQUARE]=[], HouseProfile[size=MEDIUM, color=BROWN, shape=ROUND]=[], HouseProfile[size=TINY, color=BEIGE, shape=RECTANGLE]=[], HouseProfile[size=LARGE, color=BEIGE, shape=ROUND]=[], HouseProfile[size=TINY, color=BEIGE, shape=ROUND]=[], HouseProfile[size=LARGE, color=GREEN, shape=RECTANGLE]=[], HouseProfile[size=MEDIUM, color=BEIGE, shape=SQUARE]=[], HouseProfile[size=TINY, color=GREEN, shape=RECTANGLE]=[], HouseProfile[size=LARGE, color=GREEN, shape=ROUND]=[], HouseProfile[size=LARGE, color=BEIGE, shape=RECTANGLE]=[], HouseProfile[size=MANSION, color=GREEN, shape=SQUARE]=[], HouseProfile[size=MANSION, color=GREY, shape=RECTANGLE]=[], HouseProfile[size=MANSION, color=BEIGE, shape=SQUARE]=[], HouseProfile[size=TINY, color=BROWN, shape=ROUND]=[], HouseProfile[size=LARGE, color=GREY, shape=SQUARE]=[], HouseProfile[size=MANSION, color=GREY, shape=ROUND]=[], HouseProfile[size=LARGE, color=BROWN, shape=RECTANGLE]=[], HouseProfile[size=MEDIUM, color=BROWN, shape=SQUARE]=[], HouseProfile[size=MANSION, color=BROWN, shape=ROUND]=[], HouseProfile[size=SMALL, color=GREEN, shape=RECTANGLE]=[], HouseProfile[size=TINY, color=GREY, shape=RECTANGLE]=[], HouseProfile[size=LARGE, color=GREY, shape=ROUND]=[], HouseProfile[size=SMALL, color=GREEN, shape=SQUARE]=[], HouseProfile[size=SMALL, color=BEIGE, shape=SQUARE]=[], HouseProfile[size=TINY, color=GREEN, shape=ROUND]=[], HouseProfile[size=SMALL, color=BEIGE, shape=RECTANGLE]=[], HouseProfile[size=MEDIUM, color=GREY, shape=ROUND]=[], HouseProfile[size=MANSION, color=GREY, shape=SQUARE]=[], HouseProfile[size=SMALL, color=BROWN, shape=SQUARE]=[], HouseProfile[size=MANSION, color=BEIGE, shape=ROUND]=[], HouseProfile[size=TINY, color=GREY, shape=SQUARE]=[]}

Let’s use that map to get a count of houses meeting a combination of criteria.

int countLargeBrownRound = housesByProfile.get( new HouseProfile( Size.LARGE , Color.BROWN , Shape.ROUND ) ).size() ;

2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文