Как конвертировать Java.unil.Enumeration<> в Map<>?

Есть

Enumeration<Object> propNames = message.getPropertyNames();

В ней, если смотреть через отладчик уже есть HashMap: введите сюда описание изображения

но извлечь эту HashMap не получается, хотя вот она вроде уже готовая есть. Пробовал преобразовывать

HashMap<String, String> map = (HashMap<String, String>) propNames;
  • не получилось

Пробовал стримами:

HashMap<String, String> properties = new HashMap<>();
        Enumeration<HashMap<Object, Object>> propNames = null;
        try {
            propNames = message.getPropertyNames();
        } catch (JMSException e) {
            throw new RuntimeException(e);
        }

        Collections.list(propNames).stream()
                .forEach(prop -> {
                    Set<Map.Entry<Object, Object>> entries = prop.entrySet();
                    entries.forEach(entry -> properties.put(entry.getKey().toString(), entry.getValue().toString()));
                });

Попытался:

HashMap<String, String> properties = new HashMap<>();
    Enumeration<HashMap<Object, Object>> propertiesEnumeration = null;
    try {
        propertiesEnumeration = message.getPropertyNames();
    } catch (JMSException e) {
        throw new RuntimeException(e);
    }

            while (propertiesEnumeration.hasMoreElements()){
               HashMap<Object, Object> prop = propertiesEnumeration.nextElement();
                Set<Map.Entry<Object, Object>> entries = prop.entrySet();
                entries.forEach(entry -> properties.put(entry.getKey().toString(), entry.getValue().toString()));
            }
    return properties;

java.lang.ClassCastException: java.lang.String cannot be cast to java.util.HashMap

Как это можно сделать и можно ли?


Ответы (0 шт):