企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
Java ###String > String s1 = new String(); > String s2 = "billryan"; > int s2Len = s2.length(); > s2.substring(4, 8); // return "ryan" > StringBuilder s3 = new StringBuilder(s2.substring(4, 8)); > s3.append("bill"); > String s2New = s3.toString(); // return "ryanbill" > // convert String to char array > char[] s2Char = s2.toCharArray(); > // char at index 4 > char ch = s2.charAt(4); // return 'r' > // find index at first > int index = s2.indexOf('r'); // return 4. if not found, return -1 >StringBuffer 与 StringBuilder, 前者保证线程安全,后者不是,但单线程下效率高一些,一般使用StringBuilder * * * * * ###链表 ~~~ public class ListNode { public int val; public ListNode next; public ListNode(int val) { this.val = val; this.next = null; } } ~~~ * * * * * ###二叉树 ~~~ public class TreeNode { public int val; public TreeNode left, right; public TreeNode(int val) { this.val = val; this.left = null; this.right = null; } } ~~~ * * * * * ###ArrayList ~~~ List<String> list = new ArrayList<String>(); list.add("abc"); boolean isEmpty = list.isEmpty(); int size = list.size(); boolean isExist = list.contains("abc"); int index = list.indexOf("abc"); int lastIndex = list.lastIndexOf("abc"); Object[] objectArray = list.toArray(); for(Object obj : objectArray){ System.out.println(obj); } String s = list.get(0); String old = list.set(0, "abc"); list.add(0, "ghi"); list.remove(0) ~~~ ###队列 Queue 在 Java 中是 Interface, 一种实现是 LinkedList, LinkedList 向上转型为 Queue, Queue 通常不能存储 null 元素,否则与 poll() 等方法的返回值混淆。优先考虑右侧方法,右侧元素不存在时返回 null . 判断非空时使用 isEmpty() 方法,继承自 Collection ~~~ Queue<Integer> q = new LinkedList<Integer>(); int qLen = q.size(); // get queue length ~~~ | 0:0 | Throws exception | Returns special value | | --- | --- | --- | | Insert | add(e) | offer(e) | | Remove | remove() |poll()| |Examine| element()| peek()| * * * * * ###双端队列 Java 在1.6之后提供了 Deque 接口,既可使用 ArrayDeque ( 数组) 来实现,也可以使用 LinkedList ( 链表) 来实现。前者是一个数组外加首尾索引,后者是双向链表。 ~~~ Deque<Integer> deque = new ArrayDeque<Integer>(); Deque<Integer> deque = new LinkedList<Integer>(); ~~~ |/||Throws exception| Special value| Throws exception| Special value| | --- | --- | |Insert| `addFirst(e)`| `offerFirst(e)`| `addLast(e)`| `offerLast(e)`| |Remove| `removeFirst()`| `pollFirst()`| `removeLast()`| `pollLast()`| |Examine |`getFirst()`| `peekFirst()` |`getLast()`| `peekLast()`| 其中 offerLast 和 Queue 中的 offer 功能相同,都是从尾部插入。 * * * * * ###栈Stack Methods > boolean isEmpty() - 判断栈是否为空 > E peek() - 取栈顶元素,不移除 > E pop() - 移除栈顶元素并返回该元素 > E push(E item) - 向栈顶添加元素 * * * * * ###Set Set 与 Collection 具有安全一样的接口,通常有 HashSet , TreeSet 或 LinkedHashSet 三种实现。 HashSet 基于散列函数实现,无序,查询速度最快; TreeSet 基于红-黑树实现,有序。在不允许重复元素时可当做哈希表来用。 ~~~ Set<String> hash = new HashSet<String>(); hash.add("billryan"); hash.contains("billryan"); ~~~ * * * * * ###hashmap Java 的实现中 Map 是一种将对象与对象相关联的设计。常用的实现有 HashMap 和 TreeMap ,HashMap 被用来快速访问,而 TreeMap 则保证『 键』 始终有序。Map 可以返回键的 Set, 值的 Collection,键值对的 Set. ~~~ Map<String, Integer> map = new HashMap<String, Integer>(); map.put("bill", 98); map.put("ryan", 99); boolean exist = map.containsKey("ryan"); // check key exists in map int point = map.get("bill"); // get value by key int point = map.remove("bill") // remove by key, return value Set<String> set = map.keySet(); // iterate Map for (Map.Entry<String, Integer> entry : map.entrySet()) { String key = entry.getKey(); int value = entry.getValue(); // do some thing } ~~~