Student类:
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
HashSetTest类:
import java.util.HashSet;
public class HashSetTest {
public static void main(String[] args) {
HashSet<Student> hashSet = new HashSet<>();
Student student1 = new Student("张三", 20);
Student student2 = new Student("李四", 21);
Student student3 = new Student("王五", 22);
hashSet.add(student1);
hashSet.add(student2);
hashSet.add(student3);
for (Student student : hashSet) {
System.out.println(student.getName() + "---" + student.getAge());
}
}
}
(1)自然排序:
Student类:
public class Student implements Comparable<Student> {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public int compareTo(Student o) {
int num1 = this.getAge() - o.getAge();
int num2 = (num1 == 0) ? (this.getName().compareTo(o.getName())) : num1;
return num2;
}
}
TreeSetTest类:
import java.util.TreeSet;
public class TreeSetTest {
public static void main(String[] args) {
TreeSet<Student> treeSet = new TreeSet<>();
Student student1 = new Student("张三", 20);
Student student2 = new Student("张三", 22);
Student student3 = new Student("李四", 21);
Student student4 = new Student("王五", 22);
treeSet.add(student1);
treeSet.add(student2);
treeSet.add(student3);
treeSet.add(student4);
for (Student student : treeSet) {
System.out.println(student.getName() + "---" + student.getAge());
}
}
}
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
TreeSetTest类:
import java.util.Comparator;
import java.util.TreeSet;
public class TreeSetTest {
public static void main(String[] args) {
TreeSet<Student> treeSet = new TreeSet<>(
new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
int num1 = s1.getName().compareTo(s2.getName());
int num2 = num1 == 0 ? (s1.getAge() - s2.getAge()) : num1;
return num2;
}
});
Student student1 = new Student("张三", 20);
Student student2 = new Student("张三", 22);
Student student3 = new Student("李四", 21);
Student student4 = new Student("王五", 22);
treeSet.add(student1);
treeSet.add(student2);
treeSet.add(student3);
treeSet.add(student4);
for (Student student : treeSet) {
System.out.println(student.getName() + "---" + student.getAge());
}
}
}
import java.util.HashMap;
public class CharCount {
public static void main(String[] args) {
String str = "aababcabcdabcde";
HashMap<Character, Integer> hashMap = new HashMap<>();
char[] arr = str.toCharArray();
for (char c : arr) {
if (hashMap.containsKey(c)) {
Integer integer = hashMap.get(c);
hashMap.put(c, integer + 1);
} else {
hashMap.put(c, 1);
}
}
System.out.println(hashMap);
}
}
运行结果为:
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;
public class Poker {
public static void main(String[] args) {
String[] color = {"♥", "♠", "♣", "♦"};
String[] num = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
HashMap<Integer, String> hm = new HashMap<>();
ArrayList<Integer> list = new ArrayList<>();
int index = 0;
for (String s1 : num) {
for (String s2 : color) {
hm.put(index++, s2.concat(s1));
}
}
hm.put(++index, "☀");
hm.put(++index, "");
list.addAll(hm.keySet());
Collections.shuffle(list);
TreeSet<Integer> gaijin = new TreeSet<>();
TreeSet<Integer> daozai = new TreeSet<>();
TreeSet<Integer> xingzai = new TreeSet<>();
TreeSet<Integer> dipai = new TreeSet<>();
for (int i = 0; i < list.size(); i++) {
if (i >= list.size() - 3) {
dipai.add(list.get(i));
} else if (i % 3 == 0) {
gaijin.add(list.get(i));
} else if (i % 3 == 1) {
daozai.add(list.get(i));
} else {
xingzai.add(list.get(i));
}
}
lookPoker(hm, gaijin, "高进");
lookPoker(hm, daozai, "刀仔");
lookPoker(hm, xingzai, "星仔");
lookPoker(hm, dipai, "底牌");
}
public static void lookPoker(HashMap<Integer, String> hm, TreeSet<Integer> ts, String name) {
System.out.println(name + ":\t");
for (Integer t : ts) {
System.out.print(hm.get(t));
}
System.out.println();
}
}