目录
1.数组
2.Vector
3.Stack
4.HashTable
5.List
6.Map
7.Set
一.数组
本质
数组实际代表一个简单的线性序列
优点
访问速度特别快
缺点
大小固定 不可在存在时间内发生改变
演示
package one;
public class Six {
public static void main(String[] args) {
int[] a = {1, 2, 3};
//很惊讶的一点
int[][] b = {{1},{2, 3}};
for (int i = 0; i < b.length; i++){
System.out.println("第" + i + "行长度是 = " + b[i].length);
}
//注意出现这种情况
Info[] infos = new Info[2];
try{
System.out.println(infos[0].a);
}catch(NullPointerException e){
System.out.println("空指针错误");
}
//赋值方式1
infos[0] = new Info(0);
try{
System.out.println(infos[0].a);
}catch(NullPointerException e){
System.out.println("空指针错误");
}
//赋值方式2
Info[] infos1 = new Info[]{new Info(1), new Info(2)};
try{
System.out.println(infos1[0].a);
}catch(NullPointerException e){
System.out.println("空指针错误");
}
//赋值方式3
Info[] infos2 = {new Info(1), new Info(2)};
try{
System.out.println(infos2[0].a);
}catch(NullPointerException e){
System.out.println("空指针错误");
}
}
//数组的返回
private static int[] back(){
int[] a = {0, 1};
return a;
}
private static class Info{
private int a;
public Info(int a){
this.a = a;
}
}
}
二.Vector
本质
可以动态改变的数组,初始数组长度是10,如果不够用了将数据赋值到一个新的更大的数组(好像是大小翻倍)把原来的copy进去,并且加入最新数据。
优点
节省内存
缺点
费时
add & addElement 与 get & elementAt
本质是一样的,可能只是为了方便大家使用吧。 add会返回是不是插入成功,建议使用add get
演示
package one;
import java.util.Vector;
public class Six {
public static void main(String[] args) {
Vector<Integer> vector = new Vector();
vector.add(2);
vector.add(3);
System.out.println(vector.get(0));
System.out.println(vector.get(1));
BitSet bitSet = new BitSet();
bitSet.set(0);//置为true
bitSet.set(2);
System.out.println(bitSet.get(0));
System.out.println(bitSet.get(1));
}
}
output:
2
3
true
false
Process finished with exit code 0
总结
bitset存的是二进制,一般用不到。
三.stack
本质
后进先出,继承自Vector
演示
public class Six {
public static void main(String[] args) {
Stack<Integer> integers = new Stack<>();
integers.push(2);
integers.push(3);
System.out.println(integers.pop());
System.out.println(integers.pop());
}
}
output:
3
2
Process finished with exit code 0
四.HashTable
本质
Entry数组存储 有一个初始化的大小11 存储下标是根据key算出适合当前大小的散列码存储的。大小不足时。建立新的更大数组,重新整理一次。
优点
查找快速 put为synchronized 线程安全 (hashmap 的put不是synchronized)
public class Six {
public static void main(String[] args) {
Hashtable<Integer, String> hashtable = new Hashtable<>();
hashtable.put(0, "我是0");
hashtable.put(1, "我是1");
System.out.println(hashtable.get(0));
System.out.println(hashtable.get(1));
}
}
五.List
说明
ArrayList 访问快,中间插入慢,本质上是顺序表,作为一个常规用途的对象容器使用,用于替换原先的Vector
LinkedList 访问慢,插入快,本质上是链式存储结构。
演示
public class Six {
public static void main(String[] args) {
List<Integer> array = new ArrayList<>();
array.add(99);
array.add(98);
List<Integer> linked = new LinkedList<>();
linked.add(98);
linked.add(99);
System.out.println(array.get(0));
System.out.println(array.get(1));
System.out.println(linked.get(0));
System.out.println(linked.get(1));
}
}
output:
99
98
98
99
Process finished with exit code 0
六.Map
hashmap

解释
- 链表 顺序表 结合 有插入快速 取值快速的优点
- 线程不安全
引用了 https://blog.csdn.net/MyComeIn/article/details/48447233
HashMap实现同步的方法:调用Collections的静态方法Collections.synchronizedMap(Map map);,返回一个同步的Map,这个Map封装了底层的HashMap的所有方法,使得底层的HashMap即使是在多线程的环境中也是安全的。
同时说一下,ArrayList和HashSet,他们也都不是同步的,都是线程不安全的,对其实现同步的方式和HashMap的方式相同,都允许使用null元素,ArrayList分配的初始空间为10,HashSet分配的初始空间为16
TreeMap
解释
使用了 红黑树(平衡二叉树(有时候二叉树偏向一侧,平衡二叉树使得树偏向中央)的一种)
树状结构
因为是树的原因, 得到的是已经排好顺序的键值对。
演示
public class Six {
public static void main(String[] args) {
Map<Integer, String> hash = new HashMap<>();
Map<Integer, String> tree = new TreeMap<>();
hash.put(2,"测试");
tree.put(3, "测试3");
tree.put(2, "测试2");
tree.put(4, "测试4");
for (String s : tree.values()){
System.out.println(s);
}
}
}
output:
测试2
测试3
测试4
Process finished with exit code 0
七.Set
前言
只能允许不同的值,判断是不是同一个值的方法 equals 返回值& hashcode 是不是相同
HashSet
内部用hashMap 存值
TreeSet
依旧是红黑树,得到的是排好顺序的值
演示
package one;
import java.util.*;
public class Six {
public static void main(String[] args) {
Set<Info> hash = new HashSet<>();
Set<Info1> tree = new TreeSet<>();
hash.add(new Info(1));
hash.add(new Info(2));
hash.add(new Info(2));
tree.add(new Info1(6));
tree.add(new Info1(2));
tree.add(new Info1(2));
System.out.println(hash.size());
System.out.println(tree.size());
for (Info1 info1 : tree){
System.out.println(info1.a);
}
}
//自定义hashSet
public static class Info{
private int a;
public Info(int a){
this.a = a;
}
@Override
public int hashCode(){ //复写hashCode()
return a;
}
@Override
public boolean equals(Object obj){
if (((Info)obj).a == a){
return true;
}
return false;
}
}
//自定义treeSet
private static class Info1 implements Comparable<Info1>{
private int a;
public Info1(int a){
this.a = a;
}
/**
* 如果小于时返回1 从小到大
* 如果大于时返回1 从大到小
* @param o
* @return
*/
@Override
public int compareTo(Info1 o) {
if (o.a < a)
return 1;
else if(o.a == a)
return 0;
else
return -1;
}
}
}
output:
2
2
2
6
Process finished with exit code 0
网友评论