Win a copy of Java Challengers this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

How to add List in builder pattern

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, Am very new to java and learning slowly. I am building JSON payload to send request to url is using Pojo classes.

My final expectation of request JSON payload is :
{
"Name": "Dallas"
"Status": true
"HouseType" : ["Apartment"]
}

My Pojo Classes with annotations is:
1st Pojo:
pub class example {
private String name;
private boolean Status;
private List<ABC> abc;
}

2nd Pojo class:
public class ABC {
private String number,
private Boolean Status;
private String[] methods;
}

Code:
Example ex = Example.builder()
.name("Java")
.Status(true)
.abc() --- Here am not able to make it.  Basically i am not able to build the payload for HouseType as it is in List.

Please help me to understand the how can i build the payload to get HouseType in List.

Thanks,
 
Rancher
Posts: 3850
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Somith Patel wrote:Please help me to understand the how can i build the payload to get HouseType in List.



There are a number of ways to do this, and there's not really a single standard way that everyone has settled on.

The List interface has some static methods that can help you - look at the List.of() methods.  You can use these to write something like this:

Note that I've renamed abc() to abcs(), because I like using a plural form for lists and arrays.
Here your abcs() method accepts a List<ABC>, and you use List.of() to make the List of ABC objects.

If you've learned about varargs, you can also use that to write an abc() method that accepts a comma-separated list (not List) of ABC objects:
code]Example ex = Example.builder()
   .name("Java")
   .Status(true)
   .abcs(
       ABC.builder().number("123").status(true).methods("no","idea").build(),
       ABC.builder().number("456").status(true).methods("what","these","mean").build(),
       ABC.builder().number("789").status(false).methods("could","be","anything").build()
   )
   .build();[/code]
It's a small difference, but a little easier to use, and more readable.

Personally, for Lists and other collections, I often find it helpful to also have an add() method that lets you add an individual element, like this:


Note that this may not work well with the first approach - a List.of() method creates an unmodifiable list, so if you then do a list.add(), it won't work.  So you may need to do additional work to copy the original List to a separate ArrayList, created by you in the builder class, that you know is mutable.

If those options are too confusing at beginner level, don't worry - just focus on the first one; that's simplest to understand.  The others can be considered later.
   
 
Die Fledermaus does not fear such a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic