Skip to content

Commit a00dc66

Browse files
committed
updated README with HashMap exmaple
1 parent 752cb5a commit a00dc66

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ System.out.println(user.toString());
6565

6666
## Using as HashMap Keys
6767

68+
### Using Tuple as a key
69+
6870
Both `Tuple` and `NamedTuple` override `equals()` and `hashCode()`, making them perfectly suitable for use as keys in a `HashMap` or as elements in a `HashSet`.
6971

7072
Two tuples are considered equal if they contain the same elements in the same order. Two named tuples are equal if they contain the same key-value pairs.
@@ -82,6 +84,24 @@ cache.put(key, "Cached Result");
8284
System.out.println(cache.get(Tuple.of("request", 12345))); // Output: Cached Result
8385
```
8486

87+
### Using NamedTuple as a key
88+
89+
```java
90+
import java.util.HashMap;
91+
import com.appxiom.ax.tuple.NamedTuple;
92+
import java.util.Map;
93+
94+
// Using NamedTuple as a key
95+
HashMap<NamedTuple, String> userRegistry = new HashMap<>();
96+
NamedTuple key1 = NamedTuple.of(Map.of("id", 101, "role", "admin"));
97+
98+
userRegistry.put(key1, "Admin Account");
99+
100+
// Retrieve using a new instance with identical keys and values
101+
NamedTuple key2 = NamedTuple.of(Map.of("id", 101, "role", "admin"));
102+
System.out.println(userRegistry.get(key2)); // Output: Admin Account
103+
```
104+
85105
## License
86106

87107
MIT License

0 commit comments

Comments
 (0)