Skip to content

Commit 852a29a

Browse files
committed
Tuple
1 parent 710ba23 commit 852a29a

File tree

3 files changed

+116
-1
lines changed

3 files changed

+116
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Below topics/problems are covered as of now.
3939
- [X] [Associative Array](../master/src/com/deepak/data/structures/Arrays/AssociativeArray.java)
4040
- [ ] Sparse Array
4141
- [X] [Pair](../master/src/com/deepak/data/structures/Arrays/Pair.java)
42-
- [ ] Tuple
42+
- [X] [Tuple](../master/src/com/deepak/data/structures/Arrays/Tuple.java)
4343
- [X] [ArrayList Implementation](../master/src/com/deepak/data/structures/Arrays/CustomArrayList.java)
4444

4545
**3. LinkedList**
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
* Data-Structures-In-Java
3+
* Tuple.java
4+
*/
5+
package com.deepak.data.structures.Arrays;
6+
7+
/**
8+
* Tuple class is an extension of Pair
9+
* NOTE : Tuple can have any number of elements.
10+
* We are implementing this class as a Triplet for now
11+
*
12+
* @author Deepak
13+
*
14+
* @param <L>
15+
* @param <M>
16+
* @param <R>
17+
*/
18+
public class Tuple<L, M , R> {
19+
20+
/* Since Tuple as well is a immutable data structure,
21+
* All the elements are marked as final */
22+
private final L left;
23+
private final M middle;
24+
private final R right;
25+
26+
/**
27+
* Constructor
28+
*
29+
* @param left
30+
* @param middle
31+
* @param right
32+
*/
33+
public Tuple(L left, M middle, R right) {
34+
super();
35+
this.left = left;
36+
this.middle = middle;
37+
this.right = right;
38+
}
39+
40+
/**
41+
* Method to create a new Tuple
42+
*
43+
* @param left
44+
* @param middle
45+
* @param right
46+
* @return {@link Tuple<L, M, R>}
47+
*/
48+
public static <L, M, R> Tuple<L, M, R> of(final L left, final M middle, final R right) {
49+
return new Tuple<L, M, R>(left, middle, right);
50+
}
51+
52+
/**
53+
* Method to get Left
54+
*
55+
* @return {@link L}
56+
*/
57+
public L getLeft() {
58+
return left;
59+
}
60+
61+
/**
62+
* Method to get Middle
63+
*
64+
* @return {@link M}
65+
*/
66+
public M getMiddle() {
67+
return middle;
68+
}
69+
70+
/**
71+
* Method to get Right
72+
*
73+
* @return {@link R}
74+
*/
75+
public R getRight() {
76+
return right;
77+
}
78+
79+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Data-Structures-In-Java
3+
* TupleTest.java
4+
*/
5+
package com.deepak.data.structures.Arrays;
6+
7+
import org.junit.Assert;
8+
import org.junit.Test;
9+
10+
/**
11+
* Test cases for Tuple
12+
*
13+
* @author Deepak
14+
*/
15+
public class TupleTest {
16+
17+
/**
18+
* Method to test Tuple
19+
*/
20+
@Test
21+
public void testTuple() {
22+
/* Create Tuple with two possible ways */
23+
Tuple<String, String, String> tuple1 = new Tuple<>("A", "B", "C");
24+
Tuple<String, String, String> tuple2 = Tuple.of("A", "B", "C");
25+
Assert.assertEquals(tuple1.getLeft(), tuple2.getLeft());
26+
Assert.assertEquals(tuple1.getMiddle(), tuple2.getMiddle());
27+
Assert.assertEquals(tuple1.getRight(), tuple2.getRight());
28+
29+
/* Create a Tuple with null values */
30+
Tuple<String, String, String> tuple3 = new Tuple<>(null, null, null);
31+
Assert.assertEquals(tuple3.getLeft(), null);
32+
Assert.assertEquals(tuple3.getMiddle(), null);
33+
Assert.assertEquals(tuple3.getRight(), null);
34+
}
35+
36+
}

0 commit comments

Comments
 (0)