1. ภาพรวม
การแปลงรายการเป็นแผนที่เป็นงานทั่วไป ในบทช่วยสอนนี้เราจะพูดถึงหลายวิธีในการดำเนินการนี้
เราจะคิดว่าองค์ประกอบของแต่ละรายการมีตัวบ่งชี้ซึ่งจะนำมาใช้เป็นหลักในการส่งผลให้หนึ่งแผนที่
2. โครงสร้างข้อมูลตัวอย่าง
ประการแรกเรามาสร้างแบบจำลององค์ประกอบ:
public class Animal { private int id; private String name; // constructor/getters/setters }
รหัสข้อมูลที่ไม่ซ้ำกันด้วยเหตุนี้เราสามารถทำให้มันเป็นกุญแจสำคัญ
เริ่มต้นการแปลงด้วยวิธีดั้งเดิม
3. ก่อน Java 8
เห็นได้ชัดว่าเราสามารถแปลงรายการเป็นแผนที่โดยใช้วิธีการหลักของ Java:
public Map convertListBeforeJava8(List list) { Map map = new HashMap(); for (Animal animal : list) { map.put(animal.getId(), animal); } return map; }
มาทดสอบการแปลงกัน:
@Test public void whenConvertBeforeJava8_thenReturnMapWithTheSameElements() { Map map = convertListService .convertListBeforeJava8(list); assertThat( map.values(), containsInAnyOrder(list.toArray())); }
4. ด้วย Java 8
เริ่มต้นด้วย Java 8 เราสามารถแปลงรายการเป็นแผนที่โดยใช้สตรีมและนักสะสม :
public Map convertListAfterJava8(List list) { Map map = list.stream() .collect(Collectors.toMap(Animal::getId, animal -> animal)); return map; }
อีกครั้งตรวจสอบให้แน่ใจว่าการแปลงเสร็จสมบูรณ์:
@Test public void whenConvertAfterJava8_thenReturnMapWithTheSameElements() { Map map = convertListService.convertListAfterJava8(list); assertThat( map.values(), containsInAnyOrder(list.toArray())); }
5. การใช้ห้องสมุด Guava
นอกจาก Java หลักแล้วเราสามารถใช้ไลบรารีของบุคคลที่สามสำหรับการแปลง
5.1. การกำหนดค่า Maven
ประการแรกเราต้องเพิ่มการอ้างอิงต่อไปนี้ในpom.xmlของเรา:
com.google.guava guava 23.6.1-jre
เวอร์ชันล่าสุดของไลบรารีนี้สามารถพบได้ที่นี่
5.2. การแปลงด้วยMaps.uniqueIndex ()
ประการที่สองให้ใช้Maps.uniqueIndex ()วิธีการแปลงรายการเป็นแผนที่ :
public Map convertListWithGuava(List list) { Map map = Maps .uniqueIndex(list, Animal::getId); return map; }
สุดท้ายมาทดสอบการแปลงกัน:
@Test public void whenConvertWithGuava_thenReturnMapWithTheSameElements() { Map map = convertListService .convertListWithGuava(list); assertThat( map.values(), containsInAnyOrder(list.toArray())); }
6. การใช้ Apache Commons Library
นอกจากนี้เรายังสามารถทำการแปลงด้วยไฟล์วิธีการของไลบรารี Apache Commons
6.1. การกำหนดค่า Maven
ประการแรกขอรวมการพึ่งพา Maven:
org.apache.commons commons-collections4 4.2
เวอร์ชันล่าสุดของการอ้างอิงนี้มีอยู่ที่นี่
6.2. MapUtils
ประการที่สองเราจะทำการแปลงโดยใช้MapUtils.populateMap () :
public Map convertListWithApacheCommons2(List list) { Map map = new HashMap(); MapUtils.populateMap(map, list, Animal::getId); return map; }
สุดท้ายตรวจสอบให้แน่ใจว่าทำงานได้ตามที่คาดไว้:
@Test public void whenConvertWithApacheCommons2_thenReturnMapWithTheSameElements() { Map map = convertListService .convertListWithApacheCommons2(list); assertThat( map.values(), containsInAnyOrder(list.toArray())); }
7. ความขัดแย้งของค่านิยม
ลองตรวจสอบว่าจะเกิดอะไรขึ้นหากฟิลด์idไม่ซ้ำกัน
7.1. รายการของสัตว์ด้วยซ้ำId s
ก่อนอื่นมาสร้างList of Animalด้วยid ที่ไม่ซ้ำกัน:
@Before public void init() { this.duplicatedIdList = new ArrayList(); Animal cat = new Animal(1, "Cat"); duplicatedIdList.add(cat); Animal dog = new Animal(2, "Dog"); duplicatedIdList.add(dog); Animal pig = new Animal(3, "Pig"); duplicatedIdList.add(pig); Animal cow = new Animal(4, "Cow"); duplicatedIdList.add(cow); Animal goat= new Animal(4, "Goat"); duplicatedIdList.add(goat); }
ตามที่ปรากฏข้างต้นวัวและแพะมีเดียวกันID
7.2. ตรวจสอบพฤติกรรม
Java Map‘s put() method is implemented so that the latest added value overwrites the previous one with the same key.
For this reason, the traditional conversion and Apache Commons MapUtils.populateMap() behave in the same way:
@Test public void whenConvertBeforeJava8_thenReturnMapWithRewrittenElement() { Map map = convertListService .convertListBeforeJava8(duplicatedIdList); assertThat(map.values(), hasSize(4)); assertThat(map.values(), hasItem(duplicatedIdList.get(4))); } @Test public void whenConvertWithApacheCommons_thenReturnMapWithRewrittenElement() { Map map = convertListService .convertListWithApacheCommons(duplicatedIdList); assertThat(map.values(), hasSize(4)); assertThat(map.values(), hasItem(duplicatedIdList.get(4))); }
As can be seen, the goat overwrites the cow with the same id.
Unlike that, Collectors.toMap() and MapUtils.populateMap() throw IllegalStateException and IllegalArgumentException respectively:
@Test(expected = IllegalStateException.class) public void givenADupIdList_whenConvertAfterJava8_thenException() { convertListService.convertListAfterJava8(duplicatedIdList); } @Test(expected = IllegalArgumentException.class) public void givenADupIdList_whenConvertWithGuava_thenException() { convertListService.convertListWithGuava(duplicatedIdList); }
8. Conclusion
ในบทความสั้น ๆ นี้เราได้กล่าวถึงวิธีต่างๆในการแปลงรายการเป็นแผนที่โดยให้ตัวอย่างกับ Java หลักและไลบรารีของบุคคลที่สามที่เป็นที่นิยม
ตามปกติซอร์สโค้ดที่สมบูรณ์จะพร้อมใช้งานบน GitHub