Collective exercises for landlords licensing and landlords
The underlying data structure of the HashMap key is a hash table.
The underlying data structure of ArrayList is an array, which provides fast query and slow addition and deletion.
TreeSet bottom-layer data structure tree (balanced binary tree), default constructor adopts natural sorting (Integer has implemented the Comparable interface ).
Package cn. itcast. day18; import java. util. arrayList; import java. util. collections; import java. util. hashMap; import java. util. treeSet;/** landlord Licensing Program ** A: Create A HashMap <Integer, String> to store the brand number and card number. * B: Create a color array and a number array (3 min) * B: Create an ArrayList to store the number * C: place the number and card in the corresponding HashMap, at the same time, place the license plate number in the ArrayList * C: shuffling (card washing) * D: licensing, traversing the license plate number in the ArrayList, picking the corresponding card in HashMap, and storing the card with TreeSet. * E: traverse each TreeSet to get the corresponding person's card */public class PokerDemo {public static void main (String [] args) {// create the card core HashMap <Integer, string> hm = new HashMap <Integer, String> (); // create the ArrayList of the brand box <Integer> array = new ArrayList <Integer> (); // create a color array String [] colors = {"♢","♧"," "," "}; // Create point array String [] numbers = {" 3 "," 4 "," 5 "," 6 "," 7 ", "8", "9", "10", "J", "Q", "K", "A", "2 "}; // store the grades and cards in HashMap and the grades in ArrayList into int index = 0; for (String number: numbers) {for (String color: colors) {String s = color. concat (number); hm. put (index, s); array. add (index); index ++ ;}} hm. put (index, ""); array. add (index); index ++; hm. put (index, "John"); array. add (index); // shuffling Collections. shuffle (array); // use TreeSet to store TreeSet <Integer> sunWuKong = new TreeSet <Integer> (); TreeSet <Integer> zhuBaJie = new TreeSet <Integer> () during licensing (); treeSet <Integer> shaHeShang = new TreeSet <Integer> (); TreeSet <Integer> diPai = new TreeSet <Integer> (); // traverses the ArrayList of washed brands, concurrency number for (Integer I: array) {if (array. size ()-I <= 3) {diPai. add (I);} else if (I % 3 = 2) {sunWuKong. add (I);} else if (I % 3 = 1) {zhuBaJie. add (I);} else if (I % 3 = 0) {shaHeShang. add (I) ;}} lookPoker ("Sun Wukong", sunWuKong, hm); lookPoker ("", zhuBaJie, hm); lookPoker ("Sha Monk", shaHeShang, hm); lookPoker ("card", diPai, hm);} public static void lookPoker (String name, TreeSet <Integer> pai, HashMap <Integer, String> hm) {System. out. print (name + ":"); for (Integer I: pai) {System. out. print (hm. get (I) + "");} System. out. println ();}}
/*
Sun Wukong's card is: 3♧4♢5 5 6♧7♢8 8 9♧10♢J Q♧K♢A 2
The cards of pig Bajie are:♧3♢4 4 5♧6♢7 7 8♧9♢10 10 J♧Q♢K♧2
Sha monk's card is:♢3 3 4♧5♢6 6 7♧8♢9 9 10♧J♢Q K♧A♢2
The card is: King of 2
*/