https://github.com/shubhamv108/java-immutable
Simple programs to demonstrate holding immutable data in java objects.
https://github.com/shubhamv108/java-immutable
immutable java java-record java14
Last synced: 7 months ago
JSON representation
Simple programs to demonstrate holding immutable data in java objects.
- Host: GitHub
- URL: https://github.com/shubhamv108/java-immutable
- Owner: shubhamv108
- Created: 2022-10-11T20:12:50.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2024-01-13T18:51:00.000Z (over 1 year ago)
- Last Synced: 2025-01-29T22:44:46.592Z (8 months ago)
- Topics: immutable, java, java-record, java14
- Language: Java
- Homepage:
- Size: 63.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
```
public final class PersonImmutable {private final String name;
private final HashMap info;private final Address address;
public PersonImmutable(final String name, final HashMap info, final Address address) {
this.name = name; // String is immutable for security & concurrency.
this.info = new HashMap<>(info);
this.address = address.clone();
}public String getName() {
return name;
}public HashMap getInfo() {
return new HashMap<>(info);
}public Address getAddress() {
return this.address.clone();
}@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (!(o instanceof PersonImmutable))
return false;
PersonImmutable person = (PersonImmutable) o;
return Objects.equals(name, person.name) &&
Objects.equals(info, person.info);
}@Override
public int hashCode() {
return Objects.hash(name, info);
}@Override
public String toString() {
return "PersonImmutable[" +
"name=" + name + ", " +
"info=" + info + ", " +
"address=" + address + ']';
}
}
```
```
public record PersonRecord(String name, HashMap info, Address address) {public PersonRecord(final String name, final HashMap info, final Address address) {
this.name = name; // String is immutable for security & concurrency.
this.info = new HashMap<>(info);
this.address = address;
}@Override
public HashMap info() {
return new HashMap<>(info);
}public Address address() {
return this.address.clone();
}
}
```