ArrayList
constructor and iterator
|
|
(only haveadd(Element e)
and addAll(Collection c)
, not addFirst(Element e)
)
List and ArrayList
List
is an interface, but ListArray
is a class
List
不能被构造,即不能创建实例对象,但是可以为List
接口创建一个指向自己的引用,而ArrayList
实现类的实例对象则充当了这个指向List
接口的对象引用
接口和抽象类都不能被实例化
ListArray
extend and implement List
|
|
|
|
|
|
|
|
The Key of the question:
Why using List list = new ArrayList()
, rather than ArrayList alist = new ArrayList()
, just use the interface, easy to change the implements.
List have many implement classes, you could change it to what you want. Just to change this line in your code. Maybe List list = new LinkedList();
ArrayList and LinkedList
ArrayList
和LinkedList
都实现了List
接口的类,他们都是元素的容器,用于存放对象的引用
他们都可以对存放的对象进行增删查改的操作,还可以进行排序
除了对List
接口的实现,他们还实现了其他的接口,由此造就了他们之间的差异
ArrayList
内部使用数组的形式实现了存储,实现了Random接口,利用数组对元素进行访问,因此对元素的随机访问速度非常快
- 因为是数组,所以ArrayList在初始化的时候,有初始大小10,插入新元素的时候,会判断是否需要扩容,扩容的步长是0.5倍原容量,扩容方式是利用数组的复制,因此有一定的开销;
- ArrayList在进行元素插入的时候,需要移动插入位置之后的所有元素,位置越靠前,需要位移的元素越多,开销越大,相反,插入位置越靠后的话,开销就越小了,如果在最后面进行插入,那就不需要进行位移;
add(int index, E element)
Inserts the specified element at the specified position in this list.
LinkedList
内部使用双向链表的结构实现存储,LinkedList
有一个内部类作为存放元素的单元,有3个属性,用来存放元素本身和前后2个单元的引用,另外LinkedList
内部还有一个header属性,用来标识起始位置,LinkedList
的第一个单元和最后一个单元都会指向header,因此形成了一个双向的链表结构。
LinkedList
是采用双向链表实现的。所以它也具有链表的特点,每一个元素(结点)的地址不连续,通过引用找到当前结点的上一个结点和下一个结点,即插入和删除效率较高,只需要常数时间,而get和set则较为低效。
LinkedList的方法和使用和ArrayList大致相同,由于LinkedList是链表实现的,所以额外提供了在头部和尾部添加/删除元素的方法,也没有ArrayList扩容的问题了。另外,ArrayList和LinkedList都可以实现栈、队列等数据结构,但LinkedList本身实现了队列的接口,所以更推荐用LinkedList来实现队列和栈。
public void addFirst(E e)
Inserts the specified element at the beginning of this list.
public void addLast(E e)
Appends the specified element to the end of this list.
This method is equivalent to add(E)
.
Conclude
在需要频繁读取集合中的元素时,使用ArrayList
效率较高,而在插入和删除操作较多时,使用LinkedList
效率较高
StringBuffer
|
|
|
|
|
|
Stack
|
|
Iterate through stack
|
|
Array length
length
– arrays ( int[], double[], String[])
length()
– String related object ( String, StringBuilder, etc)
size()
– Collection Object (ArrayList, Set, etc)
Array initial
|
|
iter items
|
|
StringBuilder
StringBuilder
objects are like String
objects, except that they can be modified. Internally, these objects are treated like variable-length arrays that contain a sequence of characters. At any point, the length and content of the sequence can be changed through method invocations.
|
|
StringBuilder append(float f) / (int i) / (long lng) / (Object obj) / (String s)
StringBuilder delete(int start, int end)
StringBuilder deleteCharAt(int index)
StringBuilder insert()
StringBuilder replace()
StringBuilder reverse()
String toString()
String
String Trim
public String trim()
It returns a copy of this string with leading and trailing white space removed, or this string if it has no leading or trailing white space.
|
|
String contains
|
|
String replace
|
|
char to String
|
|
String subString
|
|
|
|
HashSet
Important Methods in HashSet:
boolean add(E e)
: add the specified element if it is not present, if it is present then return false.void clear()
: removes all the elements from set.boolean contains(Object o)
: return true if element is present in set.boolean remove(Object o)
: remove the element if it is present in set.Iterator iterator()
: return an iterator over the element in the set.
How HashSet internally work?
All the classes of Set interface internally backed up by Map. HashSet uses HashMap for storing its object internally. You must be wondering that to enter a value in HashMap we need a key-value pair, but in HashSet we are passing only one value.
Then how is it storing in HashMap?
Actually the value we insert in HashSet acts as key to the map Object and for its value java uses a constant variable. So in key-value pair all the keys will have same value.
Operator Priority
Operators | Precedence | ||
---|---|---|---|
postfix | expr++ expr-- |
||
unary | ++expr --expr +expr -*expr* ~ ! |
||
multiplicative | * / % |
||
additive | + - |
||
shift | << >> >>> |
||
relational | < > <= >= instanceof |
||
equality | == != |
||
bitwise AND | & |
||
bitwise exclusive OR | ^ |
||
bitwise inclusive OR | ` | ` | |
logical AND | && |
||
logical OR | ` | ` | |
ternary | ? : |
||
assignment | `= += -= *= /= %= &= ^= | = <<= >>= >>>=` |
Queue
Interface Queue
|
|
1.boolean offer(E e)
Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions. When using a capacity-restricted queue, this method is generally preferable to add(E)
), which can fail to insert an element only by throwing an exception.
Parameters:
e
- the element to add
Returns:
true
if the element was added to this queue, else false
2.E remove()
Retrieves and removes the head of this queue. This method differs from poll
) only in that it throws an exception if this queue is empty.
Returns:
the head of this queue
3.E poll()
Retrieves and removes the head of this queue, or returns null
if this queue is empty.
Returns:
the head of this queue, or null
if this queue is empty
Modifier and Type | Method and Description |
---|---|
boolean |
add(E e) Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available. |
E |
element() Retrieves, but does not remove, the head of this queue. |
boolean |
offer(E e) Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions. |
E |
peek() Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty. |
E |
poll() Retrieves and removes the head of this queue, or returns null if this queue is empty. |
E |
remove() Retrieves and removes the head of this queue. |
Integer
Integer.MAX_VALUE = 2147483647
= -2147483647
Integer.MAX_VALUE + 1 == Integer.MIN_VALUE
2147483647+2 = -2147483647
2147483647+3 = -2147483646
Comparable, Comparator
Java provides two interfaces to sort objects using data members of the class:
- Comparable
- Comparator
Example
|
|
We can implement the Comparable interface with the Movie class, and we override the method compareTo()
of Comparable interface.
|
|
extends and implements
Java Extends: When you want to extend a subclass to be extended in inheritance that we use Java extends.
Java Implements: When an implement an interface, we use the keyword implement.
Java Extends vs Implements: In short, extends is for extending a class and implements are for implementing an interface.
As Java doesn’t support multiple interfaces and this problem overcomes by using multiple interfaces.