code_junkie

Does Java need tuples

Nov 13th, 2011
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. a, b = method();
  2.  
  3. return a, b;
  4.  
  5. Object[] results = method();
  6.  
  7. a, b = results;
  8.  
  9. Tuple key = new Tuple(keyPart1, keyPart2, ..., keyPartN);
  10. hashMap.put(key, value);
  11.  
  12. public Map.Entry<String, String> tuple(String key, String value) {
  13. Map<String, String> map = new HashMap<String, String>();
  14. map.put(key, value);
  15. return map.entrySet().iterator().next();
  16. }
  17.  
  18. // Constructor from components
  19. public Tuple2(A item1Init, B item2Init)
  20. {
  21. item1 = item1Init;
  22. item2 = item2Init;
  23. }
  24.  
  25. // Constructor from another Tuple2 with the same types:
  26. public Tuple2(Tuple2 <A, B> anotherTuple2)
  27. {
  28. item1 = anotherTuple2.item1;
  29. item2 = anotherTuple2.item2;
  30. }
  31.  
  32. // No getters/setters are needed/possible because the components are public/final.
  33.  
  34. public class MyClass {
  35.  
  36. private Integer intField;
  37. private String stringField;
  38. private Date dateField;
  39.  
  40. public MyClass(Integer intField, String stringField, Date dateField) {
  41. this.intField = intField;
  42. this.stringField = stringField;
  43. this.dateField = dateField;
  44. }
  45.  
  46. public Date getDateField() {
  47. return dateField;
  48. }
  49.  
  50. public void setDateField(Date dateField) {
  51. this.dateField = dateField;
  52. }
  53.  
  54. public Integer getIntField() {
  55. return intField;
  56. }
  57.  
  58. public void setIntField(Integer intField) {
  59. this.intField = intField;
  60. }
  61.  
  62. public String getStringField() {
  63. return stringField;
  64. }
  65.  
  66. public void setStringField(String stringField) {
  67. this.stringField = stringField;
  68. }
  69.  
  70. }
  71.  
  72. public case class Myclass(Integer intField, String stringField, Date dateField);
  73.  
  74. public class TuplePair<X, Y, Z> {
  75.  
  76. private final X first;
  77. private final Y second;
  78. private final Z third;
  79.  
  80. public TuplePair(X f, Y s, Z z) {
  81. first = f;
  82. second = s;
  83. third = z;
  84. }
  85. ...
  86. ...
  87. }
  88.  
  89. Map.Entry<String, Boolean> tuple = new AbstractMap.SimpleEntry<String, Boolean>("test",false);
  90.  
  91. class Tuple {
  92. Object[] list;
  93. Tuple(Object... value) {
  94. list = value;
  95. }
  96. Object[] get() {
  97. return list;
  98. }
  99. }
  100.  
  101. val1, val2 = obj.method()
Add Comment
Please, Sign In to add comment