

HashMap 玩法案例
package com.beyond;
import java.util.*;
class Person{
private String name;
private Integer age;
public Person(String name, Integer age){
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Person)) return false;
Person person = (Person) o;
return getName().equals(person.getName()) &&
getAge().equals(person.getAge());
}
@Override
public int hashCode() {
return Objects.hash(getName(), getAge());
}
}
public class HashMapTest {
public static void main(String[] args) {
// Map<String,Integer> map = new HashMap<>();
// map.put("One", 1);
// map.put("Two", 2);
// map.put("Three", 3);
// System.out.println(map);
//
// Map<String,Integer> map1 = new HashMap<>();
//
//
// map.clear();
// System.out.println(map);
Person person1 = new Person("dhl", 20);
Map<Integer,Object> map = new HashMap<>();
map.put(1, person1);
Map<Integer,Object> map1 = (Map<Integer, Object>) ((HashMap<Integer, Object>) map).clone();
System.out.println(map);
map1.put(2, new Person("ddd", 11));
map1.put(3, new Person("shdl", 22));
map.putAll(map1);
// Object ppp = map.replace(1, new Person("ppp", 12));
// System.out.println("2"+ppp.toString());
System.out.println("2"+map.replace(1, person1, new Person("ggg", 22)));
Set<Integer> set = map.keySet();
Iterator iterator = set.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
System.out.println(map.values());
}
}
ArrayList 玩法案例
package com.beyond;
import java.io.Serializable;
import java.util.*;
class Student implements Serializable {
private String name;
private int age;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", 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 Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Student)) return false;
Student student = (Student) o;
return getAge() == student.getAge() &&
Objects.equals(getName(), student.getName());
}
@Override
public int hashCode() {
return Objects.hash(getName(), getAge());
}
}
public class ArrayListTest {
public static void main(String[] args) {
Student student = new Student("dhl", 12);
List<Student> list = new ArrayList<>();
list.add(new Student("qqq", 12));
list.add(new Student("qqq", 12));
list.add(new Student("xxx", 13));
list.add(1, new Student("eee", 15));
// System.out.println(list);
// list.clear();
// System.out.println(list);
List<Student> list1 = (List<Student>) ((ArrayList<Student>) list).clone();
// System.out.println(list1);
// System.out.println(list.contains(new Student("xxx", 13)));
// System.out.println();
// System.out.println(list.get(1));
// System.out.println(list.set(1, new Student("ooo", 12)));
// System.out.println(list.get(1));
// Iterator<Student> iterator = list.iterator();
// while (iterator.hasNext()){
// System.out.println(iterator.next());
// }
//
// System.out.println(list.lastIndexOf(new Student("qqq", 12)));
// System.out.println(list);
// System.out.println(Arrays.toString(list.toArray()));
// list.forEach((x)->{
// System.out.println(x);
// });
// System.out.println(list.remove(new Student("qqq", 12)));
// System.out.println(list);
//
// ListIterator<Student> studentListIterator = list.listIterator(0);
// while (studentListIterator.hasNext()){
// System.out.println(studentListIterator.previousIndex());
// System.out.println(studentListIterator.next());
// }
//
// list.removeIf(x -> x.getAge()<13);
// System.out.println(list);
// list.replaceAll(x -> new Student("xxx", 24));
// System.out.println(list);
//
// list.sort(new Comparator<Student>() {
// @Override
// public int compare(Student o1, Student o2) {
// if (o1.getAge() == 0 || o2.getAge() == 0) return 0;
// return o1.getAge() > o2.getAge() ? 1 : -1; //这是顺序
// }
// });
//
// List<Student> list2 = list.subList(1, 3);
// System.out.println(list2);
//
// System.out.println(list);
// Student[] array = list.toArray(new Student[1]); // 若a的length小于collection的size时,会自动创建一个新长度的数组接受元素
// System.out.println(Arrays.toString(array));
System.out.println(list.removeAll(list));
System.out.println(list.size());
((ArrayList<Student>) list).trimToSize(); // 意义不大, 整理, 或者说是恢复到真实的容量, 即删除自动扩容的空间
System.out.println(list.size());
}
}