這次是第三期Java經典知識點以及面試題了,希望能給廣大面試的童鞋提供幫助哈!
1.試比較String,StringBuffer,StringBuilder三者間的區別。
①:String的值不可變,每次對String操作都會生成新的String對象,這樣會浪費內存空間,而StringBuffer和StringBuilder的值能多次修改,並且不會產生新的對象;
②:StringBuilder執行速度快,但線程不安全;StringBuffer執行速度慢,但線程安全;
可參照這篇博客查看具體的區別以及細節,這裏不作過多的解釋,鏈接爲https://blog.csdn.net/u011702479/article/details/82262823。
下面分別對StringBuffer以及StringBuilder分別寫一個Demo,代碼如下:
public class StringBufferDemo {
public static void main(String[] args) {
//內容可以改變的字符串,相比String節省開銷
StringBuffer sb = new StringBuffer();
//初始容量默認16字符
StringBuffer sb1 = new StringBuffer(20);
System.out.println(sb.capacity());//16 初始容量
System.out.println(sb1.capacity());//20
//使用StringBuffer和String顯示0-9
for(int i=0;i<10;i++) {
sb1.append(i);
}
System.out.println(sb1);
//反轉
System.out.println(sb1.reverse());
String str = new String();
for(int i=0;i<10;i++) {
str += i;
}
System.out.println(str);
String str2 = new String();
for(int i=9;i>=0;i--) {
str2+=str.charAt(i);
}
System.out.println(str2);
//希望顯示¥6,577.55(按銀行方式顯示)
double sal = 6577.55;
StringBuffer sb2 = new StringBuffer("6577.55").insert(0, '¥');
sb2.insert(2, ',');
System.out.println(sb2);
}
}
StringBuilder的Demo代碼如下:
public class StringBuilderDemo {
public static void main(String[] args) {
//如果項目與線程相關,請使用StringBuffer
StringBuilder sb = new StringBuilder();
sb.append("今天星期四");
sb.insert(4,"hello");
System.out.println(sb);
}
}
這裏String也寫一個簡單的Demo吧,代碼如下:
public class StringDemo {
public static void main(String[] args) {
String str = "haha1";
String str2 = str;
System.out.println(str == str2);//true
str2 = "haha1";
System.out.println(str == str2);//true
System.out.println(str2.charAt(3));//a(下標從0開始)
String str3 = "HaHA1";
System.out.println(str2.equalsIgnoreCase(str3));//true
System.out.println(str2.substring(0,2));//ha
String[] strs = str2.split("a");//h,h,1
for(String s :strs) {
System.out.println(s+" ");//"h","h","1"
}
System.out.println();
System.out.println("返回a字母出現的下標:"+str2.indexOf('a'));//1
String strInfo = "你好";
try {
strInfo = new String(strInfo.getBytes("ISO-8859-1"),"utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(strInfo);
}
}
2.請比較ArrayList和LinkedList的區別。
①:ArrayList實現了基於動態數組的數據結構,而LinkedList實現了基於鏈表的數據結構;
②:對於隨機的訪問或者查詢,比如get和set,ArrayList要優於LinkedList,因爲LinkedList要移動指針;
③:對於中間元素的添加或者刪除操作,比如add和remove,LinkedList要優於ArrayList,因爲ArrayList要移動數據。
詳情可參照博客鏈接https://www.cnblogs.com/Jacck/p/8034900.html。
下面寫一個小的例子來更加深刻的認識兩個不同的數據結構,代碼如下:
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
/**
* List應用
* 線性存儲
* @author autumn_leaf
*
*/
public class ListDemo {
/**
* List有序集合,按位置從0開始存儲,根據下標讀取
* @param args
*/
public static void main(String[] args) {
List list = new ArrayList<>();
list.add("one");
list.add("two");
list.add(2,"three");
System.out.println(list);//[one,two,three]
//在集合中如何獲取指定位置的內容
System.out.println(list.get(2));//three
//找到指定對象的下標
System.out.println(list.indexOf("two"));//1
List list2 = new ArrayList<>(3);//設置初始容量爲3,默認爲10
//LinkedList 操作集合中的元素 方便進行隊列處理
LinkedList llist = new LinkedList<>();
llist.add("one");
llist.add("two");
System.out.println(llist);//[one,two]
llist.addFirst("zero");
llist.removeLast();//移除最後一個
System.out.println(llist);//[zero,one]
//兩者區別:LinkedList功能更多,適用於插入和操作集合
//ArrayList 適用於查詢的場景 線性存儲
}
}
運行結果如下:
[one, two, three]
three
1
[one, two]
[zero, one]
3.請比較List,Set以及Map三者之間的區別。
①:List和Set都是實現了Collection接口,而Map不是Collection的子接口或者實現類,Map是一個接口;
②:List是一個有序容器,可以允許重複的對象,可以插入多個null元素;Set是一個無序容器,不允許重複的對象(值唯一),只允許一個null值;而Map以鍵值對形式進行存儲,可能有相同的值對象但鍵對象必須唯一,可以擁有多個null值但最多隻能有一個null鍵;
詳情可參考博客鏈接https://www.cnblogs.com/IvesHe/p/6108933.html。
下面寫幾個小的例子來更加深刻的認識各自的作用,SetDemo如下:
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashSet;
import java.util.TreeSet;
public class SetDemo {
/**
* 上層接口是Collection,set集合是一種無序集合,
* 存儲內容完全不同,對象要求地址不同
* 存儲內容完全不同,並且無序時,選用Set集合
* @param args
*/
public static void main(String[] args) {
//HashSet 散列存取
HashSet s = new HashSet<String>();
s.add("chen");
s.add("liu");
s.add(1);//這裏的1並不是int類型,而是轉換後的內容
s.add(new Integer(1));
System.out.println(s);
//其實是按照hashCode的值比較進行存儲
System.out.println("chen".hashCode()+" "+new Integer(1).hashCode()+" "+"liu".hashCode());
//TreeSet 按照排序樹存儲,要求各個元素可以直接比較
TreeSet tree = new TreeSet<>();
tree.add("one");
tree.add(new Integer(1).toString());
tree.add(new Float(4.0F).toString());
System.out.println(tree);
//使用自定義對象作爲內容放入集合中
HashSet hs = new HashSet<Student>();
Student s1 = new Student(1001,"chen","男",new Date());
hs.add(s1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
hs.add(new Student(1002,"zhou","女",sdf.parse("2000-01-01")));
} catch (ParseException e) {
e.printStackTrace();
}
hs.add(3);
hs.remove(3);
hs.remove(s1);
System.out.println(hs);
}
}
運行結果如下:
[chen, 1, liu]
3052494 1 107160
[1, 4.0, one]
[Student [id=1002, name=zhou, sex=女, birth=Sat Jan 01 00:00:00 CST 2000]]
MapDemo代碼如下:
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.TreeMap;
public class MapDemo {
public static void main(String[] args) {
//HashMap TreeMap HashTable
Map<String,String> hmap = new HashMap<String,String>();
hmap.put("a", "chen");
hmap.put("b", "liu");
hmap.put("c", "zhou");
hmap.put("d", null);
hmap.put(null, null);
hmap.put("a", "a");//鍵名相同,值進行覆蓋
System.out.println(hmap);
Map<String,String> htable = new Hashtable<String,String>();
//注意,HashMap線程不安全,Hashtable線程安全,synchronized關鍵字加在方法上
//Hashtable不允許錄入null,HashMap可以錄入null
//htable.put(null, null);
//System.out.println(htable);
// synchronized(htable) {
//
// }
//使用TreeMap有序存儲
Map<String,String> tmap = new TreeMap<String,String>();
tmap.put("a", "chen");
tmap.put("b", "liu");
tmap.put("c", "zhou");
//tmap.put(null, null);//key不能爲空
tmap.put("d", null);
System.out.println(tmap);
System.out.println(tmap.get("b"));//liu
}
}
運行結果如下:
{null=null, a=a, b=liu, c=zhou, d=null}
{a=chen, b=liu, c=zhou, d=null}
liu
4.試比較HashMap,Hashtable,TreeMap的區別。
①:這三個都對Map接口進行了實現;
②:HashMap線程不安全,但執行速度快;它允許key值出現一次null,value值出現多次null;
③:Hashtable線程安全,但執行速度慢;key和value的值均不允許爲null;
④:TreeMap可以排序,默認按照鍵的自然順序進行升序排序。
5.請比較Collection與Collections的區別。
Collection是集合類的上級接口,其中Map和Set都實現了Collection接口,提供了通用的接口方法;而Collections是一個工具類,提供了一系列的靜態方法實現對集合的搜索、排序等操作,關於具體的區別以及例子可以參照這篇博客,鏈接爲https://blog.csdn.net/ETEmonster/article/details/94735940,關於Collections的使用,一個簡易的Demo代碼如下:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CollectionsDemo {
public static void main(String[] args) {
//Collection是集合,Collections是爲操作集合提供的一個工具類
List list = new ArrayList<>();
list.add("apple");
list.add("pear");
list.add("banana");
list.add("orange");
list.add("grape");
String max = (String) Collections.max(list);
String min = (String) Collections.min(list);
System.out.println(max);
System.out.println(min);
Collections.sort(list);
System.out.println("排序後:"+list);
}
}
運行結果如下:
pear
apple
排序後:[apple, banana, grape, orange, pear]
好了,本期就講到這裏了,我們下一期再見!