LinkHash**特性:唯一和有序
由【哈希表】保证【键】的唯一性,由【链表】保证【键】的有序性(存储和取出的顺序一致) 【LinkHashSet】和【LinkHashMap key】的特性是完全一样的! public class Test { public static void main(String[] args) { LinkedHashMap<Person, String> linkedHashMap = new LinkedHashMap<Person, String>(); linkedHashMap.put(new Person("ea", 150), "包青天"); linkedHashMap.put(new Person("ab", 27), "包青天"); linkedHashMap.put(new Person("c", 150), "包青天"); linkedHashMap.put(new Person("c", 160), "baiqiantao"); linkedHashMap.put(new Person("ea", 160), "baiqiantao"); linkedHashMap.put(new Person("d", 27), "包青天"); System.out.println(linkedHashMap);//{ea,150=baiqiantao, ab,27=包青天, c,150=baiqiantao, d,27=包青天} } }
class Person { public String name; public int age; public Person(String name, int age) { this.name = name; this.age = age; } public int hashCode() { int newAge = this.age; if (age > 150) newAge = 150; return name.hashCode() + newAge;//这里的hashCode是基于String类的hashCode更改的 } public boolean equals(Object obj) { if (this == obj) return true; if (!(obj instanceof Person)) return false; Person p = (Person) obj; return this.name.equals(p.name);//只要name相同就认为equal值为true } public String toString() { //默认返回值是【person.getClass().getName() + ‘@‘ + Integer.toHexString(person.hashCode())】 return name + "," + age; } }
Collection案例:模拟斗地主的牌局
public class Test { public static void main(String[] args) { // 创建一个牌盒 List<PokerBean> list = new ArrayList<PokerBean>(); String[] colors = { "?", "?", "?", "?" }; String[] numbers = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" }; // 装牌 for (int i = 0; i < numbers.length; i++) { for (String color : colors) { list.add(new PokerBean(color, numbers[i], i + 1)); } } list.add(new PokerBean("小王", "小王", 14)); list.add(new PokerBean("大王", "小王", 15)); // 洗牌 Collections.shuffle(list); // 发牌 Set<PokerBean> player_1 = new TreeSet<PokerBean>(); Set<PokerBean> player_2 = new TreeSet<PokerBean>(); Set<PokerBean> player_3 = new TreeSet<PokerBean>(); Set<PokerBean> diPai = new TreeSet<PokerBean>(); for (int x = 0; x < list.size(); x++) { if (x >= list.size() - 3) diPai.add(list.get(x)); else if (x % 3 == 0) player_1.add(list.get(x)); else if (x % 3 == 1) player_2.add(list.get(x)); else if (x % 3 == 2) player_3.add(list.get(x)); } //看牌 System.out.println("玩家一的牌为:" + player_1); System.out.println("玩家二的牌为:" + player_2); System.out.println("地主家的牌为:" + player_3); System.out.println("底牌为:" + diPai); } }
class PokerBean implements Comparable<PokerBean> { public String color; public String numberString; public int numberInt; public PokerBean(String color, String numberString, int numberInt) { this.color = color; this.numberString = numberString; this.numberInt = numberInt; } @Override public String toString() { switch (numberInt) { case 10: return "[" + color + numberString + "]";//没有空格 case 14: case 15: return "[" + numberString + "]";//没有空格,没有数字 default: return "[ " + color + numberString + " ]"; } } @Override public int compareTo(PokerBean o) { int tem = this.numberInt - o.numberInt; return tem == 0 ? this.color.compareTo(o.color) : tem; } }
TreeMap案例:统计字符出现的次数
public class Test { public static void main(String[] args) { String str = "fd*+a//dA11 \n aDdD-d"; System.out.println(getcharCount(str)); }
// 获取字符串中每一个字母出现的次数,要求打印结果形式为:a-2 \n b-1 形式 public static String getcharCount(String str) { TreeMap<Character, Integer> map = new TreeMap<Character, Integer>(); char[] arr = str.toCharArray(); for (int i = 0; i < arr.length; i++) { if (!(arr[i] <= ‘z‘ && arr[i] >= ‘a‘ || arr[i] <= ‘Z‘ && arr[i] >= ‘A‘)) //限制查询范围 continue;//表示不再执行后面的代码,继续下次遍历! Integer value = map.get(arr[i]);//Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key. if (value == null) map.put(arr[i], 1);//若此字符不存在,则存入此字符,将值设为1 else map.put(arr[i], value + 1);//若此字符已经存在,则值加1 } return mapToString(map); }
//将集合中的内容以指定格式的字符串形式返回 private static <K, V> String mapToString(Map<K, V> map) { StringBuilder sb = new StringBuilder(); Iterator<K> it = map.keySet().iterator(); while (it.hasNext()) { K key = it.next(); V value = map.get(key); sb.append(key + "-" + value + "\n"); } return sb.toString(); } }
时间: 2024-12-14 03:38:04