简体   繁体   English

如何将具有 object 作为密钥的 Map 列表转换为 Map?

[英]How to convert a List of Map that has object as key to Map?

I have a repository query that is returning a list of Maps as follows:我有一个存储库查询,它返回一个地图列表,如下所示:

@Query("SELECT new map(key(m), value(m) ) from User u join u.shopRoleMap m where u.usEmail = :email")
     List<Map<Shop,Role>> findByUserEmail(String email);

The Mapping on User is as follows:用户映射如下:

@ManyToMany(cascade = {CascadeType.MERGE, CascadeType.PERSIST})
    @JoinTable(name = "user_shop_role",
            joinColumns = @JoinColumn(name = "user_fk"), inverseJoinColumns = @JoinColumn(name = "role_fk"))
    @MapKeyJoinColumn(name = "shop_fk")
    private Map<Shop, Role> shopRoleMap = new HashMap<>();

On my service I need to get the map as Map<Shop,Role> so that I can get the keys and values correspondingly.在我的服务中,我需要将 map 作为Map<Shop,Role>以便我可以相应地获取键和值。 The code is as follows:代码如下:

List<Map<Shop,Role>> shopRole= userRepository.findByUserEmail(email);

        for (Map<Shop,Role> map : shopRole){
            for (Map.Entry<Shop,Role> result : map.entrySet()){
                System.out.println("The key is: "+result.getKey());
                System.out.println("And the value is: "+result.getValue());
            }
        }
        }

The result that I get is somewhat bizarre, as I expected result.getKey() would give me Shop as the key.我得到的结果有点奇怪,因为我预计result.getKey()会给我Shop作为键。 However, I'm getting keys as 0's and 1's.但是,我得到的键是 0 和 1。

The key is: 0
And the value is: Shop{id=54, sh_name='Frefdv', sh_icon='icon url', sh_description='Fashion Shop', sh_tag='metadata', uuid='99dba3d5-dfaa-446d-9649-b9b98f422f87', sh_enabled='N', created_at=2020-07-30T15:54:10, updated_at=2020-07-30T15:54:10, created_by='e0b009ef-27c2-405b-961a-86f199b15167', updated_by='e0b009ef-27c2-405b-961a-86f199b15167'}
The key is: 1
And the value is: Role{id=0, roleName='ADMIN'}
The key is: 0
And the value is: Shop{id=55, sh_name='fnhfh', sh_icon='icon url', sh_description='Fashion Shop', sh_tag='metadata', uuid='e3ccdbdf-aad2-43ba-8331-91b2c2c01853', sh_enabled='N', created_at=2020-07-30T15:54:23, updated_at=2020-07-30T15:54:23, created_by='e0b009ef-27c2-405b-961a-86f199b15167', updated_by='e0b009ef-27c2-405b-961a-86f199b15167'}
The key is: 1
And the value is: Role{id=0, roleName='ADMIN'}

How can I convert this List<Map<Shop,Role>> to a map where I can get key value pairs?如何将此List<Map<Shop,Role>>转换为 map 以获取键值对?

The HQL in question is actually returning List<Map<String, Object>>有问题的 HQL 实际上是返回List<Map<String, Object>>

SELECT new map(key(m), value(m) ) from User u join u.shopRoleMap m where u.usEmail = :email

The syntax for new map basically expects the Values.新 map的语法基本上需要这些值。 So when key(m) and value(m) is passed to it, then the key(m) as well as value(m) are treated as Map values.因此,当将 key(m) 和 value(m) 传递给它时, key(m) 和 value(m) 将被视为 Map 值。 The key to the map is basically the index value (0,1,2,..) map的关键基本上是索引值(0,1,2,..)

You can read more about it in the Hibernate Documentation - https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#hql-select-clause You can read more about it in the Hibernate Documentation - https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#hql-select-clause

在此处输入图像描述

So, you should be replacing your HQL to因此,您应该将 HQL 替换为

SELECT entry(m) from User u join u.shopRoleMap m where u.usEmail = :email

This should return List<Map.Entry<Shop,Role>> which can be iterated to obtain the correct results.这应该返回List<Map.Entry<Shop,Role>>可以迭代以获得正确的结果。

转换列表<object>映射<div id="text_translate"><p>我有一个包含四个字段的Employee类:id、name、department 和 email。 我创建了五个对象,设置了值并将它们全部添加到列表中。</p><p> 现在,我想将该列表转换为地图,但我做不到。 供您参考,这是我尝试过的代码</p><pre>Employee emp1 = new Employee("1", "Mayank", "HR", "mayank@gmail.com"); Employee emp2 = new Employee("2", "Mahesh", "Trainer", "Mahesh@gmail.com"); Employee emp3 = new Employee("3", "Vipul", "SEO", "Vipul@gmail.com"); Employee emp4 = new Employee("4", "Ajay", "Devlopwr", "Ajay@gmail.com"); Employee emp5 = new Employee("5", "Rakesh", "Marketing", "Rakesh@gmail.com"); //add class object to list List &lt; Employee &gt; listWmp = new ArrayList &lt; &gt; (); listWmp.add(0, emp1); listWmp.add(1, emp2); listWmp.add(2, emp3); listWmp.add(3, emp4); listWmp.add(4, emp5); System.out.println("list elements are: " + listWmp); //convert the list into map Map &lt; String, Employee &gt; listMap = new HashMap &lt; &gt; (); for (Employee employee: listWmp) { listMap.put(employee.getEmpId(), employee.getEmpBame()); }</pre><p> 如何将列表转换为地图?</p></div></object> - convert List<object> to map

暂无
暂无

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

相关问题 如何将 List 转换为 Map 并使 map 成为对象属性的键 - How to convert List to Map and make the map key the object's attributes 将值列表转换为地图<key, List<Object> &gt; - Convert a list of values to a map<key, List<Object>> 转换列表<object>映射<div id="text_translate"><p>我有一个包含四个字段的Employee类:id、name、department 和 email。 我创建了五个对象,设置了值并将它们全部添加到列表中。</p><p> 现在,我想将该列表转换为地图,但我做不到。 供您参考,这是我尝试过的代码</p><pre>Employee emp1 = new Employee("1", "Mayank", "HR", "mayank@gmail.com"); Employee emp2 = new Employee("2", "Mahesh", "Trainer", "Mahesh@gmail.com"); Employee emp3 = new Employee("3", "Vipul", "SEO", "Vipul@gmail.com"); Employee emp4 = new Employee("4", "Ajay", "Devlopwr", "Ajay@gmail.com"); Employee emp5 = new Employee("5", "Rakesh", "Marketing", "Rakesh@gmail.com"); //add class object to list List &lt; Employee &gt; listWmp = new ArrayList &lt; &gt; (); listWmp.add(0, emp1); listWmp.add(1, emp2); listWmp.add(2, emp3); listWmp.add(3, emp4); listWmp.add(4, emp5); System.out.println("list elements are: " + listWmp); //convert the list into map Map &lt; String, Employee &gt; listMap = new HashMap &lt; &gt; (); for (Employee employee: listWmp) { listMap.put(employee.getEmpId(), employee.getEmpBame()); }</pre><p> 如何将列表转换为地图?</p></div></object> - convert List<object> to map 如何转换列表<map<string, object> > 列出<map<string, string> > </map<string,></map<string,> - How to Convert List<Map<String, Object>> to List<Map<String, String>> 如何将列表转换为 Map 的 Map - How to convert list to Map of Map 转换列表<Map>列出<Map<String,Object> &gt; - Convert List<Map> to List<Map<String,Object>> 如何将Map转换为用户对象列表? - How to convert Map to List of User Object? 如何将字符串和对象的映射转换为字符串和列表的映射 - How can i convert Map of Strings and Object to a Map of Strings and List 如何转换列表<map<string,object> > 至 Map<string, string> ? </string,></map<string,object> - How to convert List<Map<String,Object>> to Map<String, String>? 如何将地图转换为对象 - How to Convert Map to Object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM