文章目录
双例模式 一 Map接口简介
二 HashMap的存储结构简介 三 TreeMap容器类 3.1 TreeMap的比较规则 3.2 元素自身实现比较规则 3.3 通过比较器实现比较规则
四 Iterator迭代器 4.1 Iterator送代器接口介绍 4.2 栗子
五 Collections工具类 5.1 Collections工具类简介 5.2 栗子
双例模式
一 Map接口简介
Map接口定义了双例集合的存储特征。Map中的容器称为双列集合。 双例集合的存储特征是以key与value结构为单位进行存储。 Map中常用的容器为HashMap,TreeMap等。
1.1 常用方法
方法 说明 V put(K key,Vvalue) 把key与value添加到Map集合中 void putAll(Map m) 从指定Map中将所有映射关系复制到此Map中 V remove(Object key) 删除key对应的value V get(Object key) 根据指定的key,获取对应的value boolean containsKey(Object key) 判断容器中是否包含指定的key boolean containsValue(Object value) 判断容器中是否包含指定的value Set keySet() 获取Map集合中所有的key,存储到Set集合中 Set<Map.entry<K,V>> entrySet() 返回一个Set基于Map.Entry类型包含Map中所有映射。 void clear() 删除Map中所有的映射
1.2 演示
import java. util. HashMap ;
import java. util. Map ;
import java. util. Set ;
public class HashMapTest {
public static void main ( String [ ] args) {
HashMap < String , String > map = new HashMap < > ( ) ;
System . out. println ( map. put ( "a" , "A" ) ) ;
System . out. println ( map. put ( "b" , "B" ) ) ;
System . out. println ( map. put ( "a" , "B" ) ) ;
System . out. println ( "key: a value:" + map. get ( "a" ) ) ;
System . out. println ( "key: a value:" + map. get ( "A" ) ) ;
Set < String > keys = map. keySet ( ) ;
for ( String key: keys) {
System . out. println ( key+ ":" + map. get ( key) ) ;
}
Set < Map. Entry < String , String > > entrySet = map. entrySet ( ) ;
for ( Map. Entry < String , String > entry: entrySet) {
String key = entry. getKey ( ) ;
String value = entry. getValue ( ) ;
System . out. println ( key+ ":" + value) ;
}
System . out. println ( "=======" ) ;
HashMap < String , String > map2 = new HashMap < > ( ) ;
map2. put ( "f" , "F" ) ;
map2. put ( "a" , "aa" ) ;
map2. putAll ( map) ;
Set < String > keys2 = map2. keySet ( ) ;
for ( String key: keys2) {
System . out. println ( key+ ":" + map2. get ( key) ) ;
}
String a = map. remove ( "a" ) ;
Set < String > keys3 = map. keySet ( ) ;
for ( String key: keys3) {
System . out. println ( key+ map. get ( key) ) ;
}
System . out. println ( map. containsKey ( "b" ) ) ;
System . out. println ( map. containsValue ( "B" ) ) ;
}
}
二 HashMap的存储结构简介
HashMap底层实现采用哈希表。 哈希表的本质是"数组"+“链表”
数组特点:占用空间连续。寻址容易,查询速度快。
链表:占用空间不连续。增加和删除效率非常高。
缺点: 寻址困难,查询速度慢。
三 TreeMap容器类
TreeMap是可以对键进行排序的一种容器类。 TreeMap的底层是基于红黑树实现的。
3.1 TreeMap的比较规则
3.2 元素自身实现比较规则
class Users3 implements Comparable < Users3 > {
private String userName;
private int userAge;
public Users3 ( ) {
}
public Users3 ( String userName, int userAge) {
this . userName = userName;
this . userAge = userAge;
}
public String getUserName ( ) {
return userName;
}
public void setUserName ( String userName) {
this . userName = userName;
}
public int getUserAge ( ) {
return userAge;
}
public void setUserAge ( int userAge) {
this . userAge = userAge;
}
@Override
public int hashCode ( ) {
int result= userName != null ? userName. hashCode ( ) : 0 ;
result= 31 * result+ userAge;
return result;
}
@Override
public boolean equals ( Object o) {
if ( this == o) return true ;
if ( o== null || getClass ( ) != o. getClass ( ) ) return false ;
Users3 users = ( Users3 ) o;
if ( userAge!= users. userAge) return false ;
return userName!= null ? userName. equals ( users. userName) : false ;
}
@Override
public String toString ( ) {
return "Users{" +
"userName='" + userName + '\'' +
", userAge=" + userAge +
'}' ;
}
@Override
public int compareTo ( Users3 o) {
if ( this . userAge> o. getUserAge ( ) ) {
return 1 ;
}
if ( this . userAge== o. getUserAge ( ) ) {
return this . userName. compareTo ( o. getUserName ( ) ) ;
}
return - 1 ;
}
}
public class TreeMapTest {
public static void main ( String [ ] args) {
TreeMap < Users3 , String > map = new TreeMap < > ( ) ;
Users3 u1 = new Users3 ( "o" , 18 ) ;
Users3 u2 = new Users3 ( "a" , 22 ) ;
map. put ( u1, "o" ) ;
map. put ( u2, "a" ) ;
Set < Users3 > keys = map. keySet ( ) ;
for ( Users3 key: keys) {
System . out. println ( key+ ":" + map. get ( key) ) ;
}
}
}
3.3 通过比较器实现比较规则
class student2 {
private String name;
private int age;
public student2 ( ) {
}
public student2 ( String name, int age) {
this . name = name;
this . age = age;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public int getAge ( ) {
return age;
}
public void setAge ( int age) {
this . age = age;
}
@Override
public boolean equals ( Object o) {
if ( this == o) return true ;
if ( o == null || getClass ( ) != o. getClass ( ) ) return false ;
student2 student = ( student2) o;
return age == student. age && Objects . equals ( name, student. name) ;
}
@Override
public int hashCode ( ) {
return Objects . hash ( name, age) ;
}
@Override
public String toString ( ) {
return "student{" +
"name='" + name + '\'' +
", age=" + age +
'}' ;
}
}
class ComparatorTest2 implements Comparator < student2> {
@Override
public int compare ( student2 o1, student2 o2) {
if ( o1. getAge ( ) > o2. getAge ( ) ) {
return 1 ;
} else if ( o1. getAge ( ) == o2. getAge ( ) ) {
return o1. getName ( ) . compareTo ( o2. getName ( ) ) ;
}
return - 1 ;
}
}
public class TreeMapTest {
public static void main ( String [ ] args) {
TreeMap < student2, String > treeMap= new TreeMap < > ( new ComparatorTest2 ( ) ) ;
student2 s1 = new student2 ( "o" , 18 ) ;
student2 s2 = new student2 ( "a" , 19 ) ;
student2 s3 = new student2 ( "s" , 19 ) ;
treeMap. put ( s1, "o" ) ;
treeMap. put ( s2, "a" ) ;
treeMap. put ( s3, "s" ) ;
Set < student2> keys1 = treeMap. keySet ( ) ;
for ( student2 key: keys1) {
System . out. println ( key+ "" + treeMap. get ( key) ) ;
}
}
}
四 Iterator迭代器
4.1 Iterator送代器接口介绍
Collection接口继承了Iterable接口,在该接口中包含一个名为iterator的抽象方法,所有实现了Collection接口的容器类对该方法做了具体实现。 iterator方法会返回一个lterator接口类型的选代器对象,在该对象中包含了三个方法用于实现对单例容器的选代处理。 lterator对象的工作原理: lterator接口方法:
方法 说明 boolean hasNext(); 判断游标当前位置是否有元素,如果有返回true,否则返回false; Object next() 获取当前游标所在位置的元素,并将游标移动到下一个位置 void remove() 删除游标当前位置的元素,在执行完next后该操作只能执行一次;
4.2 栗子
import java. util. ArrayList ;
import java. util. Iterator ;
public class IteratorListTest {
public static void main ( String [ ] args) {
ArrayList < String > list = new ArrayList < > ( ) ;
list. add ( "a" ) ;
list. add ( "b" ) ;
list. add ( "c" ) ;
Iterator < String > iterator = list. iterator ( ) ;
for ( Iterator < String > it= list. iterator ( ) ; it. hasNext ( ) ; ) {
System . out. print ( it. next ( ) + " " ) ;
}
System . out. println ( ) ;
while ( iterator. hasNext ( ) ) {
System . out. print ( iterator. next ( ) + " " ) ;
}
System . out. println ( ) ;
Iterator < String > iterator2 = list. iterator ( ) ;
while ( iterator2. hasNext ( ) ) {
if ( "c" . equals ( iterator2. next ( ) ) ) {
iterator2. remove ( ) ;
} ;
}
for ( Iterator < String > it= list. iterator ( ) ; it. hasNext ( ) ; ) {
System . out. print ( it. next ( ) + " " ) ;
}
}
}
五 Collections工具类
5.1 Collections工具类简介
Collections是一个工具类 ,它提供了对Set、List、Map进行排序、填充、查找元素的辅助方法 。该类中所有的方法都为静态方法。 常用方法:
方法 说明 void sort(List) 对List容器内的元素排序,排序的规则是按照升序进行排序。 void shuffle(List) /对List容器内的元素进行随机排列。 void reverse(List 对List容器内的元素进行逆续排列。 void fill(List,Object) 用一个特定的对象重写整个List容器。 int binarySearch(List,Object) 对于顺序的List容器,采用折半查找的方法查找特定对象
5.2 栗子
import java. util. ArrayList ;
import java. util. Collections ;
public class CollectionsTest {
public static void main ( String [ ] args) {
ArrayList < String > list = new ArrayList < > ( ) ;
list. add ( "c" ) ;
list. add ( "d" ) ;
list. add ( "b" ) ;
list. add ( "a" ) ;
Collections . sort ( list) ;
for ( String str: list) {
System . out. print ( str+ " " ) ;
}
System . out. println ( ) ;
Collections . reverse ( list) ;
for ( String str: list) {
System . out. print ( str+ " " ) ;
}
System . out. println ( ) ;
Collections . shuffle ( list) ;
for ( String str: list) {
System . out. print ( str+ " " ) ;
}
}
}