รู้เบื้องต้นเกี่ยวกับ Javatuples

1. ภาพรวม

ทูเพิลคือชุดขององค์ประกอบหลายอย่างที่อาจเกี่ยวข้องกันหรือไม่ก็ได้ กล่าวอีกนัยหนึ่งทูเปิลถือได้ว่าเป็นวัตถุที่ไม่ระบุชื่อ

ตัวอย่างเช่น [“ RAM”, 16,“ Astra”] คือทูเพิลที่มีองค์ประกอบสามอย่าง

ในบทความนี้เราจะต้องดูอย่างรวดเร็วที่ห้องสมุดง่ายจริงๆที่ช่วยให้เราทำงานร่วมกับ tuple ตามโครงสร้างข้อมูลชื่อjavatuples

2. คลาสJavatuplesในตัว

ไลบรารีนี้มีคลาสที่แตกต่างกันสิบคลาสซึ่งเพียงพอกับความต้องการส่วนใหญ่ของเราที่เกี่ยวข้องกับทูเพิล

  • หน่วย
  • คู่
  • Triplet
  • ควอเตท
  • Quintet
  • เซ็กส์
  • Septet
  • Octet
  • Ennead
  • ทศวรรษ

นอกจากคลาสข้างต้นแล้วยังมีคลาสเพิ่มเติมอีก 2 คลาสคือKeyValueและLabelValueซึ่งให้ฟังก์ชันการทำงานคล้ายกับคู่แต่มีความหมายแตกต่างกัน

ตามเว็บไซต์อย่างเป็นทางการ, การเรียนทั้งหมดในjavatuples เป็น typesafe และเปลี่ยนรูป แต่ละชั้นดำเนิน tuple Iterable , Serializableและเทียบเคียงกับอินเตอร์เฟซ

3. การเพิ่มการพึ่งพา Maven

มาเพิ่มการพึ่งพา Maven ในpom.xmlของเรา:

 org.javatuples javatuples 1.2 

โปรดตรวจสอบที่เก็บ Central Maven สำหรับเวอร์ชันล่าสุด

4. การสร้าง Tuples

การสร้างทูเพิลนั้นง่ายมาก เราสามารถใช้ตัวสร้างที่เกี่ยวข้อง:

Pair pair = new Pair("A pair", 55);

นอกจากนี้ยังมีวิธีการสร้างทูเพิลที่ละเอียดน้อยและมีความหมายน้อยกว่าเล็กน้อย:

Triplet triplet = Triplet.with("hello", 23, 1.2);

นอกจากนี้เรายังสามารถสร้าง tuples จากIterable :

List listOfNames = Arrays.asList("john", "doe", "anne", "alex"); Quartet quartet = Quartet.fromCollection(collectionOfNames);

โปรดทราบว่าจำนวนของรายการในคอลเลกชันที่ควรจะตรงกับประเภทของ tuple ที่เราต้องการที่จะสร้าง ตัวอย่างเช่นเราไม่สามารถสร้างQuintetโดยใช้คอลเลกชันด้านบนได้เนื่องจากต้องใช้องค์ประกอบห้าอย่าง เดียวกันเป็นจริงสำหรับระดับ tuple อื่น ๆ ที่มีการสั่งซื้อสูงกว่ากลุ่ม

อย่างไรก็ตามเราสามารถสร้างทูเพิลลำดับที่ต่ำกว่าเช่นPairหรือTripletโดยใช้คอลเลกชันด้านบนโดยระบุดัชนีเริ่มต้นในเมธอด fromIterable () :

Pair pairFromList = Pair.fromIterable(listOfNames, 2);

โค้ดด้านบนจะส่งผลให้สร้างคู่ที่มี " anne " และ " alex "

Tuples สามารถสร้างได้อย่างสะดวกจากอาร์เรย์ใด ๆ เช่นกัน:

String[] names = new String[] {"john", "doe", "anne"}; Triplet triplet2 = Triplet.fromArray(names);

5. รับค่าจากสิ่งทอ

ทุกคลาสในjavatuplesมีเมธอดgetValueX ()สำหรับรับค่าจากทูเปิลโดยที่Xระบุลำดับขององค์ประกอบภายในทูเปิล เช่นเดียวกับดัชนีในอาร์เรย์ค่าของXเริ่มต้นจากศูนย์

มาสร้าง Quartet ใหม่และดึงค่าบางอย่าง:

Quartet quartet = Quartet.with("john", 72.5, 32, "1051 SW"); String name = quartet.getValue0(); Integer age = quartet.getValue2(); assertThat(name).isEqualTo("john"); assertThat(age).isEqualTo(32);

อย่างที่เราเห็นตำแหน่งของ“ จอห์น ” คือศูนย์“ 72.5 ” คือหนึ่งและอื่น ๆ

โปรดทราบว่าเมธอดgetValueX ()เป็นประเภทที่ปลอดภัย นั่นหมายความว่าไม่จำเป็นต้องมีการหล่อ

อีกทางเลือกหนึ่งคือเมธอดgetValue (int pos) ใช้ตำแหน่งที่เป็นศูนย์ขององค์ประกอบเพื่อดึงข้อมูล วิธีนี้ไม่ปลอดภัยและต้องมีการหล่ออย่างชัดเจน :

