Java 集合简述
1.Collection接口
2、Map接口
3-List和Set的区别:
1、List可以添加重复对象,Set不可以添加重复对象。
2、List是有序排列的,Set是无序排列的。
4-HashSet和Set用法有什么不同?
1、Set是接口,HashSet是实现类,使用Set,当别人需要调用方法的时候可以根据自己的需要来写实现类,可以改写成LinkedHashSet,但如果用的是HashSet,那么当别人调用方法的时候,就不能改写了, 只能使用HashSet。
5-Map和HashMap用法有什么不同?
1、同上
6-eclipse导入包的快捷键是什么?
Ctrl+shift+o
7-ArrayList的size和length区别是啥?
1、size()是集合的方法,用于统计集合中元素个数,length()是String的方法,统计字符串长度,length是数组的属性,统计数组长度。
8-ArrayList、LinkedList、Vector区别?从底层实现、查询遍历、修改插入删除、安全性、效率等角度.
1、底层实现:
ArrayList:
LinkedList:
Vector:
通过3者的底层代码可以看到,ArrayList 和 Vector 都是用数组的方式存储数据,而LinkedList采用的是链表。
2、查询遍历、修改插入删除
查询方面,ArrayList、Vector用的都是数组,都带有下标,所以查询数据速度较快,而LinkedList用的是链表,得依次查询,所以查询较慢。
修改插入删除方面,ArrayList、Vector在做插入、删除的时候,由于是数组,所以改动位置以后的元素都得做移动,本质就是将原数组的数据复制到新数组,再将新的元素添加到新数组中;而LinkedList只需要改动指定元素的前后节点即可;
通过底层也可以看出
ArrayList:
Vector:
LinkedList:
安全性:
ArrayList、LinkedList不是线程安全的,Vector是线程安全的。通过上面的底层代码可以看到Vector的方法都是都有synchronized关键字的,而ArrayList、LinkedList没有。
效率:
查询效率:ArrayList、Vector效率比LinkedList高,而Vector是线程安全的,所以效率不如ArrayList;
删除、插入数据效率:LinkedList比ArrayList、Vector高。
9-自己实现一个ArrayList类,并对关键代码加注释,并本地调用方法。要求实现至少ArrayList的2个方法.
// An highlighted block
public class TestArrayList<E>{
private Object[] elementData;//list中存放对象的数组
private int size;//list的元素个数
/**
* 有参构造器,构造1个长度为length的List
* @param length
*/
public TestArrayList(int length) {
this.elementData = new Object[length];
}
/**
* 无参构造器,构造1个长度为10的List
*/
public TestArrayList() {
this(10);
}
/**
* 返回list的元素个数
*/
public int size() {
return size;
}
/**
* 判断list是否为空
*/
public boolean isEmpty() {
return size == 0;
}
/**
* list添加对象
* @param E
*/
public boolean add(E e) {
elementData[size] = e;
size++;
return true;
}
/**
* 获取list中下标是index的对象
* @param index
*/
public E get(int index){
if(index>=size){
try {
throw new Exception("下标溢出");
} catch (Exception e1) {
e1.printStackTrace();
}
}
E e = (E) elementData[index];
return e;
}
public static void main(String[] args) {
//构造一个TestArrayList
TestArrayList<String> list = new TestArrayList<String>();
list.add("haha");//list中添加对象
System.out.println(list.get(0));//打印出下标0的对象
System.out.println(list.size());
System.out.println(list.isEmpty());
}
}
打印结果:
haha
1
false
10-System.arraycopy 函数的作用?请用代码说明.
// An highlighted block
public static void main(String[] args) {
String[] src = new String[] {"java","C++","php"};
String[] dest = new String[src.length];
//数组src复制给数组dest
System.arraycopy(src,0,dest,0,dest.length);
for(String s : dest) {
System.out.print(s+"-->");
}
}
结果如下:
java-->C++-->php-->
11-自己实现一个LinkedList类,并对关键代码加注释,并本地调用方法。要求实现至少LinkedList的2个方法.
// An highlighted block
public class TestLinkedList<E> {
private Node first;//第一个节点
private Node last;//最后一个节点
private int size=0;//元素个数
/**
* 无参构造器
*/
public TestLinkedList() {
}
/**
* 返回TestLinkedlist的元素个数
* @return size
*/
public int size() {
return size;
}
/**
* TestLinkedList添加元素
* @param e
*/
public void add(E e){
//list为空:该元素做为唯一的元素,首尾节点都是该元素,同时首尾相连
if(first ==null){
Node<E> node = new Node(null,e,null);
first = last = node;
first.pre = last;
last.next = first;
}else{
//list不为空:最后一个位置添加该元素,原最后一个节点变成该元素的前一个节点
Node<E> node = new Node(last,e,first);
node.pre.next = node;
last = node;
}
//list的元素个数+1
size++;
}
/**
* 获取TestLinkedList,index位置节点的元素值
* @param index
* @return E
*/
public E get(int index){
Node node = getNode(index);
return (E) node.item;
}
/**
* 获取index位置的节点
* @param index
* @return node
*/
public Node getNode(int index){
checkIndex(index);
Node node = null;
//list不为空:从第一个节点开始,依次往下直到index位置,返回该位置节点
if(first!=null){
node = first;
for(int i=0;i<index;i++){
node = node.next;
}
}
return node;
}
/**
* 删除index位置节点
* @param index
*/
public void remove(int index){
checkIndex(index);
//获取到index位置节点,将该节点的前后2个节点连接,移除当前节点
Node node = getNode(index);
node.next.pre = node.pre;
node.pre.next = node.next;
size--;
}
public void checkIndex(int index){
if(index>=size || index<0){
try {
throw new Exception("下标不合法");
} catch (Exception e) {
e.printStackTrace();
}
}
}
private static class Node<E> {
//当前节点元素值
E item;
//下一个节点
Node<E> next;
//上一个节点
Node<E> pre;
Node(Node<E> pre, E element, Node<E> next) {
this.item = element;
this.next = next;
this.pre = pre;
}
}
public static void main(String[] args) {
TestLinkedList<String> list = new TestLinkedList<String>();
list.add("haha");
list.add("zzz");
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
list.remove(1);
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
}
}
运行main方法结果:haha
zzz
haha
12-eclipse上下移动整行代码快捷键是什么?
ALT+上下方向键
13-Map接口类的方法,put,get,remove,containsKey,containsValue,size,isEmpty,putAll,clear 分别用代码加注释说明其用法.
代码如下:
public static void main(String[] args) {
Map<String,String> map = new HashMap<String, String>();
//put(K,V)向map添加数据
map.put("key01", "value01");
map.put("key02", "value02");
map.put("key03", "value03");
map.put("key04", "value04");
for(Entry<String, String> entry:map.entrySet()) {
System.out.println(entry.getKey()+"--->"+entry.getValue());
}
System.out.println("---------------------------------------------------");
String value = map.get("key01");//get()根据key值获取对应的value值
System.out.println(value);
System.out.println("---------------------------------------------------");
System.out.println(map.containsKey("key01"));//map是否包含该键值
System.out.println("---------------------------------------------------");
System.out.println(map.containsValue("value01"));//map是否包含该value值
System.out.println("---------------------------------------------------");
System.out.println(map.size());//map中键值对个数
System.out.println("---------------------------------------------------");
map.remove("key01");//删除key值对应的键值对
for(Entry<String, String> entry:map.entrySet()) {
System.out.println(entry.getKey()+"--->"+entry.getValue());
}
System.out.println("---------------------------------------------------");
System.out.println(map.containsKey("key01"));
System.out.println("---------------------------------------------------");
System.out.println(map.containsValue("value01"));
System.out.println("---------------------------------------------------");
System.out.println(map.isEmpty());//map是否为空
System.out.println("---------------------------------------------------");
Map<String,String> map02 = new HashMap<String, String>();
map02.putAll(map);//复制map所有键值对
for(Entry<String, String> entry:map02.entrySet()) {
System.out.println(entry.getKey()+"--->"+entry.getValue());
}
System.out.println("---------------------------------------------------");
map02.clear();//清空map
System.out.println(map02.size());
}
```输出结果:
key04--->value04
key03--->value03
key02--->value02
key01--->value01
---------------------------------------------------
value01
---------------------------------------------------
true
---------------------------------------------------
true
---------------------------------------------------
4
---------------------------------------------------
key04--->value04
key03--->value03
key02--->value02
---------------------------------------------------
false
---------------------------------------------------
false
---------------------------------------------------
false
---------------------------------------------------
key04--->value04
key03--->value03
key02--->value02
---------------------------------------------------
0
14-HashMap,HashTable区别?
1、HashMap允许将null作为key或者value,而Hashtable不允许。
2、HashMap 线程不安全,Hashtable线程安全
代码如下:
// An highlighted block
package test01;
import java.util.HashMap;
import java.util.Hashtable;
public class Main01 {
public static HashMap<String,String> map = new HashMap<String, String>();
public static Hashtable<String,String> table = new Hashtable<String, String>();
public static void main(String[] args) {
mapput();
tableput();
try {
//主线程休眠2s
Thread.currentThread().sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
for(int i=0;i<200;i++) {
//有打印结果的说明,在多线程在执行put方法的时候发生错误
if(!String.valueOf(i).equals(table.get(String.valueOf(i)))) {
System.out.println(i+"table:false");
} if(!String.valueOf(i).equals(map.get(String.valueOf(i)))) {
System.out.println(i+"map:false");
}
}
}
public static void tableput() {
Thread t3 = new Thread(new Runnable() {
public void run() {
int i=0;
while(i<100) {
table.put(String.valueOf(i),String.valueOf(i));
i++;
}
}
});
Thread t4 = new Thread(new Runnable() {
public void run() {
int i=100;
while(i<200) {
table.put(String.valueOf(i),String.valueOf(i));
i++;
}
}
});
t3.start();
t4.start();
}
public static void mapput() {
Thread t1 = new Thread(new Runnable() {
public void run() {
int i=0;
while(i<100) {
map.put(String.valueOf(i),String.valueOf(i));
i++;
}
}
});
Thread t2 = new Thread(new Runnable() {
public void run() {
int i=100;
while(i<200) {
map.put(String.valueOf(i),String.valueOf(i));
i++;
}
}
});
t1.start();
t2.start();
}
}
多次运行后会出现以下结果:
0map:false
2map:false
4map:false
6map:false
8map:false
11map:false
12map:false
14map:false
16map:false
18map:false
23map:false
28map:false
29map:false
32map:false
33map:false
34map:false
35map:false
36map:false
37map:false
38map:false
40map:false
41map:false
42map:false
43map:false
45map:false
46map:false
51map:false
52map:false
53map:false
55map:false
56map:false
58map:false
60map:false
61map:false
63map:false
100map:false
103map:false
104map:false
105map:false
106map:false
108map:false
110map:false
111map:false
112map:false
113map:false
114map:false
120map:false
121map:false
123map:false
125map:false
127map:false
129map:false
说明map 在put的时候发生了错误,而Hashtable不会出错。