Skip to content

Commit 3ad5cdf

Browse files
author
nicolaiparlog
committed
Create a super-small demo for equality transformation
2 parents 1d0d1b4 + eca17ca commit 3ad5cdf

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.codefx.libfx.collection.transform;
2+
3+
import java.util.Set;
4+
5+
/**
6+
* Demonstrates how to use the {@link EqualityTransformingSet}.
7+
* <p>
8+
* The demonstrated example is based on the situation that we want a set of strings which uses only their length for
9+
* equality comparison.
10+
*/
11+
public class EqualityTransformingSetDemo {
12+
13+
/**
14+
* A set of strings which uses the length for equality.
15+
*/
16+
private final Set<String> lengthSet;
17+
18+
/**
19+
* Creates a new demo.
20+
*/
21+
public EqualityTransformingSetDemo() {
22+
lengthSet = EqualityTransformingCollectionBuilder
23+
.forType(String.class)
24+
.withEquals((a, b) -> a.length() == b.length())
25+
.withHash(String::length)
26+
.buildSet();
27+
}
28+
29+
/**
30+
* Runs this demo.
31+
*
32+
* @param args
33+
* command line arguments (will not be used)
34+
*/
35+
public static void main(String[] args) {
36+
EqualityTransformingSetDemo demo = new EqualityTransformingSetDemo();
37+
38+
demo.addSomeElements();
39+
}
40+
41+
private void addSomeElements() {
42+
lengthSet.add("a");
43+
lengthSet.add("b");
44+
print(lengthSet.toString());
45+
}
46+
47+
private static void print() {
48+
System.out.println();
49+
}
50+
51+
private static void print(String text) {
52+
System.out.println(text);
53+
}
54+
55+
}

0 commit comments

Comments
 (0)