有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

hashmap如何在java中对嵌套映射中的多个条目使用单个键

我有一个问题,使用嵌套贴图可能不是理想的解决方案,但需要一些反馈

这是存在的泛型结构,其中水果、蔬菜、玩具是映射中的键,而另一个映射又与之关联,其中新鲜、不新鲜、腐烂是映射“b”的键

       private Map<String, Commodity> Comm = new HashMap<String, Commodity>();
       for (Map.Entry<String, Map<String, Object>> e : this.entrySet()) {
          Comm.put(e.getKey(), getCommodity(e.getValue(), e.getKey()));
        }

        private Commodity getCommodity(Map<String, Object> m, String section) {
           return new Commodity(m, section);
        }


  Fruit=> Fresh=>object
          notFresh=>object
          rotten=>object

  Vegetable=> fresh=> object
             notfresh=>object
             rotten=>object
  Toys=> fresh=>object
        notfresh=>object
        rotten=>object

但是,我需要介绍另一个关键点,称为“季节”,其中,如果提到商品的季节,则商品属于季节,否则如前所述保持不变

   i.e 
  Fruit=> winter =>Fresh=>object
                  notFresh=>object
                  rotten=>object

  Vegetable=> fresh=> object
              notfresh=>object
              rotten=>object

  Toys=> summer => fresh=>object
                   notfresh=>object
                   rotten=>object 

所以在这种情况下,水果和玩具有一个季节,在这种情况下,它需要在冬季寻找水果,在夏季寻找玩具。如何在java中实现这种复杂的映射结构


共 (3) 个答案

  1. # 1 楼答案

    你是对的,它“可能不是理想的解决方案”。地图是保存一个键和一个值的机制。一个简单的映射,如果你想的话。它们并不像你试图做的那样,用来构建复杂的数据结构。正如你自己看到的,它变得非常复杂和奇怪。所以不要这样做

    改为自己上课。类是生成数据结构的Java方法。首先,绘制数据外观的草图:

    • 您有一个包含所有产品的类(我们称之为shop
    • 这家商店有不同的种类(玩具、蔬菜)
    • 每个类别都有一个或多个季节
    • 你有四季(可以存放产品)
    • 你有你的实际产品(例如,可能是腐烂的苹果)

    如果我错了,请纠正我,但我就是这样理解你的问题的。如果我错了,你可以自己纠正细节

    现在我们有5个要点,我们可以把它们分成5类我在做这件事时,没有让事情变得简单的能手和二传手:

    class Shop {
        List<Category> categories;
    }
    
    class Category {    
        String name;
        List<Season> seasons;
    }
    
    class Season {
        String name;
        List<Products> products;
    }
    
    class Product {
        String name;
        boolean isRotten;
    }
    

    现在,你可以用它来把你的商店放在一起:

    Shop myShop = new Shop();
    
    Category toys = new Category();
    Category fruits = new Category();
    
    myShop.categories.add(toys);
    myShop.categories.add(fruits);
    
    Season summer = new Season();
    
    fruits.seasons.add(summer);
    
    Product apple = new Product();
    Product pineapple = new Product();
    Product banana = new Product();
    
    summer.products.add(apple);
    summer.products.add(pineapple);
    summer.products.add(banana);
    
    banana.isRotten = true;
    

    注:

    • 您可以使用getter和setter方法来实现这一点
    • 因为只有四个季节,所以要创建一些静态对象来表示它们
    • 有很多方法可以做到这一点,这只是一个快速的方法,可以为你指明方向
  2. # 2 楼答案

    正如托马斯所说,这不是理想的解决方案。然而,为了测试它,你可以这样做:

    我想我正确地理解了结构。如果没有,请建议

     Map<String, Map<String, Map<String,Commodity>>> root = new HashMap<String, Map<String,Map<String,Commodity>>>();
         Map<String, Commodity> leaf = new HashMap<String, Commodity>();
         leaf.put("fresh", new Commodity("name1", "type1"));
         leaf.put("notFresh", new Commodity("name2", "type2"));
         leaf.put("rotten", new Commodity("name3", "type3"));
         Map<String, Map<String,Commodity>> mid = new HashMap<String, Map<String,Commodity>>();
         mid.put("summer", leaf);
         mid.put("winter", leaf);
         root.put("vegetable", mid);
         root.put("fruit", mid);
    
        for (Entry<String, Map<String, Map<String, Commodity>>> middle: root.entrySet()) {
             Map<String, Map<String,Commodity>> midVal= middle.getValue();
             System.out.println("Type: "+ middle.getKey());
             for (Entry<String, Map<String, Commodity>> leaves : midVal.entrySet()) {
                 Map<String,Commodity> leafVal = leaves.getValue();
                 System.out.println("Season: " + leaves.getKey());
                 for (Entry<String, Commodity> leafValNames : leafVal.entrySet()) {
                    System.out.println("key: "+ leafValNames.getKey() + "value: " + leafValNames.getValue());
                }
            }
        }
    
    }
    

    这个输出

    Type: fruit
    Season: winter
    key: rottenvalue: test.Commodity@8def646
    key: freshvalue: test.Commodity@8def63c
    key: notFreshvalue: test.Commodity@8def641
    Season: summer
    key: rottenvalue: test.Commodity@8def646
    key: freshvalue: test.Commodity@8def63c
    key: notFreshvalue: test.Commodity@8def641
    Type: vegetable
    Season: winter
    key: rottenvalue: test.Commodity@8def646
    key: freshvalue: test.Commodity@8def63c
    key: notFreshvalue: test.Commodity@8def641
    Season: summer
    key: rottenvalue: test.Commodity@8def646
    key: freshvalue: test.Commodity@8def63c
    key: notFreshvalue: test.Commodity@8def641
    

    这张地图看起来像:

    root = 
    {vegetable={winter={rotten=test.Commodity@8df1515, fresh=test.Commodity@8df150b, notFresh=test.Commodity@8df1510}, summer={rotten=test.Commodity@8df1515, fresh=test.Commodity@8df150b, notFresh=test.Commodity@8df1510}}}
    
    mid = 
    {winter={rotten=test.Commodity@8df1515, fresh=test.Commodity@8df150b, notFresh=test.Commodity@8df1510}, summer={rotten=test.Commodity@8df1515, fresh=test.Commodity@8df150b, notFresh=test.Commodity@8df1510}}
    
    leaf = 
    {rotten=test.Commodity@8df1515, fresh=test.Commodity@8df150b, notFresh=test.Commodity@8df1510}
    
  3. # 3 楼答案

    也许你可以用一个物体来表达地图的关键点。java HashMap使用对象的hashCode进行快速比较——当键发生冲突时,它会回退到equals(@see HashMap.getEntry(object key))

    class Key {
    
       Item item; // fruit, toys or vegetable
       Season season; // an enum for the season
    
       // Constructor
    
       public int hashCode()
       {
           int hash = 1;
           hash *= 31 + item.hashCode();
           hash *= 31 + season.hashCode();
    
           return hash;
       }
    
       public boolean equals(Object obj)
       {
           return false; // implementing the equals too
       }
    }
    
    Map<Key, Commodity> store = new HashMap<>();
    store.put(
       new Key(new Item("Fruit"), Season.FRESH),
       new Commodity("asd")
    );
    store.put(
       new Key(new Item("Fruit"), Season.NOT_FRESH),
       new Commodity("qwe")
    );
    store.put(
       new Key(new Item("Vegetable"), Season.ROTTEN),
       new Commodity("zxc")
    );