今天,我们来复习下基础的集合框架List,Set,Map重点掌握
List为不定长数组,Set存储的是无序且唯一的对象,Map是存储以(K,V)形式的元素对
1.List的常用方法介绍
public class ListTest {
public static void main(String[] args) {
//创建List对象
ArrayList list = new ArrayList();
//向list中添加元素
//list可以根据index添加到指定位置,也可以直接添加
list.add("A");
list.add(1);
list.add(2,"b");
System.out.println(list);
//结果为:[A, 1, b]
//创建其他的集合框架添加到list中,也可以根据index来指定位置
Set set = new HashSet(1,2);
list.addAll(set);
list.addAll(1,set);
//输出list的长度
System.out.println(list.size());
//查询list中是否包含某个值,返回为boolean
System.out.println(list.contains("A"));
System.out.println(list.contains("a"));
//创建迭代器对象,取出list中的值
Iterator iterator = list.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
//去除指定元素
list.remove("A");
System.out.println(list);
}
}
2.Set是接口无法实例化,我们使用只能实例化它的子类对象HashSet、LinkedHashSet、TreeSet
HashSet:无序的(无序是指元素的存储顺序和遍历顺序不一致 )
LinkedHashSet:LinkedHashSet 存储一组有序(指元素的存储顺序和遍历顺序一至)且唯一的元素
TreeSet:TreeSet 可以存储有序(TreeSet 的有序是指集合内部的元素会自动进行排序,升序 )的元素
常用方法与list相差不大,主要介绍后面两种
LinkedHashSet存储进入会根据hashCode值判断两个对象是否相同,如果hashCode值不同,则判定为两个不同对象,如果hash值相同,会通过equals方法进行判断,从而得出两个对象是否相等。
public class LinkedHashSetTest {
public static void main(String[] args) {
//创建LinkedHashSet对象
LinkedHashSet test = new LinkedHashSet();
/*
* 这边因为重写了hashCode方法,两个对象的num+idd的值相同,在去进行属性a的比较。
* a不相同所以两个对象不相同
* 若a的值相同只能存入一个对象
* */
test.add(new test(1,2,3));
test.add(new test(1,2,6));
System.out.println(test.size());
}
}
//对象类
class test{
//给测试对象一些属性
private int num;
private int id;
private int a;
//创建构造方法
public test(int num, int id, int a) {
this.num = num;
this.id = id;
this.a = a;
}
//重写toString方法
@Override
public String toString() {
return "test{" +
"num=" + num +
", id=" + id +
", a=" + a +
'}';
}
//重写equals方法
@Override
public boolean equals(Object o) {
test newTest = (test)o;
return newTest.a == a;
}
//重写hashCode方法
@Override
public int hashCode() {
return num+id;
}
}
这边因为重写了hashCode方法,两个对象的num+idd的值相同,在去进行属性a的比较。 a不相同所以两个对象不相同.
若a的值相同只能存入一个对象
TreeSet:会自动将存入的数据进行升序排序
public class TreeSetTest {
public static void main(String[] args) {
//创建TreeSet对象
TreeSet treeSet = new TreeSet();
treeSet.add(1);
treeSet.add(3);
treeSet.add(6);
treeSet.add(2);
treeSet.add(5);
treeSet.add(4);
treeSet.add(1);
System.out.println(treeSet);
//结果为[1, 2, 3, 4, 5, 6]
}
}
我们存入的数据如果为无法排序的那会怎么样?
public class TreeSetTest2 {
public static void main(String[] args) {
//创建TreeSet对象
TreeSet treeSet = new TreeSet();
treeSet.add(new B(1));
treeSet.add(new B(3));
treeSet.add(new B(6));
treeSet.add(new B(2));
treeSet.add(new B(5));
treeSet.add(new B(4));
System.out.println(treeSet);
}
}
class B{
private int id;
public B(int id) {
this.id = id;
}
}
这样就会报异常
Exception in thread "main" java.lang.ClassCastException
TreeSet无法比较对象,这样我们就得重写排序规则,让B实现Comparable接口重写compareTo方法。自定义排序规则,可以实现存入对象
public class TreeSetTest2 {
public static void main(String[] args) {
//创建TreeSet对象
TreeSet treeSet = new TreeSet();
treeSet.add(new B(1));
treeSet.add(new B(3));
treeSet.add(new B(6));
treeSet.add(new B(2));
treeSet.add(new B(5));
treeSet.add(new B(4));
System.out.println(treeSet);
}
}
class B implements Comparable {
private int id;
public B(int id) {
this.id = id;
}
/*
* A>B ->1
* A<B ->-1
* A=B ->0
*/
@Override
public int compareTo(Object o) {
B b = (B)o;
if(this.id > b.id){
return 1;
}else if(this.id == b.id){
return 0;
}else{
return -1;
}
}
@Override
public String toString() {
return "B{" +
"id=" + id +
'}';
}
}
Map常用的实现类:
HashMap:存储一组无序,key 不可重复,value 可重复的元素。
Hashtable:存储一组无序,key 不可重复,value 可重复的元素。
TreeMap:存储一组有序,key 不可重复,value 可重复的元素,可以按照 key 进行排序
HashMap与Hashtable的区别就是,HashMap 线程不安全,但是效率更高,Hashtable 线程安全,但是效率低。
Map的常用方法:
public class HashMapTest {
public static void main(String[] args) {
//创建HashMap对象
HashMap hm = new HashMap();
//添加数据
hm.put("a",1);
hm.put("b",2);
hm.put("c",3);
//结果为{a=1, b=2, c=3},以键值对的形式
System.out.println(hm);
//根据Key进行删除,也可以根据一个键值对来进行删除
hm.remove("a");
hm.remove("b",2);
//输出集合长度
System.out.println(hm.size());
//判断集合是否为空
System.out.println(hm.isEmpty());
//判断是否存在某给Key值
System.out.println(hm.containsKey("a"));
//判断是否存在某给value
System.out.println(hm.containsValue(2));
//向集合中添加另一个集合
hm.putAll(new HashMap());
//取出对应的key的value
System.out.println(hm.get("c"));
//取出集合的所有Key,返回成一个set集合
Set set = hm.keySet();
System.out.println(set);
//取出集合的所有Value,返回成一个Collection
Collection collection = hm.values();
System.out.println(collection);
//将Map对象转为Set对象
Set entrySet = hm.entrySet();
System.out.println(entrySet);
//获取集合的散列值
System.out.println(hm.hashCode());
//使用迭代器遍历出Map
Iterator iterator2 = set.iterator();
while (iterator2.hasNext()) {
Object key = iterator2.next();
Object value = hm.get(key);
System.out.println(key + "-" + value);
}
//清空集合
hm.clear();
}
}
TreeMap的有序是根据Key值进行排序,若Key为不可排序对象,就像Set实现Comparable接口重写compareTo方法。自定义排序规则,可以实现存入对象
public class Test2 {
public static void main(String[] args) {
TreeMap treeMap = new TreeMap();
treeMap.put(new User(3, "1"), "1");
treeMap.put(new User(5, "2"), "2");
treeMap.put(new User(1, "3"), "3");
treeMap.put(new User(6, "4"), "4");
treeMap.put(new User(2, "5"), "5");
treeMap.put(new User(4, "6"), "6");
Set set = treeMap.keySet();
for (Object o : set) {
System.out.println(o + "-" + treeMap.get(o));
}
}
}
class User implements Comparable {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public User(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public int compareTo(Object o) {
User user = (User) o;
if(this.id > user.id) return -1;
if(this.id < user.id) return 1;
return 0;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}