![หยุดเขียนซ้ำ! แนวทางปฏิบัติที่ดีที่สุด 10 อันดับแรกสำหรับการทำงานกับคอลเลกชันใน Java 8 - 1]()
อย่างที่คุณทราบ นิสัยของเราเป็นธรรมชาติที่สอง และเมื่อคุณคุ้นเคยกับการเขียนแล้ว
for (int i = 0; i <......)
ไม่มีส่วนใดของคุณต้องการเรียนรู้โครงสร้างนี้ใหม่ (โดยเฉพาะอย่างยิ่งเนื่องจากมันค่อนข้างง่ายและเข้าใจได้) อย่างไรก็ตาม ลูปมักจะถูกใช้ซ้ำๆ เพื่อดำเนินการขั้นพื้นฐานแบบเดียวกัน และการวนซ้ำเป็นสิ่งที่เราอยากจะกำจัดทิ้งไป ด้วย Java 8 Oracle ได้ตัดสินใจที่จะช่วยเราในเรื่องนี้ ด้านล่างนี้เป็นวิธีการรวบรวมที่ดีที่สุด 10 วิธีที่จะช่วยคุณประหยัดเวลาและรหัสได้มาก
1. Iterable.forEach(การกระทำของผู้บริโภค <? Super T>)
ชื่อพูดสำหรับตัวเอง มันวนซ้ำคอลเลกชันที่ส่งผ่านเป็นอาร์กิวเมนต์ และดำเนินการนิพจน์แลมบ์ดาสำหรับแต่ละองค์ประกอบ
List <Integer> numbers = new ArrayList<>(Arrays.asList(1,2,3,4,5,6,7));
numbers.forEach(s -> System.out.print(s + " "));
1 2 3 4 5 6 7
2. Collection.removeIf (เพรดิเคต <? super E> ตัวกรอง)
อีกครั้งไม่มีอะไรยากที่นี่ เมธอดจะวนซ้ำคอลเล็กชันและลบองค์ประกอบใดๆ ที่
filter
ตรงกัน
List <Integer> numbers = new ArrayList<>(Arrays.asList(1,2,3,4,5,6,7));
numbers.removeIf(s -> s > 5);
numbers.forEach(s -> System.out.print(s + " "));
ในบรรทัดเดียว เราจะลบตัวเลขทั้งหมดที่มากกว่า 5 ออกจากรายการ
3. Map.forEach(BiConsumer <? super K, ? super V> การกระทำ)
เมธอด นี้
forEach
ใช้ไม่ได้เฉพาะกับคลาสที่ใช้
Collection
อินเทอร์เฟซเท่านั้น แต่ยังใช้กับ
Map
.
Map <String, String> books = new HashMap<>();
books.put("War and Peace", "Leo Tolstoy");
books.put("Crime and Punishment", "Fyodor Dostoevsky");
books.put("Thinking in Java", "Bruce Eckel");
books.put("The Brothers Karamazov", "Fyodor Dostoevsky");
books.put("The Lord of the Rings", "John Tolkien");
books.forEach((a,b) -> System.out.println("Book title: " + a + ". Author: "+ b));
Book title: The Brothers Karamazov. Author: Fyodor Dostoevsky
Book title: Thinking in Java. Author: Bruce Eckel
Book title: Crime and Punishment. Author: Fyodor Dostoevsky
Book title: War and Peace. Author: Leo Tolstoy
Book title: Lord of the Rings. Author: John Tolkien
4. Map.compute (คีย์ K, BiFunction<? Super K,? Super V,? Extends V> remappingFunction)
ดูน่ากลัวกว่าเล็กน้อย แต่จริงๆแล้วเรียบง่ายเหมือนก่อนหน้านี้ทั้งหมด วิธีนี้ตั้ง
key
ค่าเท่ากับผลลัพธ์ของการดำเนิน
mappingFunction
การ ตัวอย่างเช่น:
Map <String, String> books = new HashMap<>();
books.put("War and Peace", "Leo Tolstoy");
books.put("Crime and Punishment", "Fyodor Dostoevsky");
books.put("Thinking in Java", "Bruce Eckel");
books.put("The Brothers Karamazov", "Fyodor Dostoevsky");
books.put("The Lord of the Rings", "John Tolkien");
books.forEach((a,b) -> System.out.println("Book title: " + a + ". Author: "+ b));
books.compute("Thinking in Java", (a,b) -> b + ", cool dude");
System.out.println("_______________________");
books.forEach((a,b) -> System.out.println("Book title: " + a + ". Author: "+ b));
Book title: The Brothers Karamazov. Author: Fyodor Dostoevsky
Book title: Thinking in Java. Author: Bruce Eckel
Book title: Crime and Punishment. Author: Fyodor Dostoevsky
Book title: War and Peace. Author: Leo Tolstoy
Book title: Lord of the Rings. Author: John Tolkien
_______________________
Book title: The Brothers Karamazov. Author: Fyodor Dostoevsky
Book title: Thinking in Java. Author: Bruce Eckel, cool dude
Book title: Crime and Punishment. Author: Fyodor Dostoevsky
Book title: War and Peace. Author: Leo Tolstoy
Book title: Lord of the Rings. Author: John Tolkien
ผู้เขียน "Thinking in Java" เจ๋งมาก! :)
5. Map.computeIfAbsent(คีย์ K, ฟังก์ชัน <? super K, ? ขยาย V> mappingFunction)
วิธีนี้จะเพิ่มองค์ประกอบใหม่ให้กับ
Map
เฉพาะในกรณีที่ยังไม่มีองค์ประกอบที่มีคีย์นั้น ค่าที่กำหนดจะเป็นผลลัพธ์ของการดำเนิน
mappingFunction
การ หากมีองค์ประกอบที่มีคีย์อยู่แล้ว จะไม่ถูกเขียนทับ มันจะยังคงอยู่เหมือนเดิม กลับไปที่หนังสือของเราแล้วลองวิธีใหม่:
Map <String, String> books = new HashMap<>();
books.put("War and Peace", "Leo Tolstoy");
books.put("Crime and Punishment", "Fyodor Dostoevsky");
books.put("Thinking in Java", "Bruce Eckel");
books.put("The Brothers Karamazov", "Fyodor Dostoevsky");
books.put("The Lord of the Rings", "John Tolkien");
books.computeIfAbsent("Harry Potter and the Prisoner of Azkaban", b -> getHarryPotterAuthor());
books.forEach((a,b) -> System.out.println("Book title: " + a + ". Author: "+ b));
นี่คือ
mappingFunction
:
public static String getHarryPotterAuthor() {
return "Joanne Rowling";
}
และนี่คือหนังสือเล่มใหม่:
Book title: The Brothers Karamazov. Author: Fyodor Dostoevsky
Book title: Thinking in Java. Author: Bruce Eckel
Book title: Crime and Punishment. Author: Fyodor Dostoevsky
Book title: War and Peace. Author: Leo Tolstoy
Book title: Harry Potter and the Prisoner of Azkaban. Author: Joanne Rowling
Book title: Lord of the Rings. Author: John Tolkien
6. Map.computeIfPresent(ปุ่ม K, BiFunction<? super K, ? super V, ? ขยาย V> remappingFunction)
ที่นี่เรามีหลักการเดียวกันกับ
Map.compute()
แต่การคำนวณจะดำเนินการเฉพาะเมื่อมีรายการที่
key
มีอยู่แล้ว เท่านั้น
Map <String, String> books = new HashMap<>();
books.put("War and Peace", "Leo Tolstoy");
books.put("Crime and Punishment", "Fyodor Dostoevsky");
books.put("Thinking in Java", "Bruce Eckel");
books.put("The Brothers Karamazov", "Fyodor Dostoevsky");
books.put("The Lord of the Rings", "John Tolkien");
books.computeIfPresent("Eugene Onegin", (a,b) -> b = "Alexander Pushkin");
System.out.println("_________________");
books.forEach((a,b) -> System.out.println("Book title: " + a + ". Author: "+ b));
books.computeIfPresent("The Brothers Karamazov", (a,b) -> b = "Alexander Pushkin");
System.out.println("_________________");
books.forEach((a,b) -> System.out.println("Book title: " + a + ". Author: "+ b));
การเรียกใช้ฟังก์ชันครั้งแรกไม่มีการเปลี่ยนแปลงใดๆ เนื่องจากไม่มีหนังสือชื่อ "Eugene Onegin" ในไฟล์
Map
. แต่ในการโทรครั้งที่สองโปรแกรมได้เปลี่ยนผู้เขียนหนังสือ "The Brothers Karamazov" เป็น Alexander Pushkin เอาท์พุต:
_________________
Book title: The Brothers Karamazov. Author: Fyodor Dostoevsky
Book title: Thinking in Java. Author: Bruce Eckel
Book title: Crime and Punishment. Author: Fyodor Dostoevsky
Book title: War and Peace. Author: Leo Tolstoy
Book title: Lord of the Rings. Author: John Tolkien
_________________
Book title: The Brothers Karamazov. Author: Alexander Pushkin
Book title: Thinking in Java. Author: Bruce Eckel
Book title: Crime and Punishment. Author: Fyodor Dostoevsky
Book title: War and Peace. Author: Leo Tolstoy
Book title: Lord of the Rings. Author: John Tolkien
7. Map.getOrDefault (คีย์วัตถุ, V defaultValue)
เมธอดนี้ส่งคืนค่าที่สอดคล้อง
key
กับ ถ้าคีย์ไม่มีอยู่ มันจะส่งกลับค่าดีฟอลต์
Map <String, String> books = new HashMap<>();
books.put("War and Peace", "Leo Tolstoy");
books.put("Crime and Punishment", "Fyodor Dostoevsky");
books.put("Thinking in Java", "Bruce Eckel");
books.put("The Brothers Karamazov", "Fyodor Dostoevsky");
books.put("The Lord of the Rings", "John Tolkien");
String igor = books.getOrDefault("The Tale of Igor's Campaign", "Unknown author");
System.out.println(igor);
มันสะดวกมาก:
Unknown author
8. Map.merge(คีย์ K, ค่า V, BiFunction<? super V, ? super V, ? ขยาย V> remappingFunction)
ฉันไม่ต้องพยายามคำนวณว่าวิธีนี้จะช่วยคุณได้กี่บรรทัด
- หาก
key
ไม่มีอยู่ในของคุณMap
หรือถ้าvalue
สำหรับคีย์นี้คือnull
วิธีการจะเพิ่มkey-value
คู่ที่ส่งผ่านไปMap
ยัง
- หาก
key
มีอยู่และเมธอดvalue != null
จะเปลี่ยนค่าเป็นผลลัพธ์ของการดำเนินremappingFunction
การ
- หาก
remappingFunction
ส่งคืนnull
จะkey
ถูกลบออกจากคอลเล็กชัน
Map <String, String> books = new HashMap<>();
books.put("War and Peace", "Leo Tolstoy");
books.put("Crime and Punishment", "Fyodor Dostoevsky");
books.put("Thinking in Java", "Bruce Eckel");
books.put("The Brothers Karamazov", "Fyodor Dostoevsky");
books.put("The Lord of the Rings", "John Tolkien");
books.merge("Thinking in Java", "Bruce Eckel", (a, b) -> b + " and some coauthor");
books.forEach((a, b) -> System.out.println("Title: " + a + ". Author: "+ b));
เอาท์พุต:
Title: The Brothers Karamazov. Author: Fyodor Dostoevsky
Title: Thinking in Java. Author: Bruce Eckel and some coauthor
Title: Crime and Punishment. Author: Fyodor Dostoevsky
Title: War and Peace. Author: Leo Tolstoy
Title: Lord of the Rings. Author: John Tolkien
*ขอโทษบรูซ*
9. Map.putIfAbsent(คีย์ K, ค่า V)
ก่อนหน้านี้ หากต้องการเพิ่มคู่ให้กับ a
Map
หากยังไม่มี คุณต้องทำดังต่อไปนี้:
Map <String, String> map = new HashMap<>();
if (map.get("Lord of the Rings") == null)
map.put("Lord of the Rings", "John Tolkien");
ตอนนี้ทุกอย่างง่ายขึ้นมาก:
Map<String, String> map = new HashMap<>();
map.putIfAbsent("Lord of the Rings", "John Tolkien");
10. Map.replace และ Map.replaceAll()
สุดท้าย แต่ไม่ท้ายสุด.
Map.replace(K key, V newValue)
แทนที่key
ค่าของ ด้วยnewValue
ถ้ามีคีย์ดังกล่าวอยู่ ถ้าไม่ก็ไม่มีอะไรเกิดขึ้น
Map.replace(K key, V oldValue, V newValue)
ทำสิ่งเดียวกัน แต่ถ้าค่าปัจจุบันของkey
เท่ากับoldValue
Map.replaceAll(BiFunction<? super K, ? super V, ? extends V> function)
แทนที่แต่ละรายการvalue
ด้วยผลลัพธ์ของฟังก์ชัน
ตัวอย่างเช่น:
Map <String, String> books = new HashMap<>();
books.put("War and Peace", "Leo Tolstoy");
books.put("Crime and Punishment", "Fyodor Dostoevsky");
books.put("Thinking in Java", "Bruce Eckel");
books.put("The Brothers Karamazov", "Fyodor Dostoevsky");
books.put("The Lord of the Rings", "John Tolkien");
books.replace("The Brothers Karamazov", "Bruce Eckel", "John Tolkien");
books.forEach((a, b) -> System.out.println("Title: " + a + ". Author: "+ b));
Title: The Brothers Karamazov. Author: Fyodor Dostoevsky
Title: Thinking in Java. Author: Bruce Eckel
Title: Crime and Punishment. Author: Fyodor Dostoevsky
Title: War and Peace. Author: Leo Tolstoy
Title: Lord of the Rings. Author: John Tolkien
ไม่ได้ผล! ค่าปัจจุบันของคีย์ "The Brothers Karamazov" คือ "Fyodor Dostoevsky" ไม่ใช่ "Bruce Eckel" ดังนั้นจึงไม่มีอะไรเปลี่ยนแปลง
Map books = new HashMap<>();
books.put("War and Peace", "Leo Tolstoy");
books.put("Crime and Punishment", "Fyodor Dostoevsky");
books.put("Thinking in Java", "Bruce Eckel");
books.put("The Brothers Karamazov", "Fyodor Dostoevsky");
books.put("The Lord of the Rings", "John Tolkien");
books.replaceAll((a,b) -> getCoolAuthor());
books.forEach((a, b) -> System.out.println("Title: " + a + ". Author: "+ b));
public static String getCoolAuthor() {
return "Cool author";
}
Title: The Brothers Karamazov. Author: Cool author
Title: Thinking in Java. Author: Cool author
Title: Crime and Punishment. Author: Cool author
Title: War and Peace. Author: Cool author
Title: Lord of the Rings. Author: Cool author
เราเปลี่ยนค่าทั้งหมดได้อย่างง่ายดาย
Map
โดยไม่ต้องมีโครงสร้างที่ซับซ้อน! ป.ล. การทำความคุ้นเคยกับสิ่งใหม่นั้นยากเสมอ แต่การเปลี่ยนแปลงเหล่านี้เป็นสิ่งที่ดีจริงๆ ไม่ว่าในกรณีใด บางส่วนของรหัสของฉันไม่เหมือนสปาเก็ตตี้กว่าเดิมอย่างแน่นอน :) ขอให้โชคดีในการเรียนรู้!
GO TO FULL VERSION