0

I've some server response (a long one) which I've converted to POJO (by using moshi library).

Eventually I have list of "Items" , each "Item" looks like follow :

public class Item
{
    private String aa;
    private String b;
    private String abc;
    private String ad;
    private String dd;
    private String qw;
    private String arew;
    private String tt;
    private String asd;
    private String aut;
    private String id;
    ...
}

What I actually need, is to pull all properties which start with "a" , and then I need to use their values for further req ...

Any way to achieve it without Reflection ? (usage of streams maybe ?)

Thanks

Igal
  • 4,603
  • 14
  • 41
  • 66
  • 3
    You need either annotations or reflection of SOME sort (Could be hidden under some other tool though). Since all your values are Strings, is there any reason not to just forget about the pojo and drop them into a HashMap? Then they can be easily manipulated. Reflection is the ability to "Reflect" upon yourself and see your variable names and such--so by definition you won't be able to deal with variable names without it. – Bill K Apr 27 '18 at 20:41
  • Thanks , will try to work with JSONObject / Gson direction – Igal Apr 27 '18 at 22:24

2 Answers2

1

With guava-functions tranformation you might transform your items with somethng following:

 public static void main(String[] args) {
        List<Item> items //
        Function<Item, Map<String, Object>> transformer = new Function<Item, Map<String, Object>>() {
            @Override
            public  Map<String, Object> apply(Item input) {
                  Map<String, Object> result  = new HashMap<String, Object>();
            for (Field f : input.getClass().getDeclaredFields()) {
                if(! f.getName().startsWith("a")) {
                    continue;
                }
                Object value = null;
                try {
                    value = f.get(input);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException("failed to cast" + e)
                }
                result.put(f.getName(), value);
               }

            return result
        };
        Collection<Map<String, Object> result
                = Collections2.transform(items, transformer);
    }
Adrian
  • 1,973
  • 1
  • 15
  • 28
0

Sounds like you may want to perform your filtering on a regular Java map structure.

// Dependencies.
Moshi moshi = new Moshi.Builder().build();
JsonAdapter<Map<String, String>> itemAdapter =
    moshi.adapter(Types.newParameterizedType(Map.class, String.class, String.class));
String json = "{\"aa\":\"value1\",\"b\":\"value2\",\"abc\":\"value3\"}";

// Usage.
Map<String, String> value = itemAdapter.fromJson(json);
Map<String, String> filtered = value.entrySet().stream().filter(
    stringStringEntry -> stringStringEntry.getKey().charAt(0) == 'a')
    .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

You could wrap up the filtering logic in a custom JsonAdapter, but validation and business logic tends to be nice to leave to the application usage layer.

Eric Cochran
  • 8,414
  • 5
  • 50
  • 91