------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
一、Map:存储键值对
Map:一次添加一对元素。Collection 一次添加一个元素。
Map也称为双列集合,Collection集合称为单列集合。
其实map集合中存储的就是键值对。一个映射不能包含重复的键
注:map集合中必须保证键的唯一性。
常用方法:
1,添加。
value put(key,value):返回前一个和key关联的值,如果没有返回null.
存相同键,值会覆盖
2,删除。
void clear():清空map集合。
value remove(key):根据指定的key翻出这个键值对。
3,判断。
boolean containsKey(key):
boolean containsValue(value): //是否包含值
boolean isEmpty();
4,获取。
value get(key):通过键获取值,如果没有该键返回null。当然可以通过返回null,来判断是否包含指定键。
int size(): 获取键值对的个数。
===================================================================================================
取出第一种方式:
<span style="font-size:18px;">//取出map中的所有元素。
//原理,通过keySet方法获取map中所有的键所在的Set集合,在通过Set的迭代器获取到每一个键,
//在对每一个键通过map集合的get方法获取其对应的值即可。
Set<Integer> keySet=map.keySet();
Iterator<Integer> iterator = keySet.iterator();
while (iterator.hasNext()) {
<span style="white-space:pre"> </span>Integer key = iterator.next();
<span style="white-space:pre"> </span>String value= map.get(key);
<span style="white-space:pre"> </span>System.out.println(key+value);
}</span>
===============================================================================================
第二种方式:
<span style="font-size:18px;">// * 通过Map转成set就可以迭代。
// * 找到了另一个方法。entrySet。
// * 该方法将键和值的映射关系作为对象存储到了Set集合中,而这个映射关系的类型就是Map.Entry类型
Set<Map.Entry<Integer ,String>> entrySet = map.entrySet();
Iterator<Map.Entry<Integer ,String>> it = entrySet.iterator();
while (it.hasNext()) {
Map.Entry<Integer, String> entry = it.next();
String value = entry.getValue();
int key = entry.getKey();
System.out.println(key+value);
}</span>
=================================================================================================
<span style="font-size:18px;">//只输出value
Collection<String> values=map.values();
for (Iterator iterator = values.iterator(); iterator.hasNext();) {
<span style="white-space:pre"> </span>System.out.println(iterator.next());
}</span>
二、Map常用的子类:
|--Hashtable :内部结构是哈希表,是线程同步的。不允许null作为键,null作为值。
作为键的对象必须实现hashCode方法和equals方法
|--HashMap : 内部结构是哈希表,不是同步的。允许null作为键,null作为值。
|--TreeMap : 内部结构是二叉树,不是同步的。可以对Map集合中的键进行排序。
|--Properties:用来存储键值对型的配置文件的信息,可以和IO技术相结合。
特点:1.该集合的键和值都是字符串类型
2.集合中的数据可以保存在流中,或者在流获取。
用于操作以键值对形式存在的配置文件
Properties prop= new Properties();
prop.setProperty(“”,””);
Set<String> names = prop.stringPropertyNames();
For(String name:name){
String value=prop.getProperty(name);
}
三、操作练习:
public class StudentText {
public static void main(String[] args) {
//创建map集合
Map<Student, String> map = new HashMap<Student, String>();
//存入对象
map.put(new Student("ss",23), "1");
map.put(new Student("ff",24), "2");
map.put(new Student("gg",25), "3");
//取出
Set<Student> key =map.keySet();
for (Iterator iterator = key.iterator(); iterator.hasNext();) {
Student student = (Student) iterator.next();
String value= map.get(student);
// System.out.println(student+"..."+value);
Set<Map.Entry<Student, String>> entries = map.entrySet();
for (Iterator<Map.Entry<Student, String>> iterator2 = entries.iterator();iterator2.hasNext();){
Map.Entry<Student, String> s =iterator2.next();
Student student2 = s.getKey();
String string = s.getValue();
System.out.println(student2+"....."+string);
}
}
}
}
四:集合框架的工具类:
Collections:集合框架工具类,定义的是静态方法.
Collections和Collection区别:Collection是集合框架的顶层接口
Collections:Collections.sort(list); //排序
二分查找:int index =Collections.binarySearch(list,“”)
/交换位置 Collections.swap(list,I,j);
//最大值 Collections.max(list);
//顺序倒转(将已有比较器倒序)
new TreeSet<String>(Collections.reverseOrder());
//元素替换Collections.replaceAll(list,oldval,newval);
//全部替换Collections.fill(list)
//随机安放Collections.shuffle(list)
//集合加锁变同步Collections.synchronizedCollection/ synchronizedList
五、Arrays:集合框架的工具类。里面的方法都是静态的。
equals方法 比较两个数组是否相同
Arrays.toString(数组);
List asList(数组)将数组转成集合。
* 好处:其实可以使用集合的方法操作数组中的元素。
* 注意:数组的长度是固定的,所以对于集合的增删方法是不可以使用的
* 否则会发生UnsupportedOperationException
String[] arr = {"abc","haha","xixi"};
List<String> list = Arrays.asList(arr); //将数组转成集合。
boolean b1 = list.contains("xixi");
集合转成数组呢?
* 使用的就是Collection接口中的toArray方法。
* 集合转成数组:可以对集合中的元素操作的方法进行限定。不允许对其进行增删。
* toArray方法需要传入一个指定类型的数组。
长度该如何定义:
* 如果长度小于集合的size,那么该方法会创建一个同类型并和集合相同size的数组。
* 如果长度大于集合的size,那么该方法就会使用指定的数组,存储集合中的元素,其他位置默认为null。
长度就指定为,集合的size。
List<String> list = new ArrayList<String>();
list.add("gsdsd");
String[] arr=list.toArray(new String[1]);
System.out.println(Arrays.toString(arr));