set(接口):
特点:无序(无下标) 不重复
HashSet添加字符串:
// HashSet 主要用来去重
public static void fun() {
// 创建一个HashSet
HashSet<String> set = new HashSet<>()
boolean b1 = set.add("d")
boolean b2 = set.add("d")
set.add("a")
set.add("a")
set.add("b")
set.add("b")
set.add("c")
set.add("c")
// 有序 怎么存 怎么打印
System.out.println(b1)
System.out.println(b2)
System.out.println(set)
}
HashCode及equals的重写:
public class Person {
private String name;
private int age;
public Person() {
super();
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "姓名:" + name + ", 年龄:" + age;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
public static void fun() {
HashSet<Person> set = new HashSet<>();
set.add(new Person("张三", 17));
set.add(new Person("张三", 17));
set.add(new Person("李四", 18));
set.add(new Person("李四", 18));
set.add(new Person("王五", 19));
set.add(new Person("王五", 19));
Iterator<Person> iterator = set.iterator();
while(iterator.hasNext()) {
Person person = iterator.next();
System.out.println(person);
}
}
练习:
public static void fun() {
HashSet<Integer> set = new HashSet<>();
while (set.size() < 10) {
int num = (int)(Math.random() * (20 - 1 + 1) + 1);
set.add(num);
}
System.out.println(set);
}
LinkedHashSet:
public static void fun() {
// LinkedHashSet是HashSet的一个子类
// 特点:有序 怎么存怎么取
// 字符串为什么直接放进set中能去重?
LinkedHashSet<String> set = new LinkedHashSet<>();
set.add("l");
set.add("w");
set.add("w");
set.add("a");
set.add("n");
set.add("g");
System.out.println(set);
}
练习:
public static void fun() {
System.out.println("请输入一个字符串:");
Scanner scanner = new Scanner(System.in);
String string = scanner.nextLine();
LinkedHashSet<Character> set = new LinkedHashSet<>();
char[] charArray = string.toCharArray();
for (int i = 0; i < charArray.length; i++) {
set.add(charArray[i]);
}
scanner.close();
System.out.println(set);
}
public static void fun() {
ArrayList<String> list = new ArrayList<>();
list.add("a");
list.add("a");
list.add("b");
list.add("b");
list.add("c");
list.add("c");
list.add("d");
list.add("d");
LinkedHashSet<String> set = new LinkedHashSet<>();
set.addAll(list);
list.clear();
list.addAll(set);
System.out.println(list);
}
TreeSet:
特点:无序 不重复 无下标
主要作用:排序
Comparable(接口):
public class Worker extends Person implements Comparable<Worker> {
public Worker() {
super();
}
public Worker(String name, int age) {
super(name, age);
}
@Override
public int compareTo(Worker o) {
int length = this.getName().length() - o.getName().length();
int num = length == 0 ? this.getAge() - o.getAge() : length;
return num == 0 ? this.getAge() - o.getAge() : num;
}
}
public static void fun() {
TreeSet<Worker> set = new TreeSet<>();
set.add(new Worker("张三", 24));
set.add(new Worker("李四", 24));
set.add(new Worker("王五", 23));
set.add(new Worker("赵六", 27));
for (Worker worker : set) {
System.out.println(worker);
}
System.out.println((int)'张');
System.out.println((int)'李');
System.out.println((int)'王');
System.out.println((int)'赵');
}
Comparator(接口):
class KeepStringComImpl implements Comparator<String> {
@Override
public int compare(String o1, String o2) {
int length = o1.length() - o2.length();
int num = length == 0 ? o1.compareTo(o2) : length;
return num == 0 ? 1 : num;
}
}
public static void fun3() {
TreeSet<String> set = new TreeSet<>(new KeepStringComImpl());
set.add("asd");
set.add("asde");
set.add("asdef");
set.add("a");
set.add("asd");
System.out.println(set);
}
class KeepCharImpl implements Comparator<Character> {
@Override
public int compare(Character o1, Character o2) {
int num = o1.compareTo(o2);
return num == 0 ? 1 : num;
}
}
public static void fun4() {
System.out.println("请输入一个字符串:");
Scanner scanner = new Scanner(System.in);
String string = scanner.nextLine();
char[] charArray = string.toCharArray();
TreeSet<Character> set = new TreeSet<>(new KeepCharImpl());
for (int i = 0; i < charArray.length; i++) {
set.add(charArray[i]);
}
scanner.close();
System.out.println(set);
}
class KeepIntImpl implements Comparator<Integer> {
@Override
public int compare(Integer o1, Integer o2) {
int num = o1 - o2;
return -(num == 0 ? 1 : num);
}
}
public static void fun5() {
Scanner scanner = new Scanner(System.in);
TreeSet<Integer> set = new TreeSet<>(new KeepIntImpl());
while (true) {
System.out.println("请输入整数 输入quit结束:");
String string = scanner.nextLine();
if (string.equals("quit")) {
break;
}
int num = Integer.parseInt(string);
set.add(num);
}
scanner.close();
System.out.println(set);
}