简体   繁体   English

Java中的n维结构/地图

[英]n-dimensional structure / map in java

I need a n-dimensional structure (map) in java. 我需要Java中的n维结构(地图)

Let's say we have an animal shop and we want to keep minimal prices for certain type of animal. 假设我们有一家动物商店,并且希望对某些类型的动物保持最低价格。

Let's say our criterias are: 假设我们的标准是:

  • Type: Cat, Dog, ANY. 类型:猫,狗,任何。
  • Age: 1,2,ANY. 年龄:1,2,ANY。
  • Color: Brown, Red, ANY. 颜色:棕色,红色,任意色。
  • Sex: Male, Female, ANY. 性别:男,女,任何。

Result price should be the lowest from all relating configurations. 结果价格应为所有相关配置中的最低价。

For example min. 例如分钟 price for 价格

  • Cat is 20, 猫是20
  • 1year-old is: 10, 1岁是:10,
  • Brown cat: 25, 棕猫:25,
  • Anything else: 30; 其他:30;

So the result price for brown, 1yold, dog, female is 10. 因此,棕色,1岁,狗,雌性的结果价格是10。

For black, 2yold, cat, male it's 20. etc. 黑色,2岁,猫,男性为2​​0,依此类推。

Are there any structures for that? 是否有任何结构? If not, then what's the best way to implement one. 如果没有,那么实现它的最佳方法是什么。

In my first attempt to answer this I missed the requirement that prices can be assigned to combinations of attributes. 在我第一次回答这个问题时,我错过了可以将价格分配给属性组合的要求。 This answer should handle that part nicely. 这个答案应该很好地处理了这一部分。

Model each attribute in a way so that no two attributes have the same value. 以某种方式对每个属性建模,以使两个属性都不具有相同的值。 So for example if you have the dimensions eye color and fur color, don't just use Strings, because brown might belong to either of them. 因此,例如,如果您具有眼睛颜色和皮毛颜色的尺寸,则不要仅使用“字符串”,因为棕色可能属于它们之一。 I assume the have a common interface 'Attribute' of no such interface exists you might use 'Object' instead. 我假设没有这样的接口,但有一个通用接口“属性”,您可以改用“对象”。 All such classes need to properly implement 'equals' 所有这些类都需要正确实现“等于”

Now create a class to model combinations of attributes plus their price: (the following is written without IDE or compiler. Errors are to be considered intentional training for the reader) 现在创建一个类来对属性及其价格组合进行建模:(以下内容是在没有IDE或编译器的情况下编写的。错误应视为对读者的有意训练)

public class AttributeCombination{
    private final Set<Attribute> attributes;

    public AttributeCombination(Set<Attribute> attributes){
        this.attributes = attributes;
    }

    public boolean matches(Set<Attribute> attributes){
        for(a : this.attributes){
            if (!attributes.contains(a))
                return false;
        }
        return true;
    }
}

Now have a properly ordered class combining Attributes with Prices 现在有一个正确排序的类,将属性和价格相结合

public class AttributeWithPrice implements Comparable<AttributeWithPrice> {
    private final AttributeCombination attributes;
    private final int price;

    // add the obvious constructor or switch to scala

    public int compareTo(AttributeWithPrice other){
        return this.price - other.price // or the other way round, I can never remember
    }
}

Now you can create a List of AttributeWithPrice objects, sort it, so the small prices come first, and iterate through it, until you find a matching element. 现在,您可以创建AttributeWithPrice对象的列表,对它进行排序,这样小价格首先出现,然后遍历它,直到找到匹配的元素。

Just use a Map and a different class (possibly enum) for each "dimension". 只需为每个“维度”使用一个Map和一个不同的类(可能是枚举)。

The dimensions become the key for the map (key type must be the common super type, so probably Object) and the value type will be price (int judging from your examples). 尺寸成为地图的键(键类型必须是通用的超类型,因此可能是对象),而值类型将是价格(根据示例判断)。

To determine the price you pick the price based on each dimension and use the minimum of it. 要确定价格,您可以根据每个维度选择价格并使用最小值。 Done. 完成。

I don't think you can use just a Map or small set of Maps for this, since you want to define restrictions on combinations of multiple criteria. 我认为您不能为此使用一个地图或一小套地图,因为您想定义多个条件组合的限制。 I don't see a way of determining an appropriate key to use, because you're not defining restrictions in terms of points in a multidimensional space, but in terms of possibly-overlapping spaces of various shapes. 我没有确定使用适当键的方法,因为您不是在多维空间中的点方面定义限制,而是在各种形状的可能重叠的空间方面定义限制。 Your ANY is not functionally a distinct point on that dimension, but a range that covers all the other defined points, so you can't do simple equality comparisons for it, which is what Map keys need. 从功能ANY并不是该维度上的唯一点,而是覆盖所有其他定义点的范围,因此您无法对其进行简单的相等比较,这就是Map键所需要的。

I would just keep all the restrictions in a List and check them all against your pet objects that you want to find the minimum price for. 我只是将所有限制都保留在一个List并对照您要查找最低价格的宠物物品检查所有这些限制。 Make a Restriction class and give it an appliesTo(Pet pet) method. 制作一个Restriction类,并给它一个appliesTo(Pet pet)方法。

If you ended up with a large set of restrictions, you could use Maps to build "indexes" on them if you introduced the notion of a "wildcard" to each of the criteria. 如果最终遇到了一大堆限制,则可以在每个条件中引入“通配符”的概念时,使用Google Maps在其上建立“索引”。 You could then build map each of the values of each criteria, including a "wildcard" for all the conditions that could apply to pets with any value for that criteria, to a list of all the minimum price conditions that could possibly apply to that. 然后,您可以将每个条件的每个值(包括可能适用于具有该条件的任何值的宠物的所有条件的“通配符”)映射到可能适用于该条件的所有最低价格条件的列表。 This would be a lot more code, though, and probably wouldn't give you much speedup unless most of your restrict 但是,这将需要更多代码,除非您有大部分限制,否则可能不会给您太多加速

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

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