0

I have 2 pieces of codes, one written with the java old style and the other is written with lambda expression,

The first:

        Class enumClass = getConstantsEnum();
        Object[] enumConstants  = enumClass.getEnumConstants();
        
        for(Object o:enumConstants) {
            try {
                constantsMap.put(o.toString(),o.getClass().getDeclaredField("lookupValue").getInt(o));
            } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException
                    | SecurityException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }

Is working fine and filling the map correctly

The other:

        Class enumClass = getConstantsEnum();
        Object[] enumConstants  = enumClass.getEnumConstants();
        constantsMap.putAll(Arrays.asList(enumConstants)
                .stream().collect(Collectors.toMap
                        (e -> e.toString(), e -> {
                            try {
                                return e.getClass().getDeclaredField("lookupValue").getInt(e);
                            } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException
                                    | SecurityException e1) {
                                // TODO Auto-generated catch block
                                e1.printStackTrace();
                            }finally {
                                return null;
                            }
                        }
                        )));

Is giving null pointer exception, though by debugging the line e.getClass().getDeclaredField("lookupValue").getInt(e) I see it gets filled by value

My questions are: 1-what is wrong with this code?

2- how can I debug effectively inside stream methods in eclipse, is there a way to see elements getting populated inside the map one by one as in normal loop ? I could see the first element by putting a breakpoint but then the exceptions is fired immediately

3- is there a cleaner way to handle the exception inside the lambda expression, I tried wrapping the whole code with try/catch but compiler is still complaining that the exception is not handled.

Answer to any question is appreciated :)

EDIT:

Q1 answered by Holger in comments

IDE added

osama yaccoub
  • 1,884
  • 2
  • 17
  • 47

1 Answers1

0
  1. As stated in comment posted by Holger in comment, you can see that there is known bug in Java 8, with unresolved status, because of which you cannot put null values using Collectors.toMap, see issue here: https://bugs.openjdk.java.net/browse/JDK-8148463

  2. Intellij IDEA has a great support for debugging Java 8 streams. Check this article: https://www.baeldung.com/intellij-debugging-java-streams

  3. No, there currrently isn't, or I don't know about it. My suggestion for improving your code is to extract a method where you can handle your exception:

       constantsMap.putAll(Arrays.asList(enumConstants)
                                    .stream().collect(
                                         Collectors.toMap(e -> e.toString(), 
                                         this::getInteger
                                     )));
        }
    
        private Integer getInteger(Object e) {
    
            try {
                return e.getClass().getDeclaredField("lookupValue").getInt(e);
            } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException
                    | SecurityException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } finally {
                return null;
            }
        }
Ionut S.
  • 21
  • 3