Java中常用的工具类:
List
List是一个接口,而ArrayList是List接口的一个实现类。ArrayLIst是继承List接口的一个实现类
1.ArrayList
import java.util.ArrayList;
ArrayList<String> arr=new ArrayList<>();
for(int i=0; i<=5; i++)
arr.add(i+"");//i转换为字符串了 0 1 2 3 4 5
arr.add(index, element);//在index前面一个位置增加新元素
arr.set(intdex, element);//将下标为index的位置重设为该元素
arr.get(index);//index为下标从0开始
arr.clear();//清空
arr.contains(element);
arr.size();
arr.isEmpty();
2.LinkedList
定义:双向链表
线程不安全
ArrayList和LinkedList
-
ArrayList使用在查询比较多,但是插入和删除比较少的情况,而LinkedList用在查询比较少而插入删除比较多的情况
-
两者都是实现了List接口的容器类,ArrayList是实现了基于动态数组的数据结构,LinkedList是基于链表结构。
2.5Vector
3.HashSet
-
HashSet 基于 HashMap 来实现的,是一个不允许有重复元素的集合.
-
HashSet 允许有 null 值。HashSet 是无序的,即不会记录插入的顺序。
import java.util.HashSet; HashSet<String> set=new HashSet<>(); set.add("10"); System.out.println(set); set.remove(10); System.out.println("是否包含100 "+ set.contains("100")); set.clear(); System.out.println(set);
4.HashMap
-
元素也不能重复
import java.util.HashMap;
HashMap<String, Integer> map= new HashMap<>();
map.put("键", 100);
map.put("键", 1000);
map.put("键1", 2000);
map.put("键2", 3000);
System.out.println(map.get("键"));
for(HashMap.Entry<String, Integer> set:map.entrySet()){
System.out.println(set.getKey()+"="+set.getValue());
}
5.Queue
Queue和Deque都是接口,需要对象?——>LinkedList
import java.util.*;——>import java.util.Queue; import java.util.LinkedList;
Queue<String> q= new LinkedList<>();
q.add("1");
q.add("2");
q.add("3");
q.add("4");
q.add("5");
String peek=q.peek();
System.out.println(peek);//查找队列头
String poll1=q.poll();//把元素从队列头拉出来
System.out.println(poll1);
System.out.println(q);
String poll2=q.poll();
System.out.println(poll2);
System.out.println(q);
6.Deque
双向队列
import java.util.*;——>import java.util.Deque; import java.util.LinkedList;
Deque<String> qq=new LinkedList<>();
qq.add("1");
qq.add("2");
qq.add("3");
qq.add("4");
qq.add("5");
qq.add("6");
qq.add("7");
System.out.println(qq);//默认是取队列头元素——pollFirst();
System.out.println(qq.peekFirst());
System.out.println(qq.peekLast());
System.out.println(qq.pollFirst());//默认pollFirst
System.out.println(qq.pollLast());
System.out.println(qq.pop());//——>pollFirst()
System.out.println(qq);
//可以当做栈来使用,双向的队列
7.Stack
import java.util.Stack;
Stack<String> aa= new Stack<>();
aa.push("1");
aa.push("2");
aa.push("3");
aa.push("4");
aa.push("5");
aa.push("6");
aa.push("7");
System.out.println(aa.pop());
System.out.println(aa);//打印栈内所有元素
System.out.println(aa.peek());//查看栈顶元素
JDK源码:
一、查看源码
Project Structure——>找到JDK位置——>根据位置找到文件夹——>找到src.zip并解压——>在idea中打开解压得到的文件夹即可查看
二、源码解析
-
java.lang包中的类,也是Java的基础类
String类
public char charAt(int index) {
if ((index < 0) || (index >= value.length)) {
throw new StringIndexOutOfBoundsException(index);
}
return value[index];//返回index位置的元素
}
补充:==和equals区别
1.==操作符专门用来比较变量的值是否相同。
-
基本数据类型:比较的是他们的值是否相同。
-
引用数据类型:比较的是他们的内存地址是否同一地址。
2.equals方法常用来比较对象的内容是否相同。
答案:
java.util(工具包)
1.ArrayLIst
- 初始化
默认容量空元素数据
ArrayList的基础是数组,底层维护一个 Object 数组。
-
构造方法
-
其他方法举例
public int size() {//返回当前大小 return size; } public boolean isEmpty() { return size == 0; } public boolean contains(Object o) {//判断是否包含o return indexOf(o) >= 0; }
从数组首位查找指定元素,如果查找的为null只返回ArrayList中Object[] elementData存储的第一个为null的元素下标,没有找到指定的元素则返回-1
public int indexOf(Object o) { if (o == null) { for (int i = 0; i < size; i++) if (elementData[i]==null) return i; } else { for (int i = 0; i < size; i++) if (o.equals(elementData[i])) return i; } return -1; }
........
2.Vector
可以对比一下ArrayList:
相同:
都是List接口的实现类,都是有序不唯一的集合。
不同:
(1)ArrayList 线程不安全,而Vector是安全的
(2)扩容大小不同,而且Vector可以增加指定capacityIncrement大小的空间,ArrayLIst则不可以。
Arraylist: 扩大到原来的1.5倍
Vector:扩大为原来的2倍
3.LinkedList
源码继承结构:
LinkedList的实现:
4.HashMap
默认初始化容量为16
最大容量为2 ^ 30 ≈ 10 ^ 8
public V remove(Object key) {
Node<K,V> e;
return (e = removeNode(hash(key), key, null, false, true)) == null ?
null : e.value;
}
public void clear() {//清空就是全部元素重新清零
Node<K,V>[] tab;
modCount++;
if ((tab = table) != null && size > 0) {
size = 0;
for (int i = 0; i < tab.length; ++i)
tab[i] = null;
}
}