This post contains some Java language feature questions. Updating…
在此收录一些关于Java语言特性问题。更新中…
Principle Questions
Immutable Objects
- Immutable objects are also useful because they are inherently thread-safe.
Data Structure
PriorityQueue
- Compared with
TreeSet()
:- TreeSet is a set and no duplicate element;
- PriorityQueue iterator does not guarantee order;
- PriorityQueue is preferred when only need peek top element cases.
- PriorityQueue is implemented by Binary Min Heap.
add()
throw exception when failed,offer()
returnfalse
.element()
throw exception when failed,peek()
returnnull
.poll()
,size()
,remove()
,clear()
,toArray()
.- PQ does not accept
null
element.
HashMap
- Starting from Java 8, HashMap starts with storing Entry objects in linked list but after the number of items in a hash becomes larger than a certain threshold, the hash will change from using a linked list to a balanced tree. Providing $O(\log n)$ worse case performance.
put
method returns the previous value associated with key, ornull
if there was no mapping for key.
LinkedList
remove()
is notO(n)
as some blogs stated. The remove operation for a linked list isO(1)
, but it requires to find the to-be-removed node. So the specificremove()
method in Java LinkedList isO(n)
.- But the
remove()
for iterator isO(1)
.
LinkedHashMap
Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order). Note that insertion order is not affected if a key is re-inserted into the map. (A key k is reinserted into a map m if m.put(k, v)
is invoked when m.containsKey(k)
would return true immediately prior to the invocation.)
This implementation spares its clients from the unspecified, generally chaotic ordering provided by HashMap (and Hashtable), without incurring the increased cost associated with TreeMap.
TreeMap
- Implemented in Red-Black Tree;
Tuple
Defined by user:
1 | public class Tuple<X, Y> { |
Pair
1 | Pair<Integer, String> pair = new Pair<>(1, "One"); |
String
- StringBuffer is thread safe while StringBuilder is not.
Reasons for Immutable
- The string is Immutable in Java because String objects are cached in String pool. Since cached String literals are shared between multiple clients there is always a risk, where one client’s action would affect all another client.
- Since Strings are very popular as HashMap key, it’s important for them to be immutable so that they can retrieve the value object which was stored in HashMap. Mutable String would produce two different hash-codes at the time of insertion and retrieval if contents of String was modified after insertion, potentially losing the value object in the map.
Basics
Double
Double Comparison
1 | double a = 1.000001; |
Enhanced For Loop
Enhanced for loop makes a copy of each element in the iterative.
File/Cmd Read
1 | public class ReadWriteFile |
Override PriorityQueue Comparator
1 | Comparator<Integer> myComoparator = new Comparator<Integer>() { |
Or
1 | new PriorityQueue<Integer>(Collections.reverseOrder()); |
Or
1 | PriorityQueue<int[]> pq = new PriorityQueue<int[]>((a, b) -> A[a[0]] * A[b[1]] - A[a[1]] * A[b[0]]); |
Override Collections Sort
1 | Collections.sort(list, new Comparator<int[]>() { |
Exception
Syntax
final
- By using the final keyword when declaring a variable, the Java compiler won’t allow to change the value of that variable. Instead, it will report a compile-time error: