An open API service indexing awesome lists of open source software.

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.

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();
}
}
```