常用的类:
一、 StringBuffer类
StringBuffer类相当于字符串变量,它是线程安全的,所以效率较低。
(1)StringBuffer类的构造方法
1.StringBuffer():建立一个长度为16个字符的空的StringBuffer.
2.StringBuffer(int length): 建立指定长度的空的StringBuffer.
3.StringBuffer(String str): 以指定String去初始化StringBuffer,并提供另外16个字符的空间供再次分配...
(2) StringBuffer类的常用方法
1. public StringBuffer append (CharSequence s): 将指定CharSequence 追加到本StringBuffer上。
2. public int capacity(): 返回此StringBuffer 对象的最大容量,即总的可供分配的字符个数。
3. public int length(): 返回此StringBuffer对象的实际长度....
public class StringBufferTest {
public static void main(String[] args){
StringBuffer test = new StringBuffer();
//StringBuffer 的实际长度
System.out.println(test.length()); //输出0
//StringBuffer 的最大字符容量
System.out.println(test.capacity()); //输出16
test.append("123456");
System.out.println(test.length()); //6
System.out.println(test.capacity()); //16
System.out.println(test); //123456
StringBuffer test2 = new StringBuffer("012345");
System.out.println(test2.length()); //6
System.out.println(test2.capacity()); //22 -->
}
}
String, StringBuffer, StringBuilder 三者的区别?
(1)从线程安全性对比:
1.String不可变,它是线程安全的;
2. StringBuffer 是线程安全的,内部使用synchronized进行同步;
3. StringBuilder 不是线程安全的。
(2) 从可变性对比:
1. String 是不可变的;
2. StringBuffer 和 StringBuilder 是可变的。
二、集合类:
(1) Collection接口和Iterator接口:
Collection是集合框架中的根接口,对一些基本的集合操作方法(Add、Get、Delete、Modify等)进行了约束性规定。
Collection 接口及其实现类主要用来盛放对象(Object)。
Collection接口定义如下:
public interface Collection<E> extends Iterable<E>
Collection接口的3个子接口 List, Set, Queue的定义:
1. public interface List<E> extends Collection <E>: 相当于线性表,其实现类实现了有序、元素可重复的数据结构。
2. public interface Set<E> extends Collection <E>: 相当于数学上的集合,其实现类实现了无序、元素不可重复的数据结构。
3. public interface Queue<E> extends Collection <E>: 队列,实现先进先出的数据结构。
(2) Iterator接口:
public interface Iterator <E>
Iterator是专门为集合的遍历而设计的,因为通用性,其功能比较简单,在使用中也有一些限制。
例如:只能单向移动,不能添加元素等。 例子:
public class IteratorTest {
public static void main(String[] args) {
Collection<String> collection = new ArrayList<String>();
collection.add("1号 ");
collection.add("2号");
//使用Iterator 遍历集合
Iterator<String> test = collection.iterator();
String string = null;
while(test.hasNext()) {
string = test.next();
System.out.println(string); //输出1号 2号
}
//使用foreach遍历集合
for(String s : collection) {
System.out.println(s); //输出1号 2号
}
}
}
(3) List 接口及其子类
List接口定义了一个有序、元素可重复的集合的实现要求,定义如下:
public interface List<E> extends Collection<E>
List相当于线性表或动态数组, 它默认按照元素的添加顺序作为元素的索引,与数组相同,索引从0开始。 List 允许通过索引来访问List中的元素。
List 在内存中采取连续存储,适合元素的随机存取,但在大量插入元素和删除元素时效率会下降。
1.ArrayList
根据ArrayList类的JDK源码可以知道ArrayList 是通过封装了一个Object [ ] 数组来实现的List类。
ArrayList 类不是线程安全的,查询快、增删慢、轻量级。 ArrayList类的定义如下:
public class ArrayList <E> extends AbstractList <E> implements List<E>, RandomAccess, Cloneable, Serializable
例子:
import java.util.ArrayList;
public class ArrayListTest {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
list.add("123");
list.add("3.14");
list.add("test");
System.out.println(list.get(0)); //123
list.remove(2);
System.out.println(list.size()); //2
for(int i = 0; i<list.size(); i++) {
System.out.println(list.get(i)); //123 3.14
}
System.out.println(list); // 123 3.14
}
}
2.LinkedList
LinkedList 是功能强大、使用广泛的Java集合实现类,根据 LinkedList类的JDK源码可以知道 LinkedList是通过链表来实现的List类。 因此, 它增删元素快,但查询速度慢。
例子:
public class LinkedListTest {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<String>();
list.add("a");
list.add("b");
list.addFirst("5");
list.addLast("f");
list.add(1, "in");
System.out.println(list); //[5, in, a, b, f]
list.addFirst("head");
list.addLast("tail");
list.push("00");
System.out.println(list.pop()); //00
System.out.println(list.peek()); //head 获取但不移除除此列表的头
System.out.println(list.size()); //7
System.out.println(list); // [head, 5, in, a, b, f, tail]
}
}
(4) Set 接口及其子类
Set 是数学中的集合在Java中的实现,具有无序性和唯一性。 Set的接口定义如下:
public interface Set <E> extends Collection <E>
Set的子类实现:
1. HashSet: 基于哈希表(Hash技术)实现,优点在于能够快速定位元素; HashSet类不是线程同步的,不支持有序性操作,并且失去了元素的插入顺序信息,也就是说使用Iterator遍历HashSet得到的结果是不确定的。
2. TreeSet: 采用自平衡的排序二叉树(红黑树)实现有序的集合,且可以保证元素的互异性,并且以自然顺序的升序进行排列。 例如根据一个范围查找元素的操作, 但是它的查找效率不如HashSet, HashSet查找的时间复杂度位 O(1),TreeSet的则为O(logN)。
示例:
import java.util.Iterator;
import java.util.TreeSet;
public class TreeSetTest {
public static void main(String[] args) {
TreeSet<String> t = new TreeSet<String>();
t.add("5");
t.add("g");
t.add("4");
t.add("3");
t.add("4");
t.add("q");
t.add("z");
t.remove("z");
System.out.println(t); // [3, 4, 5, g, q]
Iterator<String> iterator = (Iterator<String>)t.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
(5) Map 接口及其子类
Map 主要用来存储键值对(<Key / Value>)对象的集合。 一个Map的键 (Key)是唯一的。
在Map中每个键和值是一一对应的,所以可以根据键(Key)快速查询出对应的值(Value)。 Map接口的定义如下:
public interface Map <K, V>
1.HashMap: 基于哈希表实现。
下面是例子:
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
class Student{
private int num = 0;
private String name;
private double score;
public Student() {
}
public Student(int num, String name, double score) {
super();
this.num = num;
this.name = name;
this.score = score;
}
@Override
public String toString() {
return "Student [num=" + num + ", name=" + name + ", score=" + score + "]";
}
}
public class HashMapTest {
public static void main(String[] args) {
HashMap<Integer, Student> hash = new HashMap<Integer, Student>();
hash.put(2019001, new Student(2019001, "test1", 80));
hash.put(2019002, new Student(2019002, "test2", 85));
hash.put(2019003, new Student(2019003, "test3", 90));
System.out.println(hash.size()); // 输出3
System.out.println(hash.get(2019002)); // Student [num=2019002, name=test2, score=85.0]
//遍历访问
// 通过遍历keySet访问value
Set<Integer> set = hash.keySet();
Iterator<Integer> it = set.iterator();
Integer key;
while(it.hasNext()) {
key = it.next();
System.out.println(key + " : " + "value: " + hash.get(key));
/**
* 2019002 : value: Student [num=2019002, name=test2, score=85.0]
2019003 : value: Student [num=2019003, name=test3, score=90.0]
2019001 : value: Student [num=2019001, name=test1, score=80.0]
*/
}
//For 语句增强循环遍历
for(Integer key1 : hash.keySet()) {
System.out.println(key1 + " : " + hash.get(key1));
}
/**
* 2019002 : Student [num=2019002, name=test2, score=85.0]
2019003 : Student [num=2019003, name=test3, score=90.0]
2019001 : Student [num=2019001, name=test1, score=80.0]
*/
}
}
2.TreeMap: 基于红黑树实现, 它是线程安全的, key 和 value 都不能是null,否则会抛出异常NullPointerException.
3. Hashtable: 和HashMap类似, 但它是线程安全的,这意味着同一时刻多个线程可以同时写入HashTable并且不会导致数据不一致。 现在可以使用 ConcurrentHashMap 来支持线程安全,并且 ConcurrentHashMap 的效率会更高,因为 ConcurrentHashMap 引入了分段锁。
4. LinkedHashMap:使用双向链表来维护元素的顺序,顺序为插入顺序或者最近最少使用(LRU)顺序。
HashMap 和 HashTable、ConcurrentHashMap的区别?
相同点: 1.HashMap 和 Hashtable都实现了Map接口。
2. 都可以存储key-value数据
不同点: 1. HashMap可以把null作为key或者value,HashTable则不可以。
2. HashMap的迭代器(Iterator)是fail-fast(最快的时间能把错误抛出而不是让程序执行)迭代器,而HashTable的enumberator迭代器不是fail-fast。
3. HashMap线程不安全、效率高; 而HashTable线程安全、效率低。