TreeMap
向 TreeMap 中添加 key-value,要求 key 必须是由同一个类创建的对象,因为要按照 key 进行排序:自然排序、定制排序
package com.collection.set;
public class User implements Comparable{
String name;
int age;
public User(String name, int age) {
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;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public int compareTo(Object o) {
if(o instanceof User){
User u = (User)o;
return this.name.compareTo(u.name);
}
//return 0;
throw new RuntimeException("传入的数据类型不一致");
}
}
package com.collection.set.practice;
/**
* 定义一个Employee类,
* 该类包含:private 成员变量 name age birthtday,
* 其中birthday为MyDate类的对象。
* 并为每一个属性定义 getter, setter 方法,
* 并重写toString方法输出name age birthday
*/
public class Employee implements Comparable{
private String name;
private int age;
private MyDate birthday;
public Employee(String name, int age, MyDate birthday) {
this.name = name;
this.age = age;
this.birthday = birthday;
}
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 MyDate getBirthday() {
return birthday;
}
public void setBirthday(MyDate birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", age=" + age +
", birthday=" + birthday +
'}';
}
// 按照姓名排序
@Override
public int compareTo(Object o) {
if(o instanceof Employee){
Employee e = (Employee)o;
return this.name.compareTo(e.name);
}
//return 0;
throw new RuntimeException("传入的数据类型不一致");
}
}
Properties
Hashtable 的子类,该对象用于处理属性文件
由于属性文件里的 key,value 都是字符串类型,所以 Properties 里的 key 和 value 都是字符串类型
Collections 工具类
操作 Set, List, Map 等集合的工具类
Q & A
Collection 和 Collections 的区别
collection 是存储单列数据的集合接口,子接口:list set
collections 是操作集合的工具类
常用方法
package com.collection.set;
import java.util.*;
// collections: manipulate Collection and Map
public class Demo07 {
public static void main(String[] args) {
test();
test1();
}
public static void test(){
List list = new ArrayList();
list.add(123);
list.add(13);
list.add(45);
list.add(16);
list.add(463);
System.out.println(list); // [123, 13, 45, 16, 463]
// reverse(List) 反转 List 中元素的顺序
Collections.reverse(list);
System.out.println(list); // [463, 16, 45, 13, 123]
// shuffle(List) 对 List 集合元素进行随机排序
Collections.shuffle(list);
// sort(List) 根据元素的自然顺序对指定 List 集合元素按照升序排序
Collections.sort(list);
// swap(List,int,int) 将指定 List 集合中 i 处元素和 j 处元素进行交换
Collections.swap(list,2,3);
// max(Collection)
// max(Collection, Comparator)
// frequency(Collection, Object) 返回指定集合中指定元素的出现次数
int frequency = Collections.frequency(list,123);
System.out.println(frequency); //1
}
public static void test1(){
List list = new ArrayList();
list.add(123);
list.add(13);
list.add(45);
list.add(16);
list.add(463);
// List dest = new ArrayList(); 异常
List dest = Arrays.asList(new Object[list.size()]);
System.out.println(dest.size()==list.size()); // trur
// copy(list dest, list src) 将 src 中的内容复制到 dest 中
Collections.copy(dest,list);
System.out.println(dest);
}
}
Collections 类中提供了多个 synchronizedxxx() 方法,该方法可使将指定集合包装成线程同步的集合,从而可以解决多线程并发访问集合时的线程安全问题
List list1 = Collections.synchronizedList(list);
// 返回的 list1 是线程安全的 list