String类
char charAt(int index)
String substring(int beginIndex, int endIndex) //截取子字符串,endIndex可省略
String[] split(String regex, int limit) //分割字符串为字符串数组,limit可省略
String strip() //可以去除字符串前后的“全角和半角”空白字符
boolean startsWith(String prefix)
boolean endsWith(String suffix)
char[] toCharArray() //转为char数组,便于使用增强for
String(char value[], int offset, int count) //char数组转String
int compareTo(String anotherString) //比较String
String toLowerCase() //字母都变成小写
String toUpperCase() //字母都变成大写
static int Integer.parseInt(String s) //字符串转数字,Integer静态方法
Character类
static boolean isLetterOrDigit(char ch)
static char toLowerCase(char ch)
java.util.Collections类常用静态方法
static <T> void sort(List<T> list, Comparator<? super T> c) //排序
static void reverse(List<?> list) //反转
java.util.Arrays类常用静态方法
//1.asList方法接受的泛型参数,因此不能用于基本类型
//2.Arrays.asList()方法返回的对象是Arrays的内部类,对list的操作仍然反映在原数组上,因此这个list是定长的,不支持add、remove操作
static <T> List<T> asList(T... a) //数组转List(两大缺点)
static <T> void sort(T[] a, Comparator<? super T> c) //排序,可以试试二维数组
static <T> boolean equals(T[] a, T[] a2, Comparator<? super T> cmp) //比较两个数组是否相等的方法
static void fill(Object[] a, Object val) //用于一维数组、二维数组的初始化或者填充
static <T> T[] copyOf(T[] original, int newLength) //复制新数组,并指定长度
static <T> T[] copyOfRange(T[] original, int from, int to) //原始数组from 起始位置下标to 终止位置下标 (不含to,即左闭右开)
//System类复制数组
static native void arraycopy(Object src, int srcPos,
Object dest, int destPos,
int length);
Integer[] tmp = Arrays.stream(arr1).boxed().toArray(Integer[]::new);//int[]转Integer[]
int[] arr2 = Arrays.stream(tmp).mapToInt(a -> a).toArray();//Integer[]转int[]
比较参数Comparator<? super T> c可以使用下述举例方法,两者作用一样
//匿名比较器
new Comparator<ListNode>(){
@Override
public int compare(ListNode a, ListNode b) {
return a.val - b.val;
}
};
// java8新特性
(a, b)->(a.val - b.val);
(a, b)->{
return a.val - b.val;
}
Collection接口
boolean isEmpty() //判断集合是不是空的
boolean contains(Object o) //判断是否存在
boolean add(E e) //增加元素
boolean remove(Object o) //删除元素
<T> T[] toArray(T[] a);//转数组(转不了基本数据类型)
TreeSet类
TreeSet(Comparator<? super E> comparator) //带比较器的构造方法
//lower、floor、ceiling 和 higher
//分别返回小于、小于等于、大于等于、大于给定元素的元素,如果不存在这样的元素,则返回 null。
E lower(E e) E floor(E e) E ceiling(E e) E higher(E e)
E first() //返回Set的第一个元素
E last() //返回Set的最后一个元素
NavigableSet<E> subSet(E fromElement, boolean fromInclusive,
E toElement, boolean toInclusive)
List接口
E remove(int index) //删除该索引元素
List<E> subList(int fromIndex, int toIndex) //截取子List
//可通过创建新的对象来复制List,传入Collection对象
ArrayList(Collection<? extends E> c)
LinkedList(Collection<? extends E> c)
Stack类extends Vector类
E push(E item) //压栈
E pop() //出栈
E peek() //取栈顶
int search(Object o) //搜索元素,返回位置
Queue接口
Queue<Integer> queue = new LinkedList(); //LinkedList实现DeQueue
boolean offer(E e) //进队
E poll() //出队
E peek() //取队头
int search(Object o) //搜索元素
PriorityQueue优先队列
PriorityQueue(Comparator<? super E> comparator) //构造方法
//举例
PriorityQueue<ListNode> pq = new PriorityQueue<>(
lists.length, (a, b)->(a.val - b.val));
Map接口(最上层接口)
V put(K key, V value) //
V get(Object key)
V getOrDefault(Object key, V defaultValue) //返回key对应的value, Key 不存在或者对应的值是 null,则返回第二个参数即默认值。要注意,这个默认值不会放入 Map.
V computeIfAbsent(K key,
Function<? super K, ? extends V> mappingFunction)//返回key对应的value, Key 不存在或者对应的值是 null,则调用 Function 来产生一个值,然后将其放入 Map.
V putIfAbsent(K key, V value) //不存在才放入Map,和computeIfAbsent类似
Map的遍历
//常用方式
for (String key : map.keySet()) {
System.out.println("key= "+ key + " and value= " + map.get(key));
}
// 第二种:
Iterator<Map.Entry<Integer, String>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Integer, String> entry = it.next();
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
}
//第三种:推荐,尤其是容量大时
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
}
// 第四种:
for (String v : map.values()) {
System.out.println("value= " + v);
}