Map集合
map集合的使用
package pkg2020华南虎;
import java.util.HashMap;
import java.util.Map;
/**
*
* @author yl
*/
/*双列集合:
Map:实现Map接口的集合类,具备的特点:存储的数据都是以键值对的方式,键不可以重复,
值可以重复。
*/
public class MapDemo {
public static void main(String[] args) {
Map map = new HashMap();
//添加常用方法
map.put("1", "aaa");
map.put("2", "bbb");
map.put("3", "ccc");
//输出aaa
System.out.println(map.put("1", "aaa"));
//这里返回null:表示4这个键以前没有关联的值
System.out.println(map.put("4", "ddd"));
System.out.println("集合元素:" + map);
//这里输出关联的值ddd,会被abc覆盖???
System.out.println(map.put("4", "abc"));
map.put("5", "abc");
Map map2 = new HashMap();
map2.put("6", "eee");
map2.put("1", "aaa");
map2.put("1", "ert");//重写?
map.putAll(map2);//???
System.out.println("map:" + map);
System.out.println("map2:" + map2);
//删除数据常用方法
System.out.println("删除的数据" + map.remove("1"));
System.out.println("map:" + map);
map.clear();
System.out.println("map:" + map);
//获取方法
System.out.println(map.get("1"));
System.out.println(map.size());
//判断的方法
System.out.println(map.containsKey("1"));
System.out.println(map.containsValue("aaa"));
map.clear();
map.put(null, null);
System.out.println(map.isEmpty());
System.out.println("集合元素:" + map);
}
}
结果:
run:
aaa
null
集合元素:{1=aaa, 2=bbb, 3=ccc, 4=ddd}
ddd
map:{1=ert, 2=bbb, 3=ccc, 4=abc, 5=abc, 6=eee}
map2:{1=ert, 6=eee}
删除的数据ert
map:{2=bbb, 3=ccc, 4=abc, 5=abc, 6=eee}
map:{}
null
0
false
false
false
集合元素:{null=null}
成功构建 (总时间: 0 秒)
HashMap的使用
package pkg2020华南虎;
import java.util.HashMap;
import java.util.Map;
/**
*
* @author yl
*/
/*
HashMap:
底层使用hash表来实现
向HashMap中添加元素的时候,首先调用键的hashCode方法得到元素的哈希码的值,经过
运算,得到该元素在哈希表中的位置;
1.如果该位置没有存储元素,则元素之间添加到哈希表中
2.如果该位置有其他的元素,会调用元素的equals方法,如果返回false,该元素被储存,
返回true,该元素重复不能存储。
*/
class Person01 {
private int id;
private String name;
public Person01(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "[编号:" + this.id + ";姓名:" + this.name + "]";
}
@Override
public int hashCode() {
return this.id;
}
@Override
public boolean equals(Object obj) {
Person01 p = (Person01) obj;
return this.id == p.id;
}
}
public class HashMapDemo {
public static void main(String[] args) {
Map map = new HashMap();
map.put(new Person01(1, "a"), "aaa");
map.put(new Person01(2, "b"), "bbb");
map.put(new Person01(3, "c"), "ccc");
map.put(new Person01(1, "a"), "ddd");
System.out.println("集合的元素:" + map);
}
}
结果:
run:
集合的元素:{[编号:1;姓名:a]=ddd, [编号:2;姓名:b]=bbb, [编号:3;姓名:c]=ccc}
成功构建 (总时间: 0 秒)
TreeMap的使用
package pkg2020华南虎;
import java.util.Comparator;
import java.util.TreeMap;
/**
*
* @author yl
*/
/*
TreeMap:基于二叉树的数据结构实现的,会对具备自然特性的键进行排序存储
1.元素的键具备自然特性,直接排序存储
2.不具备自然特性,实现Comparable接口,在CompareTo中定义排序规则
3.不具备自然特性,没有实现Comparable接口,在创建TreeMap对象的时候传入比较器
*/
class Emp/*implements Comparable*/ {
String name;
int salary;
public Emp(String name, int salary) {
this.name = name;
this.salary = salary;
}
@Override
public String toString() {
return "[姓名:" + this.name + ";薪水:" + this.salary + "]";
}
/*@Override
public int compareTo(Emp o) {
return this.sarlary-o.sarlary;
}*/
}
class MyComparator implements Comparator {
@Override
public int compare(Emp o1, Emp o2) {
return o1.salary - o2.salary;
}
}
public class TreeMapDemo {
public static void main(String[] args) {
//创建一个比较器
MyComparator my = new MyComparator();
TreeMap map = new TreeMap(my);
/*map.put('c', 3);
map.put('b', 2);
map.put('a', 1);*/
map.put(new Emp("a", 5000), "6");
map.put(new Emp("b", 4000), "5");
map.put(new Emp("c", 3000), "4");
//下面e的值1就会覆盖d的值3
map.put(new Emp("d", 2000), "1");
System.out.println(map);
}
}
结果:
run:
{[姓名:d;薪水:2000]=1, [姓名:c;薪水:3000]=4, [姓名:b;薪水:4000]=5, [姓名:a;薪水:5000]=6}
成功构建 (总时间: 0 秒)
Vector集合
package pkg2020华南虎;
import java.util.Enumeration;
import java.util.Vector;
/**
*
* @author yl
*/
public class VectorDemo {
/*
Vector底部维护Object数组,实现和ArrayList一样,但是Vector是线程安全,效率低,
两者比较:
相同点:
1.底部都是维护Object的数组
不同点:
1.ArrayList线程不同步,效率高
2.ArrayList JDK1.2出现,Vector JDK1.0出现
*/
public static void main(String[] args) {
Vector v = new Vector();
v.addElement("Spring1");
v.addElement("Spring2");
v.addElement("Spring3");
Enumeration e = v.elements();
while (e.hasMoreElements()) {
System.out.println(e.nextElement());
}
}
}
结果:
run:
Spring1
Spring2
Spring3
成功构建 (总时间: 0 秒)