HashSet和HashMap之间是否存在关系?
HashSet中会有一个HashMap的属性, 每当创建一个HashSet对象的时候 , 就会自动创建一个HashMap
添加数据的时候,是向HashMap的键添加一个数据
public class Person {
String name;
int age;
public Person(String name, int age) {
super();
this.name = name;
this.age = 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;
}
}
import java.util.HashMap;
import java.util.HashSet;
public class Test {
public static void main(String[] args) {
HashMap<String, Person> map = new HashMap<>();
map.put("a", new Person("a", 4));
map.put("b", new Person("a", 4));
map.put("c", new Person("a", 4));
map.put("d", new Person("a", 4));
System.out.println(map);
HashSet set = new HashSet<>();
set.add("a");
}
}
TreeMap 和TreeSet
如果自然排序和定制排序都实现了, 定制排序的优先级更高一些
排序也只能针对 键起作用, 不能针对值起作用
TreeMap和TreeSet的关系
TreeSet中会有一个TreeMap的属性, 每当创建一个TreeSet对象的时候 , 就会自动创建一个TreeMap
添加数据的时候,是向TreeMap的键添加一个数据
public class Person implements Comparable<Person>{
String name;
int age;
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
@Override
public int compareTo(Person o) {
if(this.age>o.age) {
return 1;
}else if (this.age<o.age) {
return -1;
}
return 0;
}
}
import java.util.Comparator;
public class PersonComparator implements Comparator<Person>{
@Override
public int compare(Person o1, Person o2) {
return -(o1.age-o2.age);
}
}
import java.util.Comparator;
import java.util.TreeMap;
import java.util.TreeSet;
public class Test {
public static void main(String[] args) {
TreeMap<Person, Integer> map = new TreeMap<>(new PersonComparator());
map.put(new Person("zhangsan", 16), 1);
map.put(new Person("zhangsan", 61), 1);
map.put(new Person("zhangsan", 16), 1);
map.put(new Person("zhangsan", 16), 1);
System.out.println(map);
// 定制排序 匿名内部类的写法
TreeMap<Person, Integer> map2 = new TreeMap<>(new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) {
return o1.age - o2.age;
}
});
TreeMap<Integer, Person> map3 = new TreeMap<>();
map3.put(1, new Person("lisi", 6));
map3.put(1, new Person("lisi", 6));
map3.put(1, new Person("lisi", 6));
map3.put(1, new Person("lisi", 6));
System.out.println(map3);
TreeSet<Person> set = new TreeSet<>();
set.add(new Person("a", 4));
}
}
list集合, 允许重复, 如果是重复的元素, 在排序的时候 仅仅是排序, 不会移除
集合中是自定义类型, 则必须实现自然排序
集合中泛型如果是自定义类型 , 也可以实现定制排序
public class Student implements Comparable<Student>{
int age;
public Student(int age) {
super();
this.age = age;
}
@Override
public String toString() {
return "Student [age=" + age + "]";
}
@Override
public int compareTo(Student o) {
return this.age-o.age;
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Test {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
// 批量添加
Collections.addAll(list, "D", "a", "d", "c");
System.out.println(list);
// 利用的自然排序
Collections.sort(list);
System.out.println(list);
ArrayList<Student> list2 = new ArrayList<>();
Student student = new Student(2);
list2.add(new Student(3));
list2.add(new Student(13));
list2.add(new Student(1));
list2.add(student);
list2.add(student);
System.out.println(list2);
Collections.sort(list2);
System.out.println(list2);
Collections.sort(list2, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return -(o1.age-o2.age);
}
});
System.out.println(list2);
// 将集合中元素进行 翻转
Collections.reverse(list2);
System.out.println(list2);
//替换集合中的元素, 根据地址进行替换
Collections.replaceAll(list2, student, new Student(5));
System.out.println(list2);
// 要求集合中的类实现了 自然排序
Student max = Collections.max(list2);
System.out.println(max);
Student min = Collections.min(list2);
System.out.println(min);
// 也可以实现定制排序
Student max2 = Collections.max(list2, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.age-o2.age;
}
});
System.out.println(max2);
Student min2 = Collections.min(list2, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
return o1.age-o2.age;
}
});
System.out.println(min2);
}
}