Looping through Object of Objects in java [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 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

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: Initialize Object Class [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 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!

How to call method recursively with 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 5 years ago.
Improve this question
There is java bean with list of elements and discount. I have to apply some calculate logic in those elements using recursion until that discount value gets zero.
Current implementation
for(CustomClass custom : customList) {
Pair<CustomClass, Integer> returnVal = myMethod(custom, discount);
}
private Pair<CustomClass, Integer> myMethod(CustomClass custom, Integer discount) {
pair.getKey().add(custom.setAmount(custom.getAmount - discount));
pair.getValue().add(discount - custom.getAmount);
return pair;
}
I have to do something like above and have to reuse the discount in multiple elements in the customList until it is zero. I want to make it recursive so that I get the discounted value in the CustomClass and discount is reduced.
returnVal has updated discount but it is not being used in next iteration.
Could someone give me good approach to solve this issue.?
To make recursive call, you need to call same method inside
private Pair<CustomClass, Integer> myMethod(CustomClass custom, Integer discount) {
int dis = discount - custom.getAmount;
pair.getKey().add(custom.setAmount(custom.getAmount - discount))
pair.getValue().add(dis);
if (dis <= 0)
{
return pair;
}
return myMethod (custom, dis);
}

easier way to format lines of code in Java to make your job easier? [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 5 years ago.
Improve this question
I am currently making an application that uses an api and it prints out information about that thing. So basically it gets the api and If i do System.out.println(result.getPlayer().get("displayname")); it will return the display name of the player that I am searching for. I was wondering if there was a way to make result.getPlayer().get("displayname") a variable because I have hundreds of statistics that I need to gather. so is it possible to have that line of code called displayname? Sorry if you don't understand.
I suggest that you make a special statistics/logging class that has static methods specifically for this. For example with your case, the following class can be used both to get the name and to print it. Of course you can combine them into a single method if you want just one functionality.
public class StatsLog {
public static String getPlayerDisplayName(final Result result) {
return (result == null)
? null
: result.getPlayer().get("displayname");
}
public static void printPlayerDisplayName(final Result result) {
final String displayName = getPlayerDisplayName(result);
if (displayName != null) {
System.out.println(displayName);
}
}
}
And when you call it:
StatsLog.printPlayerDisplayName(result);
You can use a getter like #Andrew Tobilko said. like this:
public String getDisplayName(){
return (result != null)? result.getPlayer().get("displayname") : "";
}
However, it depends on what is the "result" and the design of your class. And make sure to check for Null.

The best data structure and sorting algorithm to use for this particular situation [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
I'm writing a tiny java application that will read a person's details from a txt file(tab delimited)(id, First Name, Last Name, DoB, Address)
The txt file is already sorted by id and contains around 30,000 records. My program should provide the user the choice to sort either by First Name, Last Name or Address.
What i've done already is that I made a class Person and made an arraylist of type Person to store all the records and then used JAVA's collection sort to sort it by using different comparators like this:
public static Comparator<Person> addComp = new Comparator<Person>(){
public int compare(Person P1,Person P2){
String add1 = P1.getadd().toUpperCase();
String add2 = P2.getadd().toUpperCase();
return add1.compareTo(add2);
}
};
It works and all but what I wanted to know was if there was a faster algorithm to do it like in terms of running time and complexity.
Like if there was any other data structure that would be more suitable like a hash table or something
I think this is the best way to go. Using hashtables won't help here. In terms of asymptotic time complexity, you can't get any faster in general.
However you can improve user experience by pre-ordering the data after loading from file, so you only have to change a list reference on user interaction:
private List<Person> persons_by_id;
private List<Person> persons_by_name;
private List<Person> persons_by_address;
// ...
persons_by_id = readPersonsFromFile();
persons_by_name = new ArrayList<Person>(persons_by_id);
persons_by_address = new ArrayList<Person>(persons_by_id);
Collections.sort(persons_by_name, Person.ORDER_BY_NAME);
Collections.sort(persons_by_address, Person.ORDER_BY_ADDRESS);
You can make your comparators shorter with java8:
class Person {
// ...
public static final Comparator<Person> ORDER_BY_NAME =
Comparator.comparing((Person p) -> p.getName().toUpperCase());
public static final Comparator<Person> ORDER_BY_ADDRESS =
Comparator.comparing((Person p) -> p.getAddress().toUpperCase());
}

Categories