We can use above logic to implement custom logic to generate any object. Let’s consider a case where we are building a PubSub message publisher client and we want to mimic generating infinite sequence of messages that client need to publish.
We can easily extend the stream generator logic to build that . Below is our Message POJO that takes message ID and message itself.
Below we have defined our supplier that returns Message object. We again used Stream generator that generates the infinite sequence of Message object and Print it.
static class Message{
int id;
String message;
public Message(int id, String message) {
this.id = id;
this.message = message;
}
@Override
public String toString() {
return "Message{" +
"id=" + id +
", message='" + message + '\'' +
'}';
}
}
Generating Mock emailID data
In below example we will use stream generator to generate mock emailID data.
We have created list of email domains that we would like to see in our emailId.
The we build Supplier instance that first generate random userName and then concatenate with random domain that is picked from our domain list.
Once we have supplier instance ready we can call this method and provide size as number of emailId that we need.
private static String generateUserName() {
String chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
Random random = new Random();
StringBuilder userName = new StringBuilder();
for(int i=0;i<10;i++){
userName.append(chars.charAt(random.nextInt(chars.length())));
}
return userName.toString();
}
Below is the code to generate random username. Logic is to just pick random characters and append for the defined number of length for userName.
private static void streamMailId(long size) {
List<String> domain = List.of("@gmail.com", "@Yahoo.com", "@outlook.com");
Supplier<String> supplier = () ->{
String userName = generateUserName();
Random random = new Random();
int index = random.nextInt(domain.size());
return userName+domain.get(index);
};
Stream.generate(supplier).limit(size).forEach(a-> System.out.println(a));
}
Once we execute our client code we will see below output.
So it’s very easy to generate mock data with the help of generate feature and adding custom supplier logic.
Conclusion
In this article we used Streams api generate() method to generate infinite sequence of data.
We looked into custom mock data implementation such as emailId and message type.
Bonus Tip
If you want to upskill your Java, you should definitely check out this bestseller course
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
Cookie
Duration
Description
cookielawinfo-checkbox-analytics
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional
11 months
The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy
11 months
The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.