I'm having an issue with some pointers from what I can read about java. It always passes parameters as values rather than references. here
I have a project and employee class that should "share" an object but when I create an instance of activity, add it to the project list and then later add it to the employee list of activities it works. But then when I change the worktime for an employee it's only visible via the project instance where i add the worktime from. It's not visible when i then call the activity worktime from the employee object.
Is there a way to "share" an object between classes e.g. pass it by reference like you can in PHP?
When I output the hashcodes of the activity objects in both classes they are also different...
Project class:
public class Project {
private List<Activity> activites = new ArrayList<Activity>();
public List<Activity> getActivities() {
return activites;
}
public void setActivities(Activity activity) {
this.activites.add(activity);
}
}
employee class:
public class Employee {
private List<Activity> activities = new ArrayList<Activity>();
public List<Activity> getActivities() {
return activities;
}
public void setActivity(Activity activity) {
activities.add(activity);
}
}
activity class:
public class Activity {
private String activityName;
private HashMap<Employee,Integer> workTime = new HashMap<Employee,Integer>();
public Activity(String activity) {
this.activityName = activity;
}
public HashMap<Employee, Integer> getWorkTime() {
return workTime;
}
public void setWorkTime(Employee e, Integer t) {
workTime.put(e, t);
}
}
An example of the issue:
public void main(String[] args) {
Activity a = new Activity('task i');
Project p = new Project();
p.setActivities(a);
Employee e = new Employee();
e.setActivity(a);
p.getActivities().get(0).setWorkTime(e,5);
System.out.println(p.getActivities().get(0).getWorkTime()); // 5
System.out.println(e.getActivities().get(0).getWorkTime()); // -> null (would like 5)
}
The problem is here
public void setWorkTime(Employee e, Integer t) {
workTime.put(e, t);
}
You are putting the employee instance in a map. But the Employee class does not override equals and hashCode so every instance of Employee will be a new and unique key.
You need to override equals and hashCode using some field(s) of the Employee class that would constitute equal instances (e.g. Employee name and/or Employee ID).
UPDATE
In recognition of the comments regarding only a single instance of Employee is being used, I concur. Although I believe it is important to do as I suggested it was not the specific problem.
I ran the above code and made main a static entry point and fixed the Activity argument to correctly add a String. The OP originally had the following:
System.out.println(p.getActivities().get(0).getWorkTime()); // 5
System.out.println(e.getActivities().get(0).getWorkTime()); // -> null (would like 5)
The above does not print 5 or null. It prints the default toString of the map.
The Employee instance must must be supplied as the key via get and it will print 5 in both cases.
System.out.println(p.getActivities().get(0).getWorkTime().get(e)); // 5
System.out.println(e.getActivities().get(0).getWorkTime().get(e)); // 5
Related
I want to achieve method chaining in Java.
How can I achieve it?
Also let me know when to use it.
public class Dialog {
public Dialog() {
}
public void setTitle(String title) {
//Logic to set title in dialog
}
public void setMessage(String message) {
//Logic to set message
}
public void setPositiveButton() {
//Logic to send button
}
}
I want to create method chaining that I can use as follows:
new Dialog().setTitle("Title1").setMessage("sample message").setPositiveButton();
or like
new Dialog().setTitle("Title1").setMessage("sample message");
or like
new Dialog().setTitle("Title1").setPositiveButton();
Have your methods return this like:
public Dialog setMessage(String message)
{
//logic to set message
return this;
}
This way, after each call to one of the methods, you'll get the same object returned so that you can call another method on.
This technique is useful when you want to call a series of methods on an object: it reduces the amount of code required to achieve that and allows you to have a single returned value after the chain of methods.
An example of reducing the amount of code required to show a dialog would be:
// Your Dialog has a method show()
// You could show a dialog like this:
new Dialog().setMessage("some message").setTitle("some title")).show();
An example of using the single returned value would be:
// In another class, you have a method showDialog(Dialog)
// Thus you can do:
showDialog(new Dialog().setMessage("some message").setTitle("some title"));
An example of using the Builder pattern that Dennis mentioned in the comment on your question:
new DialogBuilder().setMessage("some message").setTitle("some title").build().show();
The builder pattern allows you to set all parameters for a new instance of a class before the object is being built (consider classes that have final fields or objects for which setting a value after it's been built is more costly than setting it when it's constructed).
In the example above: setMessage(String), setTitle(String) belong to the DialogBuilder class and return the same instance of DialogBuilder that they're called upon; the build() method belongs to the DialogBuilder class, but returns a Dialog object the show() method belongs to the Dialog class.
Extra
This might not be related to your question, but it might help you and others that come across this question.
This works well for most use cases: all use cases that don't involve inheritance and some particular cases involving inheritance when the derived class doesn't add new methods that you want to chain together and you're not interested in using (without casting) the result of the chain of methods as an object of the derived.
If you want to have method chaining for objects of derived classes that don't have a method in their base class or you want the chain of methods to return the object as a reference of the derived class, you can have a look at the answers for this question.
Just add a static builder method, and create another set of the setter methods.
For example
class Model {
private Object FieldA;
private Object FieldB;
public static Model create() {
return new Model();
}
public Model withFieldA(Object value) {
setFieldA(value);
return this;
}
public Model withFieldB(Object value) {
setFieldB(value);
return this;
}
}
...
And use it like
Model m = Model.create().withFieldA("AAAA").withFieldB(1234);
example of reducing the amount of code required to show a dialog would be:
package com.rsa.arraytesting;
public class ExampleJavaArray {
String age;
String name;
public ExampleJavaArray getAge() {
this.age = "25";
return this;
}
public ExampleJavaArray setName(String name) {
this.name = name;
return this;
}
public void displayValue() {
System.out.println("Name:" + name + "\n\n" + "Age:" + age);
}
}
another class
package com.rsa.arraytesting;
public class MethodChaining {
public static void main(String[] args) {
ExampleJavaArray mExampleJavaArray = new ExampleJavaArray();
mExampleJavaArray.setName("chandru").getAge().displayValue();
}
}
In case if you are using lombok, you can use parameter in your lombok.config:
lombok.accessors.chain = true
Or for particular data classes you can declare #Accessors(chain = true) annotation:
import lombok.experimental.Accessors;
#Accessors(chain = true)
#Data
public class DataType {
private int value;
// will generate setter:
public DataType setValue(int value) {
this.value = value;
return this;
}
}
I want to achieve method chaining in Java.
How can I achieve it?
Also let me know when to use it.
public class Dialog {
public Dialog() {
}
public void setTitle(String title) {
//Logic to set title in dialog
}
public void setMessage(String message) {
//Logic to set message
}
public void setPositiveButton() {
//Logic to send button
}
}
I want to create method chaining that I can use as follows:
new Dialog().setTitle("Title1").setMessage("sample message").setPositiveButton();
or like
new Dialog().setTitle("Title1").setMessage("sample message");
or like
new Dialog().setTitle("Title1").setPositiveButton();
Have your methods return this like:
public Dialog setMessage(String message)
{
//logic to set message
return this;
}
This way, after each call to one of the methods, you'll get the same object returned so that you can call another method on.
This technique is useful when you want to call a series of methods on an object: it reduces the amount of code required to achieve that and allows you to have a single returned value after the chain of methods.
An example of reducing the amount of code required to show a dialog would be:
// Your Dialog has a method show()
// You could show a dialog like this:
new Dialog().setMessage("some message").setTitle("some title")).show();
An example of using the single returned value would be:
// In another class, you have a method showDialog(Dialog)
// Thus you can do:
showDialog(new Dialog().setMessage("some message").setTitle("some title"));
An example of using the Builder pattern that Dennis mentioned in the comment on your question:
new DialogBuilder().setMessage("some message").setTitle("some title").build().show();
The builder pattern allows you to set all parameters for a new instance of a class before the object is being built (consider classes that have final fields or objects for which setting a value after it's been built is more costly than setting it when it's constructed).
In the example above: setMessage(String), setTitle(String) belong to the DialogBuilder class and return the same instance of DialogBuilder that they're called upon; the build() method belongs to the DialogBuilder class, but returns a Dialog object the show() method belongs to the Dialog class.
Extra
This might not be related to your question, but it might help you and others that come across this question.
This works well for most use cases: all use cases that don't involve inheritance and some particular cases involving inheritance when the derived class doesn't add new methods that you want to chain together and you're not interested in using (without casting) the result of the chain of methods as an object of the derived.
If you want to have method chaining for objects of derived classes that don't have a method in their base class or you want the chain of methods to return the object as a reference of the derived class, you can have a look at the answers for this question.
Just add a static builder method, and create another set of the setter methods.
For example
class Model {
private Object FieldA;
private Object FieldB;
public static Model create() {
return new Model();
}
public Model withFieldA(Object value) {
setFieldA(value);
return this;
}
public Model withFieldB(Object value) {
setFieldB(value);
return this;
}
}
...
And use it like
Model m = Model.create().withFieldA("AAAA").withFieldB(1234);
example of reducing the amount of code required to show a dialog would be:
package com.rsa.arraytesting;
public class ExampleJavaArray {
String age;
String name;
public ExampleJavaArray getAge() {
this.age = "25";
return this;
}
public ExampleJavaArray setName(String name) {
this.name = name;
return this;
}
public void displayValue() {
System.out.println("Name:" + name + "\n\n" + "Age:" + age);
}
}
another class
package com.rsa.arraytesting;
public class MethodChaining {
public static void main(String[] args) {
ExampleJavaArray mExampleJavaArray = new ExampleJavaArray();
mExampleJavaArray.setName("chandru").getAge().displayValue();
}
}
In case if you are using lombok, you can use parameter in your lombok.config:
lombok.accessors.chain = true
Or for particular data classes you can declare #Accessors(chain = true) annotation:
import lombok.experimental.Accessors;
#Accessors(chain = true)
#Data
public class DataType {
private int value;
// will generate setter:
public DataType setValue(int value) {
this.value = value;
return this;
}
}
Let's say you have different employees but each employee class has a salary() method. How can I make these different objects I made and put in an ArrayList use the method salary()?
public void betaalSalarissen(){
for(int counter = 0;werknemers.size()>counter;counter++){
Class objectClass = werknemers.get(counter).getClass();
**objectClass.salaris();** //this won't work, help please!
}
}
public void neemInDienst(Object persoon){ //objects from different classes, different employees
werknemers.add(persoon);
}
There's no reason to try to get the class; just use polymorphism.
public class Employee {
public double salaris() { ... }
}
public class CommissionedEmployee extends Employee {
#Override
public double salaris() { ... }
}
List<Employee> employees = new ArrayList<>();
// add some employees of whatever subtype(s)
for (Employee e : employees) {
e.salaris();
}
the quick solution here is to cast the result from werknemers.get(counter)``to yourEmployee` class (class name just guessed)
((Employee)werknemers.get(counter)).salaris();
but in the long run you should apply a generic parameter to the collection variable and the methods parameter:
private Collection<Employee> werknemers = new ArrayList<>();
public void neemInDienst(Employee persoon){ //objects from different classes, different employees
werknemers.add(persoon);
}
then you can simply iterate over the element without cast just as ChiefTwoPencils suggested.
My goal is to have a list of cars as an Object so that, I can retrieve a Car from that list. As well as get details of the cars. Can someone point me to the right direction
What I have done so far.
Create a class called Car and have the variables CarNum, carName, carPlate;
generated getters and setters for the variables and a toString as the carName
Create a class called CarCollection as follows
.
public class CarCollection {
private List<CarItem> mCarList;
public void addVan(CarItem v) {
mCarList.add(v);
}
public List<CarItem> getCarList() {
return mCarList;
}
The following test doesn't work. Why?
public class TestCarCollectionprocess {
public static void main(String[] args) {
CarItem car1 = new CarItem();
car1.setmCarName("Pedro");
car1.setmCarNum(1);
CarItem car2 = new CarItem();
car2.setmCarName("Rene");
car2.setmCarNum(2);
CarCollection carList = new CarCollection();
carList.addCar(car1);
carList.addCar(car2);
System.out.println(carList.getCarList());
}
}
What I see from your code, you should get NullPointerException in addVan method since you didn't initialize List, so change it like this:
private List<CarItem> mCarList = new ArrayList<>();
You are trying to add a CarItem to the CarCollection's mCarList without ever instantiating the list so you should be getting a null reference exception. In your carCollection class, create a constructor that sets
mCarList = new List<CarItem>();
I want to achieve method chaining in Java.
How can I achieve it?
Also let me know when to use it.
public class Dialog {
public Dialog() {
}
public void setTitle(String title) {
//Logic to set title in dialog
}
public void setMessage(String message) {
//Logic to set message
}
public void setPositiveButton() {
//Logic to send button
}
}
I want to create method chaining that I can use as follows:
new Dialog().setTitle("Title1").setMessage("sample message").setPositiveButton();
or like
new Dialog().setTitle("Title1").setMessage("sample message");
or like
new Dialog().setTitle("Title1").setPositiveButton();
Have your methods return this like:
public Dialog setMessage(String message)
{
//logic to set message
return this;
}
This way, after each call to one of the methods, you'll get the same object returned so that you can call another method on.
This technique is useful when you want to call a series of methods on an object: it reduces the amount of code required to achieve that and allows you to have a single returned value after the chain of methods.
An example of reducing the amount of code required to show a dialog would be:
// Your Dialog has a method show()
// You could show a dialog like this:
new Dialog().setMessage("some message").setTitle("some title")).show();
An example of using the single returned value would be:
// In another class, you have a method showDialog(Dialog)
// Thus you can do:
showDialog(new Dialog().setMessage("some message").setTitle("some title"));
An example of using the Builder pattern that Dennis mentioned in the comment on your question:
new DialogBuilder().setMessage("some message").setTitle("some title").build().show();
The builder pattern allows you to set all parameters for a new instance of a class before the object is being built (consider classes that have final fields or objects for which setting a value after it's been built is more costly than setting it when it's constructed).
In the example above: setMessage(String), setTitle(String) belong to the DialogBuilder class and return the same instance of DialogBuilder that they're called upon; the build() method belongs to the DialogBuilder class, but returns a Dialog object the show() method belongs to the Dialog class.
Extra
This might not be related to your question, but it might help you and others that come across this question.
This works well for most use cases: all use cases that don't involve inheritance and some particular cases involving inheritance when the derived class doesn't add new methods that you want to chain together and you're not interested in using (without casting) the result of the chain of methods as an object of the derived.
If you want to have method chaining for objects of derived classes that don't have a method in their base class or you want the chain of methods to return the object as a reference of the derived class, you can have a look at the answers for this question.
Just add a static builder method, and create another set of the setter methods.
For example
class Model {
private Object FieldA;
private Object FieldB;
public static Model create() {
return new Model();
}
public Model withFieldA(Object value) {
setFieldA(value);
return this;
}
public Model withFieldB(Object value) {
setFieldB(value);
return this;
}
}
...
And use it like
Model m = Model.create().withFieldA("AAAA").withFieldB(1234);
example of reducing the amount of code required to show a dialog would be:
package com.rsa.arraytesting;
public class ExampleJavaArray {
String age;
String name;
public ExampleJavaArray getAge() {
this.age = "25";
return this;
}
public ExampleJavaArray setName(String name) {
this.name = name;
return this;
}
public void displayValue() {
System.out.println("Name:" + name + "\n\n" + "Age:" + age);
}
}
another class
package com.rsa.arraytesting;
public class MethodChaining {
public static void main(String[] args) {
ExampleJavaArray mExampleJavaArray = new ExampleJavaArray();
mExampleJavaArray.setName("chandru").getAge().displayValue();
}
}
In case if you are using lombok, you can use parameter in your lombok.config:
lombok.accessors.chain = true
Or for particular data classes you can declare #Accessors(chain = true) annotation:
import lombok.experimental.Accessors;
#Accessors(chain = true)
#Data
public class DataType {
private int value;
// will generate setter:
public DataType setValue(int value) {
this.value = value;
return this;
}
}