-2

How do I get the string value from below snippet:

List<Map<String, Object>> list1= (List<Map<String, Object>>) mymap.get("getProduct");
Map<String, Object> myList= list1.get(0);
List<String> myproduct=  (List<String>) myList.get("productName");
System.out.println("value " +myproduct.toString());

When I try to print this I'm always getting as an object instead of plain string. My Output:

[Apple]

Expected output:

Apple

I'm very new to Java, any help would be really appreciated.

Salvatore
  • 10,815
  • 4
  • 31
  • 69
learn groovy
  • 487
  • 4
  • 11
  • 28

1 Answers1

1

Because myproduct is List with string values, for example below is list with fruit names

List<String> fruits = new ArrayList<>();
  list.add("apple");
  list.add("mango");
System.out.println(fruits);   //[apple,mango]

If you want each value from list you can simply iterate using for each loop

for(String str : myproduct) {
    System.out.println(str);
   }

If you just need first element you can get by index, but might end up with index out of bound exception if list is empty

String ele = myproduct.get(0);

or by using java-8 stream

String ele = myproduct.stream().findFirst().orElse(null);
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98