How to get a set of an instance variable using Stream - java

I have a class called Change. This class has two String variables called format and description, with normal getters and setters.
If I have a ArrayList<Change> changes how do I get a Set of all the different Formats using Java Streams?

You use Stream#map method in order to get a Stream of strings (based on the "method" you passed as argument), and then you collect it using Stream#collect method.
For example:
public class Change {
private String format;
private String description;
public Change(String format, String description) {
this.format = format;
this.description = description;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getFormat() {
return format;
}
public void setFormat(String format) {
this.format = format;
}
public static void main(String[] args) {
List<Change> list = new ArrayList<>();
list.add(new Change("format1", "descr1"));
list.add(new Change("formatelse", "descrelse"));
Set<String> formats = list.stream().map(Change::getFormat).collect(Collectors.toSet());
}
}

Related

How to print enum description

Im creating a Java application, I used enum to create movie category. When I input MovieCategory.WAR I would like to see War movie(My description) instead of WAR. How is it possible? I tried MovieCategory.WAR.getDescription() but does't work.
public enum MovieCategory {
COMEDY("Comedy"), HORROR("Horror"), SCIFI("Sci-Fi"),
ACTION("Action movie"), ROMANTIC("Romantic"),
CLASSIC("Classic"), WAR("War movie");
private final String description;
MovieCategory(String description) {
this.description = description;
}
public String getDescription() {
return description;
}
}
public class MovieManager {
private List<Movie> movieList;
public MovieManager() {
this.movieList = new ArrayList<>();
movieList.add(new Movie("Simple movie", MovieCategory.WAR,"Testing description.",167,12));
(...)
The enum works correctly:
public static void main(String[] args) {
// Displays: Comedy
System.out.println(COMEDY.getDescription());
// Displays: COMEDY
System.out.println(COMEDY);
}
Or, maybe you want the toString method to use description?
public enum MovieCategory {
COMEDY("Comedy");
private final String description;
MovieCategory(String description) {
this.description = description;
}
public String getDescription() {
return description;
}
#Override
public String toString() {
return getDescription();
}
public static void main(String[] args) {
// Displays: Comedy
System.out.println(COMEDY.getDescription());
// Displays: Comedy
System.out.println(COMEDY);
}
}
UPDATE #3: It seems the issue is the JSON response. If you want the description to be returned, you can annotate the getDescription method wtih #JsonValue.
import com.fasterxml.jackson.annotation.JsonValue;
public enum MovieCategory {
WAR("War movie");
private final String description;
MovieCategory(String description) {
this.description = description;
}
#JsonValue
public String getDescription() {
return description;
}
#Override
public String toString() {
return getDescription();
}
public static void main(String[] args) {
// Displays: Comedy
System.out.println(WAR.getDescription());
// Displays: Comedy
System.out.println(WAR);
}
}

How do I properly display the data from an object within an object?

Creating a linked list that has a class, Notes, that's basically an object that contains NotePages, another class, that contains Strings that are "title" and description. The class Notes extends another class which is the LinkedList class mentioned earlier. The problem is that when I try printing out the Notes with a note page in it, the display comes out like this:
Note one
[]
Assigning what to display in the object looks like:
NotePages page = new NotePages("title one", "Description");
Notes note = new Notes("Note one", page);
note.printNote();
I've tried creating other methods such as a String method to try and return the page properly to no avail.
Here's my code for the Notes object.
public class Notes extends LinkedList{
private String title;
private LinkedList<NotePages> pages;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public LinkedList<NotePages> getPages() {
return pages;
}
public void setPages(LinkedList<NotePages> pages) {
this.pages= pages;
}
public Notes(String title, LinkedList<NotePages> pages) {
this.title = title;
this.pages= pages;
}
void printNote(){
System.out.println(getTitle()+"\n"+getPages());
}
}
I need the display to output something closer to this:
Note one
title one
description
Here is the NotePages Class:
import java.awt.*;
public class NotePages {
private String title;
private String description;
private Color label;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Color getLabel() {
return label;
}
public void setLabel(Color label) {
this.label = label;
}
NotePages(String title, String description, Color label){
this.title = title;
this.description = description;
this.label = label;
}
NotePages(String title,String description){
this.title = title;
this.description = description;
}
void printPage(){
System.out.println(getTitle() + "\n "+ getDescription());
}
}
The change will need to be made in printNote function.
When the constructor for Notes is initialized pages variable gets initialised with a NotePage LinkedList.
pages does not contain the values directly. It contains objects of NotePage. So you need to use a loop for traversing through all the linkedList objects and then print the title and description for every object.
void printNote(){
System.out.println(getTitle());
//no need to use getPages function, pages already has your list
for(int i=0; i<pages.size();i++)
System.out.println(pages.get(i).getTitle()+"\n"+pages.get(i).getDescription());
}
get function will help you get the object at every ith index and then simply use the get functions of your NotePage class to print title and description.
Also the in main function add a linkedList object to the constructor of Note instead of a NotePage.
LinkedList<NotePages> notelist = new LinkedList<>();
notelist.add(page);
//adding LinkedList object to Notes constructor
Notes note = new Notes("Note one", notelist);

Adobe Flash Builder does not refresh a value object

I'm currently facing a problem with Adobe Flash Builder, I work on a project with a Java core and services to communicate with a Flex client. (BlazeDS channel)
I decided to remove an array list in an object because it was no longer useful, so I commented the following lines in my object value to remove the ArrayList named "unitFactors"
public class UnitValue extends PhysicalQuantityObjectValue{
String symbol;
String name;
String description;
String physicalQuantityName;
//UnitFactorValue[] unitFactors;
UnitConstraintValue unitConstraint;
public UnitValue(AdnUnit unit) {
super(unit.getId());
//Constructor code ...
}
public UnitValue() {}
public String getSymbol() {
return symbol;
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPhysicalQuantityName() {
return physicalQuantityName;
}
public UnitValue(String symbol, String name, String description,
String physicalQuantityName,
UnitConstraintValue unitConstraint) {
super();
this.symbol = symbol;
this.name = name;
this.description = description;
this.physicalQuantityName = physicalQuantityName;
//this.unitFactors = unitFactors;
this.unitConstraint = unitConstraint;
}
public void setPhysicalQuantityName(String physicalQuantityName) {
this.physicalQuantityName = physicalQuantityName;
}
//public UnitFactorValue[] getUnitFactors() {
//return unitFactors;
//}
//public void setUnitFactors(UnitFactorValue[] unitFactors) {
//this.unitFactors = unitFactors;
//}
public UnitConstraintValue getUnitConstraint() {
return unitConstraint;
}
public void setUnitConstraint(UnitConstraintValue unitConstraint) {
this.unitConstraint = unitConstraint;
}
Well, no problem with the Java part. I tested it with SoapUI, everything's fine.
But when I switch on my Flex client code to refresh the valueObjects, it appears impossible to refresh UnitValue, unitFactors is still there, and it's obviously impossible to make my application work without errors on that specific point ... Can't find getUnitFactors().
Is someone facing the same problem with AdobeFlashBuilder?

Two Dimensional ArrayList Java (Gui)

I am trying to create program that holds the details of various instruments (e.g. guitars, keyboards). I want it to have information on each item in the Arraylist. I want each instrument to have a Manufacturer and a "description". I am not new to 2D arrays, however i do not know how to use it in Array lists.
I have given it an attempt. (Ps i need to be able to access all of these things to be able to put into a gui. I hope that made sense.)
public class Main {
public static Login form = new Login();
public static ArrayList<ArrayList<String>> instt = new ArrayList<ArrayList<String>>();
public static ArrayList<String> row = new ArrayList<String>();
public static void main(String[] args) {
row.add("Chelo");
row.add("Drums");
row.add("Flute");
row.add("Guitar");
row.add("Harp");
row.add("Piano");
row.add("Recorder");
row.add("Trombone");
row.add("Trumpet");
row.add("Xylophone");
instt.add(row);
form.setVisible(true);
}
}
Any suggestions? Thank you.
The best approach would be to create an Instrument-Class containing all your information.
public static ArrayList<Instrument> row = new ArrayList<Instrument>();
public static void main(String[] args){
row.add(new Instrument("Guitar","Stackoverflow-Instruments","Best guitar!!"));
row.add(new Instrument("Piano","Stackoverflow-Instruments","Best piano!!"));
}
public static class Instrument{
private String name;
private String manufacturer;
private String description;
public String getName() {
return name;
}
public String getManufacturer() {
return manufacturer;
}
public String getDescription() {
return description;
}
public Instrument(String name, String manufacturer, String description) {
this.name = name;
this.manufacturer = manufacturer;
this.description = description;
}
}

How to create an object from an enumeration without all values in Java

I have an enumeration called PaymentFrequency:
public enum PaymentFrequency {
D("Daily"),
W("Weekly"),
M("Monthly"),
Y("Yearly");
private final String description;
PaymentFrequency(final String description) {
this.description = description;
}
public String getDescription() {
return description;
}
}
And now if I do this in my bean:
private PaymentFrequency[] paymentPeriods = PaymentFrequency.values();
public PaymentFrequency[] getPaymentPeriods() {
return paymentPeriods;
}
public void setPaymentPeriods(PaymentFrequency[] paymentPeriods) {
this.paymentPeriods = paymentPeriods;
}
I will get all the values from my enumeration. How can I get only the values W and M ?
Well, you could do
private PaymentFrequency[] paymentPeriods =
new PaymentFrequency[] {PaymentFrequency.W, PaymentFrequency.M};
You can add an array
static final PaymentFrequency[] WM = { W, M };
private PaymentFrequency[] paymentPeriods = new PaymentFrequency[] {PaymentFrequency.W, PaymentFrequency.M};
I'm not sure if this is your question...

Categories