简体   繁体   English

如何按Java映射的键的一部分分组?

[英]How do I group by a part of a key of a Java map?

How do I group by a part of a key of a Java map? 如何按Java映射的键的一部分分组?

For example: my keys are 12A, 12B, 12C, 13A, 13B, 13D and I want to group starting with the prefix number and then with characters in the hashmap as 12(A,B,C) 13(A,B,D) and after again add a new object to the hash map. 例如:我的键是12A,12B,12C,13A,13B,13D,我想从前缀编号开始分组,然后在哈希图中将字符分组为12(A,B,C)13(A,B,D ),然后再次将新对象添加到哈希图中。

My object type: Map <String, BookingClass> 我的对象类型: Map <String, BookingClass>

Object key eg: '12A' 对象密钥,例如:“ 12A”

242G=BookingClass [classCode=G, seatsAvailable=, 242G = BookingClass [classCode = G,seatAvailable =,
242E=BookingClass [classCode=E, seatsAvailable=, 242E = BookingClass [classCode = E,seatAvailable =,
121D=BookingClass [classCode=D, seatsAvailable=, 121D = BookingClass [classCode = D,seatAvailable =,
121C=BookingClass [classCode=C, seatsAvailable=, 121C = BookingClass [classCode = C,seatAvailable =,
242B=BookingClass [classCode=B, seatsAvailable=, 242B = BookingClass [classCode = B,seatAvailable =,
242A=BookingClass [classCode=A, seatsAvailable=, 242A = BookingClass [classCode = A,seatAvailable =,
242O=BookingClass 242O = BookingClass

You have two possibilities. 您有两种可能性。

First, you can store your data as a map of maps. 首先,您可以将数据存储为地图地图。 For example: 例如:

Map<String, Map<String,BookingClass>>

Second, you can use a TreeMap . 其次,您可以使用TreeMap This type of map is sorted and you can find keys in a given range. 此类地图已排序,您可以找到给定范围内的键。 So assuming that the second part of your key is an upper-case letter and that 'A' is the minimum and 'Z' is the maximum, then you can find the range from, for example, 12A to 12Z or anything in betweeen. 因此,假设密钥的第二部分是大写字母,并且“ A”是最小字母,“ Z”是最大字母,那么您可以找到从12A到12Z的范围或其他任何范围。

The methods floorKey and ceilingKey can find the first and last keys. floorKeyceilingKey方法可以找到第一个和最后一个键。

The method: 方法:

public NavigableMap<K,V> subMap(K fromKey,
                       boolean fromInclusive,
                       K toKey,
                       boolean toInclusive)

can return you a submap between two keys which you can then navigate over. 可以在两个键之间返回一个子图,然后可以在其上进行导航。

Please see theAPI documenation for more details: 请参阅API文档以获取更多详细信息:

https://docs.oracle.com/javase/7/docs/api/java/util/TreeMap.html https://docs.oracle.com/javase/7/docs/api/java/util/TreeMap.html

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

相关问题 Java 8组映射按键 - Java 8 group map by key 如何在Java中搜索与谓词匹配的键的映射? - How do I search a map in Java for a key that matches a predicate? 我如何在Hibernate中映射多对一关联,其中孩子有一个组合键,其中一部分是父级的主键? - How do I map a many-to-one association in Hibernate where the child has a composite key and a part of that is the primary key in the parent? Java.. 给定公共 class 并且没有 Main 方法作为组项目的一部分.. 我该如何编译? - Java .. Given public class and no Main method as part of a group project .. how do I compile? Java-如何将新值添加到哈希映射中的现有键。 1个键,多个值 - Java - How do I add a new value to an existing key in a Hash Map. 1 key, multiple values java - 如何根据带有Java流的键将地图的值分组到列表中? - How to group values of map into lists based on key with java streams? 如何从列表中的 Map 获取密钥并在 Java 中执行获取 rest 请求 - How do I get the key from Map in a List and do a get rest request in Java 如何截断Java中字符串的一部分? - How do I truncate a part of the string in Java? 我如何映射这个复合主键? - How do I map this composite primary key? 在MongoDB Java驱动程序中,如何将键函数(&#39;keyf&#39;)传递给&#39;group&#39;命令 - In the MongoDB java driver, how do I pass a key function ('keyf') to a 'group' command
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM