Table of contents
1. The usage scenarios of collections under the Collection series
2. The role of variable parameters
3. Precautions for variable parameters
Third, the collection tool class Collections
1. Collections collection tool class
2. API s commonly used in Collections
3. Collections sorting related API
4. Comprehensive Cases of Collection System
1. The usage scenarios of collections under the Collection series
1. If you want the elements to be repeatable and have an index, should the index query be faster?
Use ArrayList collections, based on arrays. (most used)
2. If you want the elements to be repeatable and have an index, is it faster to add or delete the beginning and end?
Use LinkedList collection, based on linked list.
3. If you want to add, delete, modify and search quickly, but the elements are not repeated, disordered, and indexed.
Use HashSet collection, based on hash table.
4. If you want to add, delete, modify and search quickly, but the elements are not repeated, ordered, and have no index.
Use LinkedHashSet collection, based on hash table and double linked list.
5. If you want to sort objects.
Collection with TreeSet, based on red-black trees. Subsequent sorting can also be achieved using the List collection.
2. Variable parameters
1. Variable parameters
Variable parameters are used in formal parameters to receive multiple data.
Format of variadic parameters: datatype...parameter name.
2. The role of variable parameters
Receiving parameters is very flexible and convenient. It can receive no parameters, it can receive one or more parameters, or it can receive an array.
A variadic parameter is essentially an array inside a method.
3. Precautions for variable parameters
3.1. There can only be one variable parameter in a formal parameter list.
3.2. Variable parameters must be placed at the end of the formal parameter list.
4. Case
4.1. Requirements
If you need to define a method for summation, this method can flexibly fulfill the following requirements:
Calculate the sum of 1 data.
Calculate the sum of 2 data.
Calculate the sum of 3 numbers.
Calculate the sum of n data, and even support calling without parameters.
4.2. Code
public static void main(String[] args) { sum(); //1. Do not pass parameters sum(10); //2. Pass a parameter sum(10, 20, 30, 40); //3. Pass multiple parameters sum(new int[]{10, 20, 30, 40}); //4. Pass an array } /** Note: There can only be one variable parameter in a formal parameter list, and the variable parameter must be placed at the end of the parameter list * @param sums */ public static void sum(int...sums){ //Note: The variable parameter is actually an array inside the method. nums System.out.println("element length"+sums.length); System.out.println("element content"+Arrays.toString(sums)); }
4.3. Results
Third, the collection tool class Collections
1. Collections collection tool class
java.utils.Collections: is a collection utility class
Role: Collections do not belong to collections, but are tool classes used to manipulate collections.
2. API s commonly used in Collections
method name | illustrate |
public static <T> boolean addAll(Collection<? super T> c, T... elements) | Add elements to a collection object in batches |
public static void shuffle(List<?> list) | Shuffle the order of List collection elements |
3. Collections sorting related API
Scope of use: Only for the sorting of the List collection.
Sort by 1:
method name | illustrate |
public static <T> void sort(List<T> list) | Sort the elements in the collection according to the default rules |
Note: This method cannot directly sort the List collection of the custom type, unless the custom type implements the comparison rule Comparable interface.
Sort by 2:
method name | illustrate |
public static <T> void sort(List<T> list,Comparator<? super T> c) | Sort the elements in the collection according to the specified rules |
4. Comprehensive Cases of Collection System
1. Demand
Landlord game: When starting the game room, you should prepare 54 cards in advance to complete shuffling, dealing, sorting and logic.
2. Analysis
2.1: When the system needs to prepare data at the same time when the system starts, you can use static code blocks.
2.2: Shuffle is to shuffle the order of the cards.
2.3: Define three players and deal 51 cards in sequence
2.4: Sort the player's cards
2.5: Output the card data of each player.
3. Code
3.1. Define the entity class Card
public class Card { private String size; private String colors; private int index; public Card(){ } public Card(String size, String colors, int index) { this.size = size; this.colors = colors; this.index = index; } public String getSize() { return size; } public void setSize(String size) { this.size = size; } public String getColors() { return colors; } public void setColors(String colors) { this.colors = colors; } public int getIndex() { return index; } public void setIndex(int index) { this.index = index; } @Override public String toString() { return size + colors; } }
3.2, main function
public class GameDemo { //1. Define a static collection to store 54 card objects public static List<Card> allCards = new ArrayList<>(); //2. Make a card: define a static code block to initialize the card data static { //3. Define the number of points: the number is determined, the type is determined, and an array is used String[] sizes = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "1", "2"}; //4. Define the suit: the number is determined, the type is determined, and an array is used String[] colors = {"♥", "♦", "♣", "♠"}; //5. Combining points and suits int index = 0; for (String size : sizes) { index++; for (String color : colors) { //6, packaged into a card object Card c = new Card(size , color, index); //7. Store in the collection container allCards.add(c); } } //8. The big and small kings are stored in the collection Card c1 = new Card("","little king",++index); Card c2 = new Card("","king",++index); Collections.addAll(allCards,c1,c2); System.out.println("New card:"+allCards); } public static void main(String[] args) { //9. Shuffle Collections.shuffle(allCards); System.out.println("After shuffling:"+allCards); //10. Dealing cards (define three players, each player's card is also a collection container) List<Card> linhuchong = new ArrayList<>(); List<Card> jiumozhi = new ArrayList<>(); List<Card> renyingying = new ArrayList<>(); //11. Start dealing cards (51 cards are dealt to three players from the collection, and the remaining 3 cards are used as hole cards) //allCards = : [K♠, 7♦, 3♣, 5♠, 3♠, 10♥........ // i 0 1 2 3 4 5 for (int i = 0; i < allCards.size() - 3; i++) { //Get the card first Card c = allCards.get(i); if (i % 3 == 0){ //Ah Chong linhuchong.add(c); }else if (i % 3 == 1){ //Ah Dove takes the card jiumozhi.add(c); }else if (i % 3 == 2){ //Yingying takes over renyingying.add(c); } } //12. Get the last three cards (cut the last three cards into a subset) List<Card> lastThreeCards = allCards.subList(allCards.size() - 3, allCards.size()); //13. Sort the player's cards (from high to low) sortCard(linhuchong); sortCard(jiumozhi); sortCard(renyingying); //14. Print out their cards System.out.println(linhuchong); System.out.println(jiumozhi); System.out.println(renyingying); System.out.println(lastThreeCards); } public static void sortCard(List<Card> c){ // Collections.sort(c, new Comparator<Card>() { // @Override // public int compare(Card o1, Card o2) { // return o2.getIndex() - o1.getIndex(); // } // }); Collections.sort(c, ( o1, o2) ->o2.getIndex() - o1.getIndex() ); } }