-2

Maybe this is a very silly question but I'm banging my head to 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 tried my best but no luck.. can someone help how can i get the plain string from the above list. I'm very new to Java, any help would be really appreciated.

learn groovy
  • 417
  • 2
  • 9
  • 24

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);
Deadpool
  • 33,221
  • 11
  • 51
  • 91