Quartet quartet = Quartet.with("john", 72.5, 32, "1051 SW"); String name = (String) quartet.getValue(0); Integer age = (Integer) quartet.getValue(2); assertThat(name).isEqualTo("john"); assertThat(age).isEqualTo(32);

โปรดทราบว่าการเรียนKeyValueและLabelValueมีวิธีการที่สอดคล้องกันของพวกเขาgetKey () / getValue ()และgetLabel () / getValue ()

6. การตั้งค่าให้กับทูเปิล

คล้ายกับgetValueX ()ชั้นเรียนทั้งหมดใน javatuples มีsetAtX ()วิธีการ อีกครั้งXคือตำแหน่งที่อิงศูนย์สำหรับองค์ประกอบที่เราต้องการตั้งค่า:

Pair john = Pair.with("john", 32); Pair alex = john.setAt0("alex"); assertThat(john.toString()).isNotEqualTo(alex.toString());

สิ่งสำคัญที่นี่คือประเภทผลตอบแทนของเมธอด setAtX ()คือชนิดทูเพิลเอง เพราะนี่คือjavatuples จะไม่เปลี่ยนรูป การตั้งค่าใหม่จะทำให้อินสแตนซ์เดิมไม่เสียหาย

7. การเพิ่มและการลบองค์ประกอบจากสิ่งทอ

เราสามารถเพิ่มองค์ประกอบใหม่ ๆ ให้กับสิ่งทอ อย่างไรก็ตามสิ่งนี้จะส่งผลให้มีการสร้างทูเพิลใหม่ของหนึ่งคำสั่งที่สูงขึ้น:

Pair pair1 = Pair.with("john", 32); Triplet triplet1 = pair1.add("1051 SW"); assertThat(triplet1.contains("john")); assertThat(triplet1.contains(32)); assertThat(triplet1.contains("1051 SW"));

มันเป็นที่ชัดเจนจากตัวอย่างข้างต้นว่าการเพิ่มองค์ประกอบหนึ่งไปยังคู่จะสร้างใหม่Triplet ในทำนองเดียวกันการเพิ่มองค์ประกอบหนึ่งไปยังTripletจะสร้างใหม่สี่

The example above also demonstrates the use of contains() method provided by all the classes in javatuples. This is a really handy method for verifying if the tuple contains a given value.

It is also possible to add one tuple to another using the add() method:

Pair pair1 = Pair.with("john", 32); Pair pair2 = Pair.with("alex", 45); Quartet quartet2 = pair1.add(pair2); assertThat(quartet2.containsAll(pair1)); assertThat(quartet2.containsAll(pair2));

Note the use of containsAll() method. It will return true if all the elements of pair1 are present in quartet2.

By default, the add() method adds the element as a last element of the tuple. However, it is possible to add the element at a given position using addAtX() method, where X is the zero-based position where we want to add the element:

Pair pair1 = Pair.with("john", 32); Triplet triplet2 = pair1.addAt1("1051 SW"); assertThat(triplet2.indexOf("john")).isEqualTo(0); assertThat(triplet2.indexOf("1051 SW")).isEqualTo(1); assertThat(triplet2.indexOf(32)).isEqualTo(2);

This example adds the String at position 1, which is then verified by the indexOf() method. Please note the difference in order of the types for the Pair and the Triplet after the call to addAt1() method call.

We can also add multiple elements using any of add() or addAtX() methods:

Pair pair1 = Pair.with("john", 32); Quartet quartet1 = pair1.add("alex", 45); assertThat(quartet1.containsAll("alex", "john", 32, 45));

In order to remove an element from the tuple, we can use the removeFromX() method. Again, X specifies the zero-based position of the element to be removed:

Pair pair1 = Pair.with("john", 32); Unit unit = pair1.removeFrom0(); assertThat(unit.contains(32));

8. Converting Tuples to List/Array

We have already seen how to convert a List to a tuple. Let's now see hot to convert a tuple to a List:

Quartet quartet = Quartet.with("john", 72.5, 32, "1051 SW"); List list = quartet.toList(); assertThat(list.size()).isEqualTo(4);

It is fairly simple. The only thing to note here is that we will always get a List, even if the tuple contains the same type of elements.

Finally, let's convert the tuple to an array:

Quartet quartet = Quartet.with("john", 72.5, 32, "1051 SW"); Object[] array = quartet.toArray(); assertThat(array.length).isEqualTo(4);

Clear enough, the toArray() method always returns an Object[].

9. Conclusion

ในบทความนี้เราได้สำรวจไลบรารี javatuples และสังเกตเห็นความเรียบง่าย ให้ความหมายที่หรูหราและใช้งานง่ายมาก

ตรวจสอบให้แน่ใจว่าคุณได้ตรวจสอบซอร์สโค้ดที่สมบูรณ์สำหรับบทความนี้บน GitHub ซอร์สโค้ดที่สมบูรณ์มีตัวอย่างมากกว่าตัวอย่างเล็กน้อยที่กล่าวถึงที่นี่ หลังจากอ่านบทความนี้ตัวอย่างเพิ่มเติมควรจะเข้าใจได้ง่าย