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);
Related
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 5 months ago.
Improve this question
I generate a report where I read data from postgresql and then populate a Java object
Below is kind of sample skeleton where I want to show there are many collection object within Main object i.e ReportMessageStructure
public class ReportMessageStructure {
protected MessageHeader messageHeader;
protected MessageDuration messageDuration;
protected ObjectA sampleListA;
protected ObjectB sampleListB;
protected ObjectC smapleListC;
}
Now My requirement is to write data into TSV file . Could you please suggest me best way to do this. I know I can use JAXB if I had to convert into XML. However need way to convert into TSV. Any tips/suggestion would be great help.
Max size the data will produce would be around 700MB from object to TSV
You can use CSVPrinter from Apache Commons CSV library with CSVFormat equal to TDF.
try (CSVPrinter printer = new CSVPrinter(new FileWriter("csv.txt"), CSVFormat.TDF)) {
printer.printRecord("id", "userName", "firstName", "lastName", "birthday");
printer.printRecord(1, "john73", "John", "Doe", LocalDate.of(1973, 9, 15));
printer.println();
printer.printRecord(2, "mary", "Mary", "Meyer", LocalDate.of(1985, 3, 29));
} catch (IOException ex) {
ex.printStackTrace();
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 11 months ago.
Improve this question
I wanted to add an array as an instance field, also a part of an object, however it gives me interesting errors when i try to do so:
class Surgeon {
private String[] surgeons;
public Surgeon() {
this.surgeons = {"Romero", "Jackson", "Hannah", "Camilla"};
}
public static void main(String[] args) {
Surgeon surg = new Surgeon();
}
}
And it gives me three errors.
This does not make any sense bc is it not how you initialize an array?
You have to add the operator "new" to instantiate the array in the class constructor
public Surgeon() {
this.surgeons = new String[]{"Romero", "Jackson", "Hannah", "Camilla"};
}
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.
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
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 7 years ago.
Improve this question
I need to create a binding that allows me to do the equivalent of String.replaceAll(...) but with bindings. I have a string, "${driver} driving ${name}", and I want the keys, "${driver}", etc. to be replaced with the specific property. I also want the returned property to be able to add listeners so when driverProperty or another changes, the returned property value will change without having to re-call getString().
public String getString(Derby derby) {
String ret;
if (driverProperty.get().equals("") && nameProperty.get().equals("") && numberProperty.get().equals("") && groupProperty.get().equals("")) {
ret = "[blank]";
} else {
ret = (String) derby.getSettings().get("general.cardisplay").getValue();
ret = ret.replace("${driver}", driverProperty.get());
ret = ret.replace("${name}", nameProperty.get());
ret = ret.replace("${number}", numberProperty.get());
ret = ret.replace("${group}", groupProperty.get());
}
return ret;
}
Use Bindings.createStringBinding(). This takes a function supplying the String and a list of values to observe; if any of those values change the binding is marked as invalid.
Your question isn't too clear to me, but I think you can do something like
StringBinding formattedString = Bindings.createStringBinding(
() -> getString(derby),
derby.settingsProperty(),
nameProperty, numberProperty,
driverProperty, groupProperty);
Now you can do things like
formattedString.addListener((obs, oldFormattedString, newFormattedString) -> {
// invoked any time the formatted string changes...
});
or
Label label = new Label();
label.textProperty().bind(formattedString);