刷算法,这些api不可不知!

集合 List

List l = new ArrayList();

l.size(); // 集合大小
l.add("adsd"); // 添加元素
l.get(int index); // 根据index获取元素
l.contains("fff") // 是否包含
l.remove("adsd") // 删除第一个出现的元素
l.remove(int index) // 删除对应index的元素
l.clear(); // 清空
l.isEmpty(); // 判断是否为空
l.toArray(); // 转换为Array
l.subList(int from_index, int to_index)

集合 Set

Set<String> s = new HashSet<>();

s.size(); // 集合大小
s.add("asdsa"); // 添加
s.remove("asd"); // 删除
s.contains("fff") // 是否包含
s.clear(); // 清空
s.isEmpty(); // 判断是否为空

栈 Stack

FILO结构

Stack<Integer> s = new Stack<>();

// 添加元素
s.add(2);  // 优先使用,返回值为boolean 表明添加成功或失败
s.push(1); // 不返回值

// 输出元素
s.pop(); // 推出顶部的元素
s.peek(); // 只是查看一下顶部的元素

// 判断是否为空,返回boolean
s.empty();

队列 Queue

FIFO结构

Queue<Integer> q = new LinkedList<>(); // 是LinkedList实现的Queue

// 添加元素
q.offer(2); // 优先使用,返回boolean,表示添加成功或失败
q.add(2); // 只添加,不返回值

// 输出元素
q.poll(); // 弹出队首的元素
q.peek(); // 只是查看一下队首的元素

// 判断是否为空,返回boolean
q.empty();

哈希表 HashMap

Map<Integer, Integer> m = new HashMap<>();

// 添加
m.put(1, 292);  // 如果第一次建立映射关系,返回 null
								// 如果替代了之前的映射关系,返回之前的value
// 查看
m.get(1);

// 删除
m.remove(1);   // 如果存在就删除,并返回value,不存在返回[???]

// 判断是否存在,返回boolean
m.containsKey(1); // key
m.containsValue(292); // value

// 判断是否为空
m.empty();

数学 Math

Math.max(long a, long b)
Math.sqrt(double a)
Math.abs(double a)
Math.pow(double a, double b)
Math.ceil(double x)
Math.floor(double x)
Math.round(double x)