集合-map&&泛型

来源:互联网 发布:血色残阳知乎 编辑:程序博客网 时间:2024/06/15 00:18
集合框架

是为表示和操作集合而规定的一种统一的标准的体系结构。任何集合框架都包含三大块内容:对外的接口、接口的实现和对集合运算的算法。

Collection 接口

Collection对象是将多个元素组成一个单元的对象

集合用于存储、检索和操纵数据

集合框架是用于表示和操纵集合的统一体系结构

集合框架的优点

提供有用的数据结构和算法,从而减少编程工作

提高了程序速度和质量,因为它提供了高性能的数据结构和算法

允许不同 API 之间的互操作,API之间可以来回传递集合

可以方便地扩展或改写集合

常用集合接口

List接口

特点: 有序的集合;允许重复
常见实现类:ArrayListLinkedList,Vector

 

Set接口

特点: 不允许重复
常见实现类:HashSetTreeSet

 

Map接口

特点: 用于存储键/值映射关系
常见实现类:HashMapHashtableProperties

泛型

泛型提供了一个可以对集合使用类型参数来指定元素的类型

 如:

ArrayList<String> list = new ArrayList<String>();

 

注意:

1、泛型的类型参数只能是引用类型,不能是基本数据类型。

 

2、泛型的类型参数可以有多个。

 

3、泛型的参数类型可以使用extendssuper语句

 

4、泛型的参数类型还可以是通配符(?)类型。

 

泛型的使用

public class Couple<T> {private T first;private T second;public Couple(T first, T second) {super();this.first = first;this.second = second;}public T getFirst() {return first;}public void setFirst(T first) {this.first = first;}public T getSecond() {return second;}public void setSecond(T second) {this.second = second;}}

public class CoupleTest {public static void main(String[] args) {// TODO Auto-generated method stubCouple<String> cp1 = new Couple<String>("first", "second");String first = cp1.getFirst();Couple<Integer> cp2 = new Couple<Integer>(1, 2);int second = cp2.getSecond();//返回类型为Integer,有自动拆箱过程}}

Map遍历

@Testpublic void test7(){Map<String,Integer> map=new HashMap<String,Integer>();map.put("jessica",100);map.put("tom",200);map.put("den",300);//第一种:普遍使用,二次取值System.out.println("通过Map.keySet遍历key和value:");for (String key : map.keySet()) {System.out.println("key= "+ key + " and value= " + map.get(key));}//第二种:通过Map.entrySet使用iterator遍历key和value:Iterator<Entry<String, Integer>> it = map.entrySet().iterator();while (it.hasNext()) {Entry<String, Integer> entry = it.next();System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());}//第三种:推荐,尤其是容量大时System.out.println("通过Map.entrySet遍历key和value");for (Entry<String, Integer> entry : map.entrySet()) {System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());}//第四种:通过Map.values()遍历所有的value,但不能遍历key"for (Integer v : map.values()) {System.out.println("value= " + v);}}

演示创建map集合,给集合添加元素,根据key获取元素的值

@Testpublic void test(){Map<Long,Student> ms = new HashMap<Long, Student>();Student s =null;s = new Student("zs");Long id = s.getId();ms.put(id, s);//给map添加元素s = new Student("zs");id = s.getId();ms.put(id, s);//给map添加元素s = new Student("zs");id = s.getId();ms.put(id, s);//给map添加元素System.out.println(ms);//根据key获取元素的值Student stu = ms.get(1L);System.out.println(stu);}

set集合中添加数据时,如果数据已经存在,数据添加不进去,但在map中,如果键已经存在,会把原来对应的键的值覆盖

@Testpublic void test4(){Set<Long> ls = new HashSet<Long>();System.out.println(ls.add(1L));//trueSystem.out.println(ls.add(1L));//false,元素已经存在Map<Long,Student> ms = new HashMap<Long, Student>();ms.put(1L, new Student("张三"));ms.put(2L, new Student("李四"));System.out.println(ms);ms.put(2L, new Student("王五"));//key2L已经存在,这个元素会怎么处理?覆盖System.out.println(ms);}




0 0
原创粉丝点击