package lch;
import java.util.ArrayList;
import java.util.Collections;
public class fangfa {
public static void main(String[] args){
ArrayList<Integer> list1 = new ArrayList<>();
Collections.addAll(list1, 1,2,3,4,5,6,7);
System.out.println(list1);
Collections.shuffle(list1);
System.out.println(list1);
Collections.sort(list1);
System.out.println(list1);
}
}
被排序的集合里的元素,必须实现Comparable,重写接口中的方法compareTo定义排序的规则。
返回值:自己减去参数为升序。
package lch;
import java.util.Objects;
public class Animal implements Comparable<Animal>{
Animal(){
}
Animal(String name, int age){
this.age = age;
this.name = name;
}
private String name;
private int age;
@Override
public int compareTo(Animal o) {
//return 0; //认为元素都是相同的
return this.age - o.age; //年龄升序
}
@Override
public String toString() {
return "Animal{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Animal animal = (Animal) o;
return age == animal.age &&
Objects.equals(name, animal.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}
package lch;
import java.util.ArrayList;
import java.util.Collections;
public class fangfa {
public static void main(String[] args){
Animal a1 = new Animal("a", 1);
Animal a2 = new Animal("b", 3);
ArrayList<Animal> list2 = new ArrayList<>();
Collections.addAll(list2, a2, a1);
System.out.println(list2);
Collections.sort(list2);
System.out.println(list2);
}
}
Comparable和Comparetor的区别:
Comparable:自己和别人比较,需要实现Comparable接口,重写compareTo方法;
Comparator:找一个第三方进行比较。前减后为升序。
package lch;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class fangfa {
public static void main(String[] args){
Animal a1 = new Animal("a", 1);
Animal a2 = new Animal("b", 3);
ArrayList<Animal> list2 = new ArrayList<>();
Collections.addAll(list2, a2, a1);
System.out.println(list2);
Collections.sort(list2, new Comparator<Animal>() {
@Override
public int compare(Animal o1, Animal o2) {
return o1.age - o2.age;
}
});
System.out.println(list2);
}
}
类似C++的Map。
HashMap:多线程实现,底层是哈希表,存储元素和取出元素的顺序可能不同。
LinkedHashMap:是HashMap的子类,具有可预知的迭代顺序,存储元素和取出元素的顺序相同。
package lch;
import java.util.HashMap;
public class fangfa {
public static void main(String[] args){
HashMap<Integer, Integer> fre = new HashMap<>();
System.out.println(fre.put(1,10)); //无重复key则返回null,否则返回被替换的值
System.out.println(fre.put(1,15));
System.out.println(fre.put(2,20));
System.out.println(fre.put(2,25));
System.out.println(fre.remove(2)); //key存在则返回删除的value,否则返回空
System.out.println(fre.get(1)); //key存在则返回对应value,否则返回null
System.out.println(fre.containsKey(1));
System.out.println(fre.containsValue(15));
}
}
package lch;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Objects;
public class fangfa {
public static void main(String[] args){
HashMap<Integer, Integer> fre = new HashMap<>();
fre.put(1,2345);
fre.put(2,2142);
fre.put(3,2365);
fre.put(4,3753);
fre.put(5,3636);
fre.put(6,3564);
fre.put(7,3322);
System.out.println(fre.keySet());
System.out.println(fre.values());
Iterator<Integer> it1 = fre.keySet().iterator();
while(it1.hasNext()){
Integer key1 = it1.next();
System.out.println(key1);
System.out.println(fre.get(key1));
}
}
}
Map接口中有一个内部接口Entry。
当Map集合创建时,会在Map集合中创建一个Entry对象,用来记录key和value。
package lch;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class fangfa {
public static void main(String[] args){
HashMap<Integer, Integer> fre = new HashMap<>();
fre.put(1,2345);
fre.put(2,2142);
fre.put(3,2365);
fre.put(4,3753);
fre.put(5,3636);
fre.put(6,3564);
fre.put(7,3322);
Iterator< Map.Entry<Integer, Integer> > it = fre.entrySet().iterator();
while(it.hasNext()){
Map.Entry e = it.next();
System.out.println(e.getKey());
System.out.println(e.getValue());
}
}
}
作为key的元素必须重写hashCode方法和equals方法,以保证唯一。
package lch;
import java.util.Objects;
public class Person {
Person(int age, String name){
this.age = age;
this.name = name;
}
public int age;
public String name;
@Override
public String toString() {
return "Person{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return age == person.age &&
Objects.equals(name, person.name);
}
@Override
public int hashCode() {
return Objects.hash(age, name);
}
}
package lch;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class fangfa {
public static void main(String[] args){
HashMap<Person, Integer> map1 = new HashMap<>();
map1.put(new Person(3, "a"), 3);
map1.put(new Person(3, "a"), 3);
map1.put(new Person(3, "a"), 3);
map1.put(new Person(3, "a"), 3);
map1.put(new Person(3, "a"), 3);
Iterator< Map.Entry<Person, Integer> > it = map1.entrySet().iterator();
while(it.hasNext()){
Map.Entry<Person, Integer> e = it.next();
System.out.println(e.getKey());
System.out.println(e.getValue());
}
}
}
LinkedHashMap继承了HashMap。
package lch;
import java.util.HashMap;
import java.util.LinkedHashMap;
public class fangfa {
public static void main(String[] args){
HashMap<Integer, Integer> map1 = new HashMap<>();
map1.put(1,10);
map1.put(2,20);
map1.put(4,40);
map1.put(3,30);
System.out.println(map1);
LinkedHashMap<Integer, Integer> map2 = new LinkedHashMap<>();
map2.put(1,10);
map2.put(2,20);
map2.put(4,40);
map2.put(3,30);
System.out.println(map2);
}
}
类似HashMap集合,但Hashtable集合不允许null键和null值。Hashtable是最早的双链map。单线程。
of方法只适用于List、Map、Set接口,不适用于接口的实现类。
of方法的返回值是一个不能改变的集合,不能再使用add或put方法添加元素了。
Set和Map在调用of方法时,不能有重复元素,否则抛出异常。
package lch;
import java.util.Map;
import java.util.Set;
public class fangfa {
public static void main(String[] args){
Set<String> set1 = Set.of("a", "b", "c", "d");
System.out.println(set1);
Map<Integer, String> map1 = Map.of(2,"d", 3, "weqd");
System.out.println(map1);
}
}
使用方式:
1.在行号处鼠标左键单击,添加断点;
2.右键使用Debug执行程序,程序就会停留在添加的第一个断点处;
F8:逐行执行程序
F7:进入方法中
shift+F8:跳出方法
F9:跳到下一个断点,如果没有就停止程序
ctrl+F2:退出debug模式
Console:切换到控制台