JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成。JDBC提供了一种基准,据此可以构建更高级的工具和接口,使数据库开发人员能够编写数据库应用程序。
连接数据库
使用的连接类是java.sql包中的DriverManager类,方法名:getConnection(),会返回一个数据库Connection对象,它的代码如下:
JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成。JDBC提供了一种基准,据此可以构建更高级的工具和接口,使数据库开发人员能够编写数据库应用程序。
连接数据库
使用的连接类是java.sql包中的DriverManager类,方法名:getConnection(),会返回一个数据库Connection对象,它的代码如下:
一直都不适用vsftp,但是最近因为公司需要一个官网,所以才启动起来,安装vsftp很简单,可以搜索本站的资源“vsftpd”进行查阅,这个帖子只要是记录使用过程中的问题。
以前服务器的ContOS是6.5版本的,现在的版本是7.5,问题就随之而来了,以前出现过一次无法上传文件的问题,该问题是防火墙,帖子:“http://www.lanxinbase.com/?p=607”。
问题1:500 OOPS: vsftpd: refusing to run with writable root inside chroot ()。
解答:这个问题是因为你在vsftpd目录下的chroot_list文件中加入了FTP的用户名,只要把它删除就可以了;也可以把写入的权限去掉:chmod a-w /www。或者你可以在vsftpd的配置文件中增加下列两项中的一项:allow_writeable_chroot=YES
造成这个原因是因为,如:我们新建了一个www的用户,目录指向/www,并给/www赋予了写入的权限,所有就出现了500的错误。而chroot_list文件列表是制定需要锁定用户在它当前根目录的一个权限文件。简单点,如果你给了写入的权限,而又限制用户只能在它当前的根目录下,这样子会造成冲突,比如我上传一个文件夹。
*注:以前也遇到过这个问题是selinux的问题,所以要排除一下。
问题2:修改vsftp用户目录的问题。
解答:修改用户信息,使用usermod命令,其参数:
[root@iZ2zed1931stl3pem7ew6vZ vsftpd]# usermod –help
Usage: usermod [options] LOGINOptions:
-c, –comment COMMENT new value of the GECOS field
-d, –home HOME_DIR new home directory for the user account
-e, –expiredate EXPIRE_DATE set account expiration date to EXPIRE_DATE
-f, –inactive INACTIVE set password inactive after expiration
to INACTIVE
-g, –gid GROUP force use GROUP as new primary group
-G, –groups GROUPS new list of supplementary GROUPS
-a, –append append the user to the supplemental GROUPS
mentioned by the -G option without removing
him/her from other groups
-h, –help display this help message and exit
-l, –login NEW_LOGIN new value of the login name
-L, –lock lock the user account
-m, –move-home move contents of the home directory to the
new location (use only with -d)
-o, –non-unique allow using duplicate (non-unique) UID
-p, –password PASSWORD use encrypted password for the new password
-R, –root CHROOT_DIR directory to chroot into
-s, –shell SHELL new login shell for the user account
-u, –uid UID new UID for the user account
-U, –unlock unlock the user account
-Z, –selinux-user SEUSER new SELinux user mapping for the user account
我就不翻译出来了,具体的自己拷贝到翻译软件里看一下就清楚了,但是这里主要介绍一下参数:-d、-u。
-d参数后面跟的目录很好解决,但是如果是-u的UID,你不知道是多少,那么可以使用命令: cat /etc/passwd |grep www
[root@iZ2zed1931stl3pem7ew6vZ vsftpd]# cat /etc/passwd |grep www
www:x:1000:1000::/www:/sbin/nologin
其中第一个1000就是UID,那么修改目录的命令,就应该如下:
usermod -m -d /www/home -u 1000 www;
或
usermod -d /www/home -m -u 1000 www;
*注:有些系统可能不需要-m的参数。
问题3:修改vsftp用户登陆密码。
解答:这个应该不是问题,就是:passwd www。
问题4:访问vsftp服务器出现200、227错误。
解答:ftp服务器有两种工作模式,port和pasv(主动和被动),这两种模式都是客户端先向服务器端发出请求,服务器建立链接,以服务器为对象,当传输数据时,如果是服务器从20端口向客户端空闲端口发送请求建立链接,就是port(主动),反之,如果是客户端向服务器空闲端口请求建立链接,就是pasv(被动)。
打开IE浏览器:选择设置—-internet—–高级—-使用被动FTP(为防火墙和DSL调制解调器兼容性)”前面的勾去掉。
问题5:450:读取目录列表失败。
解答:这个挺困扰的,我找了很久才发现,只需要在vsftp的配置文件vsftpd.conf加入pasv_enable=NO就可以了,接着重启一下服务器,引起整个原因是pasv的问题。
问题6:中文乱码的问题。
解答:中文乱码的问题其实我没有遇到过,网上的解决方案是改i18n,不过我们服务器的名称一般都是英文的,所以就不需要搭理它了。
问题7:530 Login incorrect.
解答:请检查一下密码是否输入错误。
问题8:550 外网无法登录。
解答:这个可能是没有开放tcp 20的端口,请检查一下防火墙规则配置文件。
问题9:其他问题。
这次出现过一次很奇葩的问题,但其实是FTP客户端的问题,我在登陆www账号的时候,FTP客户端居然把服务器“/”目录当成了用户的主目录,0.0吓死我了,搞了我一下午都没有找出问题来,后来第二天再登陆进去,发现正常了,根目录是/www,这就奇怪了,后来我再次改一下配置文件,然后换了不同的FTP客户端登陆,确定这个是客户端的问题。
所以遇到问题不要怕麻烦,一个个去排查就可以了。
fdisk [选项] <disk> 改变分区表
fdisk [选项] –l <disk> 列出所有分区表
fdisk –s <partition(分区编号)> 以分区块为单位,给出指定分区的大小
这是一个创建和维护分区的命令,兼容DOS类型的分区表、BSD或SUN类型的磁盘列表。注意fdisk不支持2T以上的硬盘分区,此时需要使用gdisk。
相关了解:
磁头数(Heads)表示硬盘有几个磁头,也就是有几面盘片,一个硬盘最多有255个磁头
柱面数(Cylinders)表示硬盘每面盘片上有几条磁道
扇区数(Sectors)表示每条磁道上有几个扇区,每条磁道最多有63个扇区
-b <
size
> 指定扇区大小(512,1024,2048或4096 B)
-c 关闭DOS兼容模式
-u <
size
> 以扇区编号取代柱面编号来表示每个分区的起始地址,一般与-l选项联合使用
-C <
number
> 指定柱面编号
-H <
number
> 指定磁头编号
-S <
number
> 指定磁道扇区编号
a 设置可引导标记(活动分区/引导分区之间切换)
b 编辑BSD磁盘标签
c 设置DOS操作系统兼容标记(兼容/不兼容之间切换)
d 删除一个分区
l 显示已知的分区类型,其中82为Linux swap分区,83为Linux分区
m 显示帮助信息
n 增加一个新的分区
o 创建一个新的空白的DOS分区表
p 显示磁盘当前的分区表
q 退出fdisk程序,不保存任何修改
s 创建一个新的空白的Sun磁盘标签
t 改变一个分区的系统ID,就是改变分区类型(比如把Linux Swap分区改为Linux分区)
u 改变显示或输入单位
v 验证磁盘分区表
w 将分区表写入磁盘并退出(保存并退出)
x 额外功能(专家级)
注:使用fdisk <磁盘>可以进入到菜单操作,如:fdisk /dev/vdb
[root@iZ2zed1931stl3pem7ew6vZ /]# fdisk -l
Disk /dev/vda: 42.9 GB, 42949672960 bytes, 83886080 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000a57dfDevice Boot Start End Blocks Id System
/dev/vda1 * 2048 83884031 41940992 83 LinuxDisk /dev/vdb: 64.4 GB, 64424509440 bytes, 125829120 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
我这台linux有两个磁盘,一个是/dev/vda共42.9GB,另一个是/dev/vdb共64.4GB。
[root@iZ2zed1931stl3pem7ew6vZ /]# fdisk -lu
Disk /dev/vda: 42.9 GB, 42949672960 bytes, 83886080 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000a57dfDevice Boot Start End Blocks Id System
/dev/vda1 * 2048 83884031 41940992 83 LinuxDisk /dev/vdb: 64.4 GB, 64424509440 bytes, 125829120 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
[root@iZ2zed1931stl3pem7ew6vZ /]# fdisk /dev/vdb
Welcome to fdisk (util-linux 2.23.2).Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x90e7133d.Command (m for help):
分区操作(扩展分区):
[root@iZ2zed1931stl3pem7ew6vZ /]# fdisk /dev/vdb
Welcome to fdisk (util-linux 2.23.2).Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x90e7133d.Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): e
Partition number (1-4, default 1): 1
First sector (2048-125829119, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-125829119, default 125829119):
Using default value 125829119
Partition 1 of type Extended and of size 60 GiB is setCommand (m for help): p
Disk /dev/vdb: 64.4 GB, 64424509440 bytes, 125829120 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x90e7133dDevice Boot Start End Blocks Id System
/dev/vdb1 2048 125829119 62913536 5 Extended
写入磁盘(保存):
Command (m for help): w
The partition table has been altered!Calling ioctl() to re-read partition table.
Syncing disks.
此时在分一个逻辑分区:
[root@iZ2zed1931stl3pem7ew6vZ /]# fdisk /dev/vdb
Welcome to fdisk (util-linux 2.23.2).Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.Command (m for help): n
Partition type:
p primary (0 primary, 1 extended, 3 free)
l logical (numbered from 5)
Select (default p): 1
Invalid partition type `1′Command (m for help): n
Partition type:
p primary (0 primary, 1 extended, 3 free)
l logical (numbered from 5)
Select (default p): l
Adding logical partition 5
First sector (4096-125829119, default 4096):
Using default value 4096
Last sector, +sectors or +size{K,M,G} (4096-125829119, default 125829119):
Using default value 125829119
Partition 5 of type Linux and of size 60 GiB is setCommand (m for help): w
The partition table has been altered!Calling ioctl() to re-read partition table.
Syncing disks.
[root@iZ2zed1931stl3pem7ew6vZ /]#
至此,分区完毕,使用fdisk -l查看磁盘分区:
[root@iZ2zed1931stl3pem7ew6vZ /]# fdisk -l
Disk /dev/vda: 42.9 GB, 42949672960 bytes, 83886080 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000a57dfDevice Boot Start End Blocks Id System
/dev/vda1 * 2048 83884031 41940992 83 LinuxDisk /dev/vdb: 64.4 GB, 64424509440 bytes, 125829120 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x90e7133dDevice Boot Start End Blocks Id System
/dev/vdb1 2048 125829119 62913536 5 Extended
/dev/vdb5 4096 125829119 62912512 83 Linux
最后的/dev/vdb1就是刚刚分区的扩展分区,/dev/vdb5 是逻辑分区,但是还没有挂载到系统中。
Linux mount命令是经常会使用到的命令,它用于挂载Linux系统外的文件。
mount [-hV] mount -a [-fFnrsvw] [-t vfstype] mount [-fnrsvw] [-o options [,...]] device | dir mount [-fnrsvw] [-t vfstype] [-o options] device dir
参数说明:
将/dev/vdb1挂在 /ww2 之下
[root@iZ2zed1931stl3pem7ew6vZ /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 40G 4.9G 33G 14% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 600K 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
tmpfs 379M 0 379M 0% /run/user/0
首先使用df -h查看一下,当前的分区情况,然后再使用mount挂载分区:
[root@iZ2zed1931stl3pem7ew6vZ /]# mount /dev/vdb1 /ww2
mount: /dev/vdb1 is write-protected, mounting read-only
mount: unknown filesystem type ‘(null)’//这里出错了,因为没有格式化,下面在试一次:
[root@iZ2zed1931stl3pem7ew6vZ /]# mkfs.ext4 /dev/vdb
vdb vdb1 vdb5
[root@iZ2zed1931stl3pem7ew6vZ /]# mkfs.ext4 /dev/vdb5
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
3932160 inodes, 15728128 blocks
786406 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2164260864
480 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done[root@iZ2zed1931stl3pem7ew6vZ /]#
挂载分区:
[root@iZ2zed1931stl3pem7ew6vZ /]# mount /dev/vdb5 /ww2
[root@iZ2zed1931stl3pem7ew6vZ /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 40G 4.9G 33G 14% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 604K 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
tmpfs 379M 0 379M 0% /run/user/0
/dev/vdb5 59G 53M 56G 1% /ww2
/dev/vdb5 59G 53M 56G 1% /ww2指向了ww2的目录,这样子挂载是临时的,如果需要下次开机自动挂载磁盘,就需要修改/etc/fstab文件。
网上找了个图,方便理解。
Set是最简单的一种集合。集合中的对象不按特定的方式排序,并且没有重复对象。
Modifier and Type | Method and Description |
---|---|
boolean |
add(E e)
Adds the specified element to this set if it is not already present (optional operation).
|
boolean |
addAll(Collection<? extends E> c)
Adds all of the elements in the specified collection to this set if they’re not already present (optional operation).
|
void |
clear()
Removes all of the elements from this set (optional operation).
|
boolean |
contains(Object o)
Returns true if this set contains the specified element.
|
boolean |
containsAll(Collection<?> c)
Returns true if this set contains all of the elements of the specified collection.
|
boolean |
equals(Object o)
Compares the specified object with this set for equality.
|
int |
hashCode()
Returns the hash code value for this set.
|
boolean |
isEmpty()
Returns true if this set contains no elements.
|
Iterator<E> |
iterator()
Returns an iterator over the elements in this set.
|
boolean |
remove(Object o)
Removes the specified element from this set if it is present (optional operation).
|
boolean |
removeAll(Collection<?> c)
Removes from this set all of its elements that are contained in the specified collection (optional operation).
|
boolean |
retainAll(Collection<?> c)
Retains only the elements in this set that are contained in the specified collection (optional operation).
|
int |
size()
Returns the number of elements in this set (its cardinality).
|
default Spliterator<E> |
spliterator()
Creates a
Spliterator over the elements in this set. |
Object[] |
toArray()
Returns an array containing all of the elements in this set.
|
<T> T[] |
toArray(T[] a)
Returns an array containing all of the elements in this set; the runtime type of the returned array is that of the specified array.
|
HashSet底层用的是哈希表,它把对象根据其哈希值存放到对应的区域里。由于这种特性,两个在不同区域的对象会被认为不相同的。
所以如果对象要存放到Hash集合里面,则需要重写对象的hashCode方法,让相等的对象的hashCode的值也相等。
TreeSet采用的数据结构是红黑树,我们可以让它按指定规则对其中的元素进行排序。它又是如何判断两个元素是否相同呢?除了用equals方法检查两个元素是否相同外,还要检查compareTo方法是否返回为0。
所以如果对象要存放到Tree集合里,需要在重写compareTo方法,把相同的对象的比较值定为0,防止相同的元素被重复添加进集合中。
底层数据结构是链表和哈希表。(FIFO插入有序,唯一)
set的测试:
Set<String> data = new TreeSet<>(); data.add("re"); data.add("idn"); data.add("mob"); data.add("ref"); data.add("01"); out(data); Set<Integer> integers = new TreeSet<>(); integers.add(12); integers.add(1); integers.add(15); integers.add(5); integers.add(65); out(integers);
打印结果如下:
[01, idn, mob, re, ref]
[1, 5, 12, 15, 65]
List 接口继承了 Collection 接口以定义一个允许重复项的有序集合。该接口不但能够对列表的一部分进行处理,还添加了面向位置的操作。
有序的 collection(也称为序列)。此接口的用户可以对列表中每个元素的插入位置进行精确地控制。用户可以根据元素的整数索引(在列表中的位置)访问元素,并搜索列表中的元素。
Modifier and Type | Method and Description |
---|---|
boolean |
add(E e)
Appends the specified element to the end of this list (optional operation).
|
void |
add(int index, E element)
Inserts the specified element at the specified position in this list (optional operation).
|
boolean |
addAll(Collection<? extends E> c)
Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection’s iterator (optional operation).
|
boolean |
addAll(int index, Collection<? extends E> c)
Inserts all of the elements in the specified collection into this list at the specified position (optional operation).
|
void |
clear()
Removes all of the elements from this list (optional operation).
|
boolean |
contains(Object o)
Returns true if this list contains the specified element.
|
boolean |
containsAll(Collection<?> c)
Returns true if this list contains all of the elements of the specified collection.
|
boolean |
equals(Object o)
Compares the specified object with this list for equality.
|
E |
get(int index)
Returns the element at the specified position in this list.
|
int |
hashCode()
Returns the hash code value for this list.
|
int |
indexOf(Object o)
Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
|
boolean |
isEmpty()
Returns true if this list contains no elements.
|
Iterator<E> |
iterator()
Returns an iterator over the elements in this list in proper sequence.
|
int |
lastIndexOf(Object o)
Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
|
ListIterator<E> |
listIterator()
Returns a list iterator over the elements in this list (in proper sequence).
|
ListIterator<E> |
listIterator(int index)
Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.
|
E |
remove(int index)
Removes the element at the specified position in this list (optional operation).
|
boolean |
remove(Object o)
Removes the first occurrence of the specified element from this list, if it is present (optional operation).
|
boolean |
removeAll(Collection<?> c)
Removes from this list all of its elements that are contained in the specified collection (optional operation).
|
default void |
replaceAll(UnaryOperator<E> operator)
Replaces each element of this list with the result of applying the operator to that element.
|
boolean |
retainAll(Collection<?> c)
Retains only the elements in this list that are contained in the specified collection (optional operation).
|
E |
set(int index, E element)
Replaces the element at the specified position in this list with the specified element (optional operation).
|
int |
size()
Returns the number of elements in this list.
|
default void |
sort(Comparator<? super E> c)
Sorts this list according to the order induced by the specified
Comparator . |
default Spliterator<E> |
spliterator()
Creates a
Spliterator over the elements in this list. |
List<E> |
subList(int fromIndex, int toIndex)
Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.
|
Object[] |
toArray()
Returns an array containing all of the elements in this list in proper sequence (from first to last element).
|
<T> T[] |
toArray(T[] a)
Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array.
|
PS:ArrayList是实现了基于底层的数据结构,LinkedList基于底层数据结构是链表;对于随机访问get和set,ArrayList觉得优于LinkedList,因为LinkedList遍历全部再确定;对于新增和删除操作add和remove,LinedList比较占优势,因为ArrayList要移动数据。
|————Set子接口:无序,不允许重复。
|————List子接口:有序,可以有重复元素。
区别:Collections是集合类
List<Integer> list = new ArrayList<>(); list.add(12); list.add(1); list.add(15); list.add(5); list.add(65); list.sort((a,b)->{ return a-b; }); out(list); 打印结果: [1, 5, 12, 15, 65] 如果降序,只需要把a-b改成b-a即可。
Map 是一种把键对象和值对象映射的集合,它的每一个元素都包含一对键对象和值对象。 Map没有继承于Collection接口 从Map集合中检索元素时,只要给出键对象,就会返回对应的值对象。
map主要的特性:
因此,每个 key 只能对应一个 value, 多个 key 可以对应一个 value。Map集合维护“键、值对”的关联性,使你可以通过“键”查找“值”。
Modifier and Type | Method and Description |
---|---|
void |
clear()
Removes all of the mappings from this map (optional operation).
|
default V |
compute(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)
Attempts to compute a mapping for the specified key and its current mapped value (or
null if there is no current mapping). |
default V |
computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)
If the specified key is not already associated with a value (or is mapped to
null ), attempts to compute its value using the given mapping function and enters it into this map unless null . |
default V |
computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)
If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value.
|
boolean |
containsKey(Object key)
Returns true if this map contains a mapping for the specified key.
|
boolean |
containsValue(Object value)
Returns true if this map maps one or more keys to the specified value.
|
Set<Map.Entry<K,V>> |
entrySet()
Returns a
Set view of the mappings contained in this map. |
boolean |
equals(Object o)
Compares the specified object with this map for equality.
|
default void |
forEach(BiConsumer<? super K,? super V> action)
Performs the given action for each entry in this map until all entries have been processed or the action throws an exception.
|
V |
get(Object key)
Returns the value to which the specified key is mapped, or
null if this map contains no mapping for the key. |
default V |
getOrDefault(Object key, V defaultValue)
Returns the value to which the specified key is mapped, or
defaultValue if this map contains no mapping for the key. |
int |
hashCode()
Returns the hash code value for this map.
|
boolean |
isEmpty()
Returns true if this map contains no key-value mappings.
|
Set<K> |
keySet()
Returns a
Set view of the keys contained in this map. |
default V |
merge(K key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction)
If the specified key is not already associated with a value or is associated with null, associates it with the given non-null value.
|
V |
put(K key, V value)
Associates the specified value with the specified key in this map (optional operation).
|
void |
putAll(Map<? extends K,? extends V> m)
Copies all of the mappings from the specified map to this map (optional operation).
|
default V |
putIfAbsent(K key, V value)
If the specified key is not already associated with a value (or is mapped to
null ) associates it with the given value and returns null , else returns the current value. |
V |
remove(Object key)
Removes the mapping for a key from this map if it is present (optional operation).
|
default boolean |
remove(Object key, Object value)
Removes the entry for the specified key only if it is currently mapped to the specified value.
|
default V |
replace(K key, V value)
Replaces the entry for the specified key only if it is currently mapped to some value.
|
default boolean |
replace(K key, V oldValue, V newValue)
Replaces the entry for the specified key only if currently mapped to the specified value.
|
default void |
replaceAll(BiFunction<? super K,? super V,? extends V> function)
Replaces each entry’s value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception.
|
int |
size()
Returns the number of key-value mappings in this map.
|
Collection<V> |
values()
Returns a
Collection view of the values contained in this map. |
标准的Java类库中包含了几种不同的Map:
HashMap, TreeMap, LinkedHashMap, WeakHashMap, IdentityHashMap。
它们都有同样的基本接口Map,但是行为、效率、排序策略、保存对象的生命周期和判定“键”等价的策略等各不相同。
执行效率是Map的一个大问题,看看get方法的代码,就会明白为什么在ArrayList中搜索“键”是相当慢的。而这正是HashMap提高速度的地方。HashMap使用了特殊的值,称为“散列码”(hash code),来取代对键的缓慢搜索。“散列码”是“相对唯一”用以代表对象的int值,它是通过将该对象的某些信息进行转换而生成的。所有Java对象都能产生散列码,因为hashCode()是定义在基类Object中的方法。
Map基于散列表的实现,就是使用对象的hashCode()进行快速查询的,插入和查询“键值对”的开销是固定的,此方法能够显着提高性能。
类似于HashMap,但是迭代遍历它时,取得“键值对”的顺序是其插入次序,或者是最近最少使用(LRU算法)的次序;只比HashMap慢一点,而在迭代访问时发而更快,因为它使用链表维护内部次序。
基于红黑树数据结构的实现,查看“键”或“键值对”时,它们会被排序(次序由Comparabel或Comparator决定)。TreeMap的特点在于,你得到的结果是经过排序的。TreeMap是唯一的带有subMap()方法的Map,它可以返回一个子树。
弱键(weak key)Map,Map中使用的对象也被允许释放: 这是为解决特殊问题设计的。如果没有map之外的引用指向某个“键”,则此“键”可以被垃圾收集器回收。
使用==代替equals()对“键”作比较的hash map,专为解决特殊问题而设计。
List,Set,Map将持有对象一律视为Object型别;Collection、List、Set、Map都是接口,不能实例化。继承自它们的 ArrayList, Vector, HashTable, HashMap是具体的对象,可被实例化。vector容器确切知道它所持有的对象隶属什么型别,vector不进行边界检查。
Java语言提供了一种稍弱的同步机制,即volatile变量,用来确保将变量的更新操作通知到其他线程。当把变量声明为volatile类型后,编译器与运行时都会注意到这个变量是共享的,因此不会将该变量上的操作与其他内存操作一起重排序。volatile变量不会被缓存在寄存器或者对其他处理器不可见的地方,因此在读取volatile类型的变量时总会返回最新写入的值。
在访问volatile变量时不会执行加锁操作,因此也就不会使执行线程阻塞,因此volatile变量是一种比sychronized关键字更轻量级的同步机制。
当对非 volatile 变量进行读写的时候,每个线程先从内存拷贝变量到CPU缓存中。如果计算机有多个CPU,每个线程可能在不同的CPU上被处理,这意味着每个线程可以拷贝到不同的 CPU cache 中。
而声明变量是 volatile 的,JVM 保证了每次读变量都从内存中读,跳过 CPU cache 这一步。
1.保证此变量对所有的线程的可见性,这里的“可见性”,当一个线程修改了这个变量的值,volatile 保证了新值能立即同步到主内存,以及每次使用前立即从主内存刷新。但普通变量做不到这点,普通变量的值在线程间传递均需要通过主内存来完成。
2.禁止指令重排序优化。有volatile修饰的变量,赋值后多执行了一个“load addl $0x0, (%esp)”操作,这个操作相当于一个内存屏障(指令重排序时不能把后面的指令重排序到内存屏障之前的位置),只有一个CPU访问内存时,并不需要内存屏障;
什么是指令重排序:是指CPU采用了允许将多条指令不按程序规定的顺序分开发送给各相应电路单元处理。
volatile 的读性能消耗与普通变量几乎相同,但是写操作稍慢,因为它需要在本地代码中插入许多内存屏障指令来保证处理器不发生乱序执行。
近期评论