黑马程序员-Java 集合框架(二)-Map、Collections、新特性
来源:互联网 发布:股票涨幅计算软件 编辑:程序博客网 时间:2024/05/29 21:31
——Java培训、Android培训、iOS培训、.Net培训、期待与您交流! ——-
一、Map集合
Map是一种存储键值对的存储容器,而且保证键的唯一性。Map中的存储的一对元素:一个是键,一个是值,键与值之间有对应(映射)关系。
因此Map中键值必须保证唯一。
Map集合不属于Collection,它与Collection有着以下几点不同:
1.Collection一次存一个元素;Map一次存一对元素。
2.Collection是单列集合;Map是双列集合。
Map有三个子类:
hashMap、TreeMap和Hashtable。下面会逐一介绍。
- Map集合的共性函数
E put(key,value);//添加新键值对,当存储的键相同时,新的值会替换老的值,并将老值返回。如果键没有重复,返回null。
void clear();//清空
value remove(key) ;//删除指定键。
boolean isEmpty();判断Map集合是否为空
boolean containsKey(key);是否包含key
boolean containsValue(value);是否包含value
int size();返回Map长度
value get(key);通过指定键获取对应的值。如果返回null,可以判断该键不存在。
注意:Map没有List和Set那种迭代器遍历方式,因此Map使用KeySet和entrySet俩种方式遍历;
下面是第一种方法使用keySet:
import java.util.*;class Main { public static void main(String[] args) { HashMap<String, String> map = new HashMap<String, String>(); map.put("aa", "11"); map.put("bb", "22"); // 取出键的集合 Set<String> set = map.keySet(); //获取所有key的集合 Iterator<String> it = set.iterator(); // 遍历键的集合 while(it.hasNext()) { String key = it.next();//得到key // 根据键获取值 System.out.println( key + "," + map.get(key) ); } }}
另一种方式是使用entrySet实现,示例代码如下:
import java.util.*;class Main { public static void main(String[] args) { HashMap<String, String> map = new HashMap<String, String>(); map.put("aa", "11") map.put("bb","22") // 取出键-值的集合 Set<Map.Entry<String, String>> set = map.entrySet(); // 遍历键-值集合 Iterator<Map.Entry<String, String>> it = set.iterator(); while(it.hasNext()) { Map.Entry<String, String> entry = it.next(); // 通过getKey和getValue方法可获取键和值 System.out.println(entry.getKey() + " "+ entry.getValue()); } }}
- HashMap
底层是哈希表数据结构,是线程不同步的。可以存储null键,null值。
它是无序的一般都使用这个HashMap来存储键值对,下面就是个简单示例:
import java.util.*;class Main { public static void main(String[] args) { HashMap<String, String> map = new HashMap<String, String>(); map.put("aa", "11"); map.put("bb", "22"); // 取出键的集合 Set<String> set = map.keySet(); //获取所有key的集合 Iterator<String> it = set.iterator(); // 遍历键的集合 while(it.hasNext()) { String key = it.next();//得到key // 根据键获取值 System.out.println( key + "," + map.get(key) ); } }}
Hashtable:底层是哈希表数据结构,是线程同步的。不可以存储null键,null值。现在基本已经不使用,被HashMap取代。
- TreeMap
底层是二叉树结构,可以对map集合中的键进行指定顺序的排序。
同样的TreeMap和TreeSet类似,需要实现Comparable接口或者在创建TreeMap对象时指定比较器。下面是一个统计字符的实例:
import java.util.*;import java.lang.*;class CharKey implements Comparable<CharKey> { private String key; public CharKey(char key) { this.key = String.valueOf(key); } public int compareTo(CharKey o) { return this.key.compareTo(o.getKey()); } public String getKey() { return this.key; } // 用第一个字符的Unicode代码点作为哈希值 public int hashCode() { return this.key.codePointAt(0); } // 判断字符是否相同 public boolean equals(Object o) { if(!(o instanceof CharKey)) { throw new RuntimeException("类型不匹配"); } CharKey key = (CharKey) o; return this.key.equals(key.getKey()); }}class Main { public static void main(String[] args) { String str = "sdfgzxcvasdfxcvdf"; // 创建TreeMap对象,CharKey作为键,Integer作为值 TreeMap<CharKey, Integer> map = new TreeMap<CharKey, Integer>(); // 遍历统计的结果 Set<Map.Entry<CharKey, Integer>> entrySet = map.entrySet(); Iterator<Map.Entry<CharKey, Integer>> it = entrySet.iterator(); while(it.hasNext()) { Map.Entry<CharKey, Integer> entry = it.next(); CharKey key = entry.getKey(); Integer value = entry.getValue(); System.out.println(key.getKey()+"("+ value +")"); } }}
二、Collections
Collections为java.util包中为集合框架提供的工具类,其内部全部是静态函数。之前我们提到的Collection与Collections不同,区别如下:
Collections是个java.util下的类,是针对集合类的一个工具类,提供一系列静态方法,实现对集合的查找、排序、替换、线程安全化(将非同步的集合转换成同步的)等操作。
Collection是个java.util下的接口,它是各种集合结构的父接口,继承于它的接口主要有Set和List,提供了关于集合的一些操作,如插入、删除、判断一个元素是否其成员、遍历等。
静态函数主要有:
Collections.sort(list);//list集合进行元素的自然顺序排序。
Collections.sort(list,new ComparatorByLen());//按指定的比较器方法排序。
Collections.reverseOrder();//逆向反转排序。
Collections.shuffle(list);//随机对list中的元素进行位置的置换。
Collections.binarySearch(list, E);//返回该元素的位置,下标。
下面是一个简单排序的代码:
import java.util.*;class Main { public static void main(String[] args) { ArrayList<String> list = new ArrayList<String>(); list.add("a"); list.add("b"); list.add("c"); list.add("dd"); list.add("abcdef"); // 排序前的list System.out.println(list); Collections.sort(list); // 排序后的list System.out.println(list); Collections.reverseOrder();//逆向反转排序。 System.out.println(list); }}
三、Arrays
用于操作数组对象的工具类,里面都是静态方法。
List list = Arrays.asList(arr);//将arr数组转成list集合,根元素类型不同而有俩种方式:
// 基本数据类型数组转集合int[] nums = {1, 3, 2};List<int[]> list = Arrays.asList(nums);// 基本数据类型数组转集合int[] strs = {"hello", "hi", "oo"};List<String> list = Arrays.asList(strs);
集合转成数组,使用toArray(t[] t)方法可以将集合转为数组。,示例代码如下:
String[] strs = list.toArray(new String[0]);
那数组和集合有什么区别呢?
可以通过list集合中的方法来操作数组中的元素。
而数组限定了对集合中的元素进行增删操作,只要获取这些元素即可。
四、Jdk5.0新特性
- foreach:
增强了for循环,可以使用foreach语句。格式为:for(数据类型 变量:被遍历Collection或数组)。
示例如下:
import java.util.*;class Main { public static void main(String[] args) { HashMap<String, String> map = new HashMap<String, String>(); map.put("aa", "11"); map.put("bb", "22"); // 取出键的集合 for(Object obj : map.entrySet()) { Map.Entry entry = (Entry) obj; // obj 依次表示Entry System.out.println(entry.getKey() + "=" + entry.getValue());}
- 自动装箱/自动拆箱:
在 java程序中所有的数据都需要当做对象来处理,针对8种基本数据类型提供了包装类,如下:int –> Integer,byte –> Byte,short –> Short,long –> Long,char –> Character,double –> Double,float –> Float,boolean –> Boolean
自动装箱:为将基本数据类型封装为对象类型,来符合java的面向对象;
例如用int来举例:
Integer num = 10;
可以解析为 :Integer num = new Integer(10);10是属于基本数据类型的,原则上它是不能直接赋值给一个对象Integer的,但jdk1.5后你就可以进行这样的声明。
自动拆箱:故名思议就是将对象重新转化为基本数据类型
还是用int举例:
int num1 = num;自动拆箱有个很典型的用法就是在进行运算的时候:因为对象时不恩直接进行运算的,而是要转化为基本数据类型后才能进行加减乘除。
- 静态导入:
全部是静态方法,可以将其直接导入使用,不需要再指明方法名。如Arrays代码:
import java.util.*;import static java.util.Arrays.*;class Main { public static void main(String[] args) { int[] arr = {2, 4, 2, 1, 9, 8}; sort(arr);// 直接调用Arrays内的静态方法sort System.out.println(Arrays.toString(arr)); }}
- 黑马程序员-Java 集合框架(二)-Map、Collections、新特性
- 黑马程序员——Java之集合框架工具类Collections、Arrays以及Java新特性
- 黑马程序员——Java基础---泛型、集合框架工具类:Collections和Arrays、JDK 1.5新特性
- 黑马程序员--集合框架(map集合,Collections,Arrays)
- 黑马程序员---Java集合框架(二)--Map
- 黑马程序员_(集合框架(Map,Collections,Arrays))
- 黑马程序员——Java语言基础:集合框架(Collection、Map,工具类Collections、Arrays)
- 黑马程序员——Java基础---集合<二>Map,Arrays,Collections
- 黑马程序员---集合-Map集合,Collections类
- 黑马程序员——Java基础—集合(Map、Collections)
- 黑马程序员——Java基础---集合框架(二)及工具类Collections、Arrays
- 黑马程序员——Java集合框架(三)之Map集合、Collections与Arrays工具类
- 黑马程序员-Java集合框架Map
- 黑马程序员java之集合框架Map
- 黑马程序员 --- Map集合 , Collections,Arrys
- 黑马程序员--集合(Map以及Collections)
- 黑马程序员--java基础--集合框架Set,集合框架map
- 黑马程序员——java基础——集合框架(二: 双列集合 (Map体系))
- Python学习笔记(6),实践
- 黑马程序员--Java基础--类和类之间的基本关系
- Android Sensor (g-sensor) 整体架构流程(mtk)
- 外来键:foreign与reference
- 运用HOG特征与SVM检测行人
- 黑马程序员-Java 集合框架(二)-Map、Collections、新特性
- hdoj 时钟 2180 (数学)
- sicily 8843 Ranking and Friendship
- Heritrix的介绍与使用
- java基础
- 5个经典PV操作题(附答案)
- 数组
- nyoj 1185 最大最小值【线段树】
- 使用WebView加载网页