java常用方法整理

// List and Array
// convert array to list
list = Arrays.asList(arr);
list = Arrays.asList(1, 2, 3); 
// convert int[] to list
List<Integer> list = IntStream.of(arr).boxed().collect(Collectors.toList());
// convert list to array
T[] arr = list.toArray(new T[]);
// convert List<Integer> to int[]
int[] arr = list.stream().mapToInt(i->i).toArray();
unmodifiableSet = Collections.unmodifiableSet(set)
str = String.valueOf(chars);
chars = str.toCharArray();

// Queue
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.add(p);  // insert element
p = queue.poll();  // poll element

// PriorityQueue to implement MaxHeap or MinHeap
        PriorityQueue<Map.Entry<Integer, Integer>> maxHeap 
            = new PriorityQueue<>(nums.length, 
                                  new Comparator<Map.Entry<Integer, Integer>>() {
                public int compare(Map.Entry<Integer, Integer> a
                                   , Map.Entry<Integer, Integer> b) {
                    return b.getValue() - a.getValue();
                } 
            });

// String
String[] words = str.split(" ");
String s = String.valueOf(int);
int i = Integer.parseInt(s);
// StringBuilder
sb.deleteCharAt(i);
sb.delete(start, end);
sb.setLength(len);  // remove trailing chars

// Map
Integer preValue = map.get(i); // for a Map<T, Integer>, return null if no value before

// Set
public static final Set<Integer> primes
        = new HashSet<>(Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19));

// Comparator
Collections.sort(intervals, new Comparator<Interval>() {
    public int compare(Interval a, Interval b) {
        return a.start - b.start;
    }
});

// Math
java.util.Random random = new java.util.Random();
int x = random.nextInt(int bound);  // bound - the upper bound (exclusive). Must be positive. 
int val = Integer.parseInt(s, 16);  // convert hex str to decimal integer
return Integer.toHexString(val);  // convert decimal integer to hex str

运算符优先级

优先级

ASCII

ASCII

Tips

  • 由于Leetcode lib中不包含Pair,如果成员类型相同,可以使用array来代替Pair。

String#split

Java中的 split 函数是用于按指定字符(串)或正则去分割某个字符串,结果以字符串数组形式返回;

例如:

String str="1234@abc";
String[] a = str.split("@");
System.out.println("处理结果: "+a[0]+","+a[1]);   //输出的是: 处理结果: 1234,abc

对于分割的字符(串),通常是常见,普通的,没什么问题;

但是对某些特殊字符,如果字符(串)正好是正则的一部分,则需要转义才能使用,

这些字符有** | , + , * , ^ , $ , / , | , [ , ] , ( , ) , - , . , **等, 因它们是正则表达式中的一部分, 所以如果想用该字符本身, 这些字符需要进行转义才能表示它本身;

例如:

想用 | 竖线去分割某字符,因 | 本身是正则表达式中的一部分,所以需要 \ 去转义,因转义使用 , 而这个 \ 正好也是正则表达式的字符,所以还得用一个 \ , 所以需要两个 \。

String str="5678|XYZ";
String[] b = str.split("\\|");  //注意这里用两个 \\,而不是一个\
System.out.println("处理结果: "+b[0]+","+b[1]);   //输出的是: 处理结果: 5678,XYZ

再来看看:

String str="5678|XYZ";
String[] b = str.split("|");  //注意直接使用|,该字符是正则表达式的一部分,
String x="处理结果: ";
for(int i=0;i<b.length;i++){
    x=x+b[i]+",";
}
System.out.println(x);   //输出的是: 处理结果: 5,6,7,8,|,X,Y,Z,

可能我们人为主观感觉是用 | 来分割希望得到 5678 和 XYZ,因用特殊字符,实际结果是得到意外的结果。

LinkedHashSet

在Java中可以利用LinkedHashSet来当做DoublyLinkedList。

A LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all elements. When the iteration order is needed to be maintained this class in used. When iterating through a HashSet the order is unpredictable, while a LinkedHashSet lets us iterate through the elements in the order in which they were inserted.when cycling through LinkedHashSet using an iterator, the elements will be returned in the order in which they were inserted.

LinkedHashSet内部应该是一个HashMap + DoublyLinkedList,其特性为:

  • 包含HashSet的一切特性:unique value,O(1) get, put
  • remains insertion order:用iterator遍历时会按照插入顺序从老到新。

题目:LFU Cache

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容