[Interest] QMultiHash / QMultiMap: insert QList?

Giuseppe D'Angelo giuseppe.dangelo at kdab.com
Thu Oct 8 13:08:01 CEST 2026


Hi,

Il 08/10/26 09:24, Schimkowitsch Robert ha scritto:
> Hi there,
> 
> I saw this data structure:
> 
> QHash<SomeKey, QList<SomeValue>>
> 
> …and I was wondering: Why not use QMultiHash?
> 
> The rationale was that it’s easier to extract and re-insert whole blocks 
> (i.e. all values sharing the same key), which is indeed a relevant use 
> case for the surrounding code.
> 
> There IS QHash::values(key) -> QList<value>, but what’s missing is the 
> opposite function: insert(key, QList<value>)

Do you mean QMultiHash::values(key)? And QMultiHash::insert(key, 
QList<values>)?

The main reason why this doesn't exist as you describe it is that a 
QMultiHash<K, V> isn't implemented by mapping keys to QList<V> objects.

QMultiHash::values(key) is linear: it will *build* the list iteratively; 
it's not amortized constant time or somesuch (something like "find the 
list corresponding to this key and return it", which is what QHash<key, 
QList<values>> gives you).

What might make sense would be something QMultiHash::insert(key k, 
iterator b, iterator e), that inserts the [b,e) range of values for the 
key k, avoiding e-b key lookups / hashing.

> 
> Any special reason for this omission? Did I overlook a direct way to 
> insert a QList of multiple values with the same key with a single 
> operation, that is (almost) as efficient as adding a whole new list to 
> the QHash?

Again, if that's your use case, having a QHash<key, QList<value>> may 
generally be much more efficient anyhow.


My 2 c,
-- 
Giuseppe D'Angelo | giuseppe.dangelo at kdab.com | Senior Software Engineer
KDAB (France) S.A.S., a KDAB Group company
Tel. France +33 (0)4 90 84 08 53, http://www.kdab.com
KDAB - Trusted Software Excellence
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4889 bytes
Desc: Firma crittografica S/MIME
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20261008/59a8b085/attachment.bin>


More information about the Interest mailing list