JAVA: Initialize Object Class [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I'm new learner in programming, and i have a logic problem.
I would like to initialize a object class to settle it.
I have 2 Entities object Class:
Entity1:
private Date date;
private List<Entity2> entity2;
.... Getters and Setters ....
Entity2:
private String description;
.... Getters and Setters ....
Now I've init the entities
Entity1 entity1 = new Entity1();
List<Entity2> Entity2 = new ArrayList<Entity2>();
entity1.setEntity2(entity2);
I have no error message by doing this, in debugging mode I see in the entity1 and the ArrayList of entity2, but empty. I see no object inside, just this [ ] and I would like to see "description" object (which will be normal), like this [description = null];
Someone can explain to me what I do wrong ?
Thanks so much for your help

You're really close. Let's consider what you've got thus far.
Entity1 entity1 = new Entity1();
You've created yourself an instance of Entity1. So far, this looks like this:
{
Date: null,
entity2: null
}
Then you've issued the following commands:
List<Entity2> Entity2 = new ArrayList<Entity2>();
entity1.setEntity2(entity2);
So now your entity1 variable looks like this:
{
Date: null,
entity2: [],
}
The next thing you need to do is add a new object into your new list. First, you need to make yourself an instance of Entity2.
Entity2 myEntity = new Entity2();
Then you need to put it into your list, using the add command on the List interface. I won't write this out for you, you need to work it out yourself. Have fun!

Related

Why can't I use add? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
I have a problem with and exercise in java.
I'm managin a logistics company I want to add a Booking into a BookingRegister.
Booking booking = Booking.makebooking (trip,originCity,arrivalDate,merchandise,departureDate,kg);
BookingRegister bookingRegister = getBookingRegister();
bookingRegister.add(Prenotazione);
But i can't use add() even if the BookingRegister is an ArrayList
public class BookingRegister {
private List<Booking> BookingRegister = new ArrayList<>();
}
First, you should not make the List public. Instead, provide a method that takes a Booking and does something with it. That could be adding it to a List. Like,
public class BookingRegister {
private List<Booking> register = new ArrayList<>();
public void addBooking(Booking b) {
register.add(b);
}
}
Next, call that method. Like,
// This looks like a builder pattern. Why not new Booking?
Booking booking = Booking.makebooking (trip,
originCity, arrivalDate, merchandise, departureDate, kg);
BookingRegister bookingRegister = getBookingRegister();
bookingRegister.addBooking(booking);
Because the List BookingRegister is private.
Make it public.
Another thing is, in line 3 you have used the class name instead of the variable name.
Changing it to bookingRegister would solve it...

Java 8 iterate nested loop and fetch details [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a model object which contains nested arrays and i want to retrieve some details within that model .How to collect property msg from subscriberCriteriaList into an array where subscriberCriteriaList.status is FAIL. I would expect java 8 solution for the same ? Below is the sample model objects and the corresponding json structure .
public class Data{
private List<subscriberList> subscriberCriteriaList;
}
public class subscriberList{
private String mdn;
private List<SubscriberCriteriaList> subscriberCriteriaList;
}
public class SubscriberCriteriaList{
private String status;
private String msg;
}
Sample json structure
{
"subscriberList": [
{
"mdn": "string",
"subscriberCriteriaList": [
{
"status": "FAIL",
"msg": "error message"
}
]
}
]
}
Assuming that the top-level object has type Data, and appropriate getters are available in all the mentioned classes, it is possible to apply flatMap to the nested lists and filter by status value:
String[] failureMessages = data.getSubscriberCriteriaList()
.stream() // Stream<subscriberList>
.flatMap(sl -> sl.getSubscriberCriteriaList().stream()) // Stream<SubscriberCriteriaList>
.filter(scl -> "FAIL".equals(scl.getStatus()))
.map(SubscriberCriteriaList::getMsg) // map to messages
.distinct() // (optionally) remove duplicates if necessary
.toArray(String[]::new);

Someone please explain contractions in Java code syntax [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
A question from dummy.... What is the full version of code below?
How to interpret it in classic (long) version of code?
FirebaseDatabase.getInstance()
.getReference()
.push()
.setValue(new ChatMessage(input.getText().toString(),
FirebaseAuth.getInstance()
.getCurrentUser()
.getDisplayName())
);
The called methods all return an object (apart of the last one), not void. In some other languages, you'd call them functions as opposed to procedures.
Since the method returns an object, you can in turn call a method on that returned object, and chain the calls like this.
It's equivalent to something like this, if that makes it more clear to you:
<some class> var1 = FirebaseDatabase.getInstance();
<some class> var2 = var1.getReference();
<some class> var3 = var2.push();
var3.setValue(new ChatMessage(input.getText().toString(),
FirebaseAuth.getInstance()
.getCurrentUser()
.getDisplayName())
);
It's just Java
SomeClass1 instance = FirebaseDatabase.getInstance();
SomeClass2 reference = instance.getReference();
SomeClass3 push = reference.push();
SomeClass4 authInstance = FirebaseAuth.getInstance();
SomeClass5 currentUser = authInstance.getCurrentUser();
SomeClass6 displayName = currentUser.getDisplayName();
SomeClass6 message = input.getText();
SomeClass7 messageAsString = message.toString();
SomeClass8 chatMessage = new ChatMessage(messageAsString, displayName);
push.setValue(chatMessage);
Note: Code is ridiculously formatted for the purpose of clarity. Please don't use formatting like this example in your code.

Looping through Object of Objects in java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have an object which in turn contains other objects. Now I have to iterate through this main object and then pick each object and then iterate through them to find out whether any empty fields are present in them. If the object itself is empty, I have to cut it out of main object. Any thoughts on this please.
public class Transactions {
private Integer totalTransactionCount = null;
private List<Transaction> transactionsList = new ArrayList<Transaction>();
}
public class Transaction {
private String amount = null;
private Foreign foreign = null;
}
public class Foreign {
private String amount = null;
private String commissionAmount = null;
private String exchangeRate = null;
}
Now I have a Transaction object with me and I have to loop throught each of its fields and in turn loop through their fields to find out any null/empty fields.
pseudo code for looping through a list of lists:
for each (innerList in outerList) do
if(innerlist.size == 0) then
//Code for removing empty inner lists.
else
for each ( object in innerList) do
//Check if objects are empty as well and remove it
end for
end if
end for
EDIT: Pointing out lack of research.
I would like to point out that you haven't really done your research properly, simply by googling iterate list of object as well as iterate list of list of object I got plenty of solutions.
Not to mention a question already asked here on Stack Overflow, please read the first answer of this post

BucketSort in Java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to get my head round the bucketsorting algorithm, but failed to do so.
Looked at numerous examples... but can't get it working...
Let's say I have this:
public class Employee {
int id; /// example: 52015
String firstname,lastname;
String department;
}
I have a huge list of employees, I then strip the list of all employees to sublists for each department. And the goal is to bucketsort these lists, on employee id. So I have my arraylists of employees, ready to pass on. I just can't seem to understand it.
THANK YOU!
Instead of bucket-sorting use Comparable<Employee> interface.
public class Employee implement Comparable<Employee> {
int id; /// example: 52015
String firstname,lastname;
String department;
public int compareTo(Employee compareEmployee) {
return this.id - compareEmployee.getID();
}
Anyway U can read this article to understand this mechanism better.

Categories