文章目录
- 常用Math库函数
- 栈Stack
- 队列Queue
- 动态数组ArrayList
- 哈希表hashmap
- 其他常用函数
常用Math库函数
abs:求一个数的绝对值
Math.abs(-4);//4
min/max:求两数最值
Math.min(2,4);//2
Math.max(2,4);//4
log:对数
Math.log(4);//以e为底,4的对数
Math.log10(4);//以10为底,4的对数
sqrt:开平方根
Math.sqrt(4);//2
pow:指数
Math.pow(3,4);//3的四次方
exp:e的次方
Math.exp(3);//e的三次方
expm1:e的次方-1
Math.expm1(4);//e的四次方-1
floor: 求小于参数的最大整数。
Math.floor(-4.2);//-5.0
ceil: 求大于参数的最小整数。
Math.ceil(5.6);//6.0
round: 对小数进行四舍五入后的结果。
Math.round(-4.6);//-5
栈Stack
栈的初始化
Stack<Integer> s=new Stack<Integer>();
栈Stack的插入和弹出
s.push(1);//插入栈顶值1
s.pop();//弹出
返回栈顶值
minStack.top();
empty(常用来判断栈输出完为空的boolean值)
s.empty():
栈Stack中访问栈顶
s.peek();
队列Queue
不允许添加null元素。
初始化
Queue<Integer> q=new Queue<Integer>();
插入和弹出
q.offer(1);// 插入队尾值为1
q.poll();//队头出值
访问
q.peek();
动态数组ArrayList
动态数组ArrayList的初始化
ArrayList<Integer> a=new ArrayList<Integer>();
Collections.fill(a,100);//初始值为100
动态数组ArrayList的增删
a.add(1);
a.add(2,4);//将4插入到第二个位置
a.remove(2);//删除下标为2的
动态数组ArrayList的排序、最大值、最小值
Collections.sort(a);
Collections.max(a);
Collections.min(a);
动态数组ArrayList的遍历
for(int i=0;i<a.length;i++){
a.get(i);//访问数组下标为i的值
}
哈希表hashmap
创建一个HashMap对象:
HashMap<Integer, String> map = new HashMap<Integer, String>();
//创建类型为整型(Integer)的key和字符串(String)类型的 value:
添加元素:put方法
map.put(1, "baidu");//在map的键1中添加字符串'baidu'
//put(key-value)
map.put(2, "taobao");
System.out.println(Sites);//输出为:{1=baidu, 2=taobao}
访问元素:get方法
System.out.println(map.get(1));//执行输出之前添加的:baidu
//get(key)
计算大小:size() 方法
System.out.println(map.size());//输出为2
删除元素:remove方法
map.remove(1);//删除了键1的数据,输出为{2=taobao}
//remove(key)
删除所有元素:clear()
map.clear();//输出为:{}
检查 hashMap 中是否存在指定的 key 对应的映射关系:containsKey()方法
hashmap.containsKey(Object key)//如果 hashMap 中存在指定的 key 对应的映射关系返回 true,否则返回 false。
常见hash表函数
clear() | 删除 hashMap 中的所有键/值对 |
clone() | 复制一份 hashMap |
isEmpty() | 判断 hashMap 是否为空 |
size() | 计算 hashMap 中键/值对的数量 |
put() | 将键/值对添加到 hashMap 中 |
putAll() | 将所有键/值对添加到 hashMap 中 |
putIfAbsent() | 如果 hashMap 中不存在指定的键,则将指定的键/值对插入到 hashMap 中。 |
remove() | 删除 hashMap 中指定键 key 的映射关系 |
containsKey() | 检查 hashMap 中是否存在指定的 key 对应的映射关系。 |
containsValue() | 检查 hashMap 中是否存在指定的 value 对应的映射关系。 |
replace() | 替换 hashMap 中是指定的 key 对应的 value。 |
replaceAll() | 将 hashMap 中的所有映射关系替换成给定的函数所执行的结果。 |
get() | 获取指定 key 对应对 value |
getOrDefault() | 获取指定 key 对应对 value,如果找不到 key ,则返回设置的默认值 |
forEach() | 对 hashMap 中的每个映射执行指定的操作。 |
entrySet() | 返回 hashMap 中所有映射项的集合集合视图。 |
keySet() | 返回 hashMap 中所有 key 组成的集合视图。 |
values() | 返回 hashMap 中存在的所有 value 值。 |
merge() | 添加键值对到 hashMap 中 |
compute() | 对 hashMap 中指定 key 的值进行重新计算 |
computeIfAbsent() | 对 hashMap 中指定 key 的值进行重新计算,如果不存在这个 key,则添加到 hasMap 中 |
computeIfPresent() | 对 hashMap 中指定 key 的值进行重新计算,前提是该 key 存在于 hashMap 中。 |
其他常用函数
反转函数
String str="anssda";
str2 = new StringBuffer(str2).reverse().toString();//adssna
升序函数(可以搭配反转函数实现降序)
int[]a=new int[]{5,4,3,2,1};
Arrays.sort(a,0,5); //1,2,3,4,5
//Arrays.sort(数组名,起始下标,终止下标);
初始化数组函数
int[]a=new int[]{5,4,3,2,1};
fill(a,100)//所有元素赋值为100