黑马程序员----集合

来源:互联网 发布:java 重载 为什么 编辑:程序博客网 时间:2024/06/05 16:08

----------------------ASP.Net+Unity开发.Net培训、期待与您交流! ---------------------

首先要说的是:集合中存放的是对象的引用,而非对象本身(切记)

1、Set(集)

  • 集合中的对象不按特定方式排列,并且没有重复对象,set接口主要有两个实现类HashSet和TreeSet。
  • HashSet类按照哈希算法来存取集合中的对象,存取速度比较快,HashSet类还有一个子类LinkedHashSet类,不仅实现了哈希算法,而且实现了链表数据结构
  • TreeSet类实现了SortedSet接口,具有排序功能.

package com.itheima;import java.util.Comparator;import java.util.TreeSet;/** * 第10题:声明类Student,包含3个成员变量:name、age、score,  * 创建5个对象装入TreeSet,按照成绩排序输出结果(考虑成绩相同的问题) *  */public class Test10 {public static void main(String[] args) {TreeSet<Student> ts = new TreeSet<Student>(//定义匿名内部类进行降序排序,实现comparator接口new Comparator<Object>() {public int compare(Object o1, Object o2) {Student s1 = (Student) o1;Student s2 = (Student) o2;return s1.getScore() >= s2.getScore() ? -1 : 1;//采用降序排列 -1:1;则为升序排列}});ts.add(new Student("张三", 10, 76));ts.add(new Student("李四", 12, 84));ts.add(new Student("王五", 16, 91));ts.add(new Student("赵六", 20, 87));ts.add(new Student("钱七", 30, 84));java.util.Iterator<Student> it = ts.iterator();//输出排序后的tswhile (it.hasNext()) {Student student = (Student) it.next();System.out.println(student);}}}class Student implements Comparable<Object> {// 实现comparable借口private String name;private int age;private double score;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;}public double getScore() {return score;}public void setScore(double score) {this.score = score;}public Student(String name, int age, double score) {this.name = name;this.age = age;this.score = score;}// 用于消除相同元素,姓名和年龄相同则视为同一对象@Overridepublic int compareTo(Object o) {Student student = (Student) o;int num = this.name.compareTo(student.name);return 0 == num ? new Integer(this.age).compareTo(new Integer(student.age)) : num;}public String toString() {return this.name + " 年龄" + this.age + " 成绩"+ this.score;}}


2、List(列表)

对象以线性方式存储,集合中的对象按索引位置排序,可以有重复对象,允许按照对象在集合中的索引位置检索对象.

实现类有LinkedList,ArrayList和Vector

1>ArrayList和LinkedList的大致区别 
①.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构。 
②.对于随机访问get和set,ArrayList觉得优于LinkedList,因为LinkedList要移动指针。 
③.对于新增和删除操作add和remove,LinedList比较占优势,因为ArrayList要移动数据

2>ArrayList和Vector比较相似,区别在于:vector类的实现采用了同步机制。

3>List只能对集合中的对象按索引位置排序,如果希望对List中的对象按其他特定方式排序,

可以借助Comparator接口和Collections类.

Collections类提供了操纵集合的各种静态方法,其中sort()方法用于对List中的对象进行排序:

          sort(List list):对List中的对象进行自然排序.

          sort(List list,Comparator comparator):对List中的对象进行客户化排序,comparator参数指定排序方式

package com.itheima;import java.util.ArrayList;import java.util.Collections;import java.util.List;public class ListSort {@SuppressWarnings("unchecked")public static void main (String[] args){@SuppressWarnings({"rawtypes", "serial" })List list = new ArrayList(){{add(new Integer(3));add(new Integer(4));add(new Integer(2));add(new Integer(5));}};System.out.println("排序前");for (Object obj : list) {System.out.println(obj);}Collections.sort(list);System.out.println("排序后");for (Object obj : list) {System.out.println(obj);}}}

3、Map(映射)

  1. 集合中的每一个元素包含一对键对象和一对值对象,集合中没有重复的键对象,值对象可以重复
  2. Map有两种比较常用的实现:HashMap和TreeMap
  3. HashMap:根据键的HashCode值存储数据,根据键可以直接获取它的值,具有很快的访问速度。
    1. HashMap最多只允许一条记录的键为Null;允许多条记录的值为Null;
    2. HashMap不支持线程的同步,即任一时刻可以有多个线程同时写HashMap;可能会导致数据的不一致。
    3. 如果需要同步,可以用Collections的synchronizedMap方法使HashMap具有同步的能力. 
  4. TreeMap:能够把它保存的记录根据键排序,默认是按升序排序,也可以指定排序的比较器
    1. 当用Iterator 遍历TreeMap时,得到的记录是排过序的. 
  5. System.out.println(Map)这里面存储的是键值对的关系,也就是引用。
    1. 所以直接打印会打印出这个引用所指向的具体的数值。(以前不理解的地方)

---------------------- ASP.Net+Unity开发、.Net培训、期待与您交流! 
----------------------详细请查看:http://edu.csdn.net


0 0
原创粉丝点击