Java Cheat Sheet For Interview
Java Cheat Sheet For Interview
map.isEmpty();
map.size();
HashSet<Integer> set = new HashSet<Integer>();
set.add(10);
set.remove(10);
if(set.contains(10)){
}
set.size();
set.isEmpty();
setA.retainAll(setB); // setA keeps the intersection of original setA and setB;
setB.removeAll(setC); // Removes from this set all of its elements that are
contained in the specified collection (setB - setC)
setC.addAll(setD); // union two sets of setC and setD
setC.containsAll(setD); // Returns true if this set contains all of the elements of
specified collection
Object[] arr = setA.toArray(); // Returns an array containing all of the elements
in this set.
import java.util.Deque;
Deque<Integer> dq = new LinkedList<Integer>(); // Deque is usually used to
implement monotone queue
dq.addFirst(); // dq.offerFirst();
dq.addLast(); // dq.offerLast();
dq.peekFirst(); //
dq.peekLast();
dq.pollFirst(); // dq.removeFirst();
dq.pollLast(); // dq.removeLast();
try{
BufferedWriter out = new BufferedWriter(new FileWriter(outputfile));
for(String str : map.keySet()){
out.write(str + " " + map.get(str));
out.newLine();
}
out.close(); // close the file
}catch (IOException e)
{
e.printStackTrace();
}
# The most notable pros of the ConcurrentSkipListMap are the methods that
# can make an immutable snapshot of its data in a lock-free way.
Collection<? extends Fruit> // what we know is that the collection is one type of
Fruit. We can get but cannot add into it in case that added fruit violates the
current type
Collection<? super Banana> // what we know is fruits in collection belong to parent
class of banana. We can add fruit as long as we know the added fruit is parent
class of banana. But we can't get one from it because we don't know what is excatly
the type is.
Scatter:
Scattering read 指的是从通道读取的操作能把数据写入多个 buffer,也就是 sctters 代表了数据从
一个 channel 到多个 buffer 的过程。
Gather:
gathering write 则正好相反,表示的是从多个 buffer 把数据写入到一个 channel 中。
Scatter/gather 在有些场景下会非常有用,比如需要处理多份分开传输的数据。举例来说,假设一个消息包含
了 header 和 body,我们可能会把 header 和 body 保存在不同独立 buffer 中,这种分开处理 header 与
body 的做法会使开发更简明。
SelectionKey 对象包含以下属性:
The interest set;
The ready set;
The Channel;
The Selector;
An attached object (optional);
访问 Ready 的 Channel
You can access the ready channels via the "selected key set", by calling the
selectors selectedKeys() method.
Set<SelectionKey> selectedKeys = selector.selectedKeys();
Close()函数
When you are finished with the Selector you call its close() method.
This closes the Selector and invalidates all SelectionKey instances registered with
this Selector.
The channels themselves are not closed.