ITeye博客

黑马程序员- JAVA/集合(3)

javappx · 2013-05-29 22:47 · 编程语言 · Markdown

------- android培训、java培训、期待与您交流! ----------

 

 

 

15.01   02 

Set:无序,不可以重复元素

--HashSet:数据结构是哈希表,线程是非同步的

保证元素唯一性的原理,判断元素的hashCode值是否相同

如果相同,还会继续判断元素的equals方法,是否为true

--TreeSet:可以对Set集合中的元素进行排序

底层数据结构是二叉树

1,保证元素唯一性的依据compareTo方法return 0 。

 

2,当元素不具备比较性时,或者具备的比较性不是所需要的

这时就需要让集合自身具备比较性

在集合初始化时,就有了比较方式

需求;

往TreeSet集合中存储自定义对象学生

想按照学生的年龄进行排序

 

记住:排序时,当主要条件相同时,一定判断次要条件

 

*/

 

import java.util.*;

 

class TreeSetDemo

{

public static void main(String[] args) 

{

TreeSet ts = new TreeSet();

 

ts.add(new Student("lisi02",22));

ts.add(new Student("lisi007",20));

ts.add(new Student("lisi09",19));

ts.add(new Student("lisi08",19));

 

Iterator it = ts.iterator();

 

while (it.hasNext())

{

Student stu = (Student)it.next();

System.out.println(stu.getName()+"::"+stu.getAge());

}

}

}

 

class Student implements Comparable//该接口强制让学生具备比较性

{

private String name;

private int age;

 

Student(String name,int age)

{

this.name = name;

this.age = age;

}

 

public int compareTo(Object obj)

{

if (!(obj instanceof Student))

throw new RuntimeException("不是学生对象");

Student s = (Student)obj;

 

System.out.println(this.name+"::compare to::"+s.name);

if (this.age > s.age)

return 1;

if(this.age == s.age)

{

return this.name.compareTo(s.name);

};

return -1;

}

 

public String getName()

{

return name;

}

public int getAge()

{

return age;

}

}

 

 

15.04集合框架 实现Comparator方式排序 

 

 

 

import java.util.*;

/*

当元素自身不具备比较性,或者具备的比较性不是所需要的

这时需要让容器自身具备比较性

定义了比较器,将比较器做为参数传递给TreeSet集合的构造函数

 

当两种顺序都存在时,以比较器为主

 

定义一个类,实现Comparator接口,覆盖compare方法

*/

 

class TreeSetDemo2 

{

public static void main(String[] args) 

{

TreeSet ts = new TreeSet(new MyCompare());

 

ts.add(new Student("lisi02",22));

ts.add(new Student("lisi007",20));

ts.add(new Student("lisi09",19));

ts.add(new Student("lisi08",19));

ts.add(new Student("lisi007",21));

 

Iterator it = ts.iterator();

 

while (it.hasNext())

{

Student stu = (Student)it.next();

System.out.println(stu.getName()+"::"+stu.getAge());

}

}

}

class Student implements Comparable//该接口强制让学生具备比较性

{

private String name;

private int age;

 

Student(String name,int age)

{

this.name = name;

this.age = age;

}

 

public int compareTo(Object obj)

{

if (!(obj instanceof Student))

throw new RuntimeException("不是学生对象");

Student s = (Student)obj;

 

System.out.println(this.name+"::compare to::"+s.name);

if (this.age > s.age)

return 1;

if(this.age == s.age)

{

return this.name.compareTo(s.name);

};

return -1;

}

public String getName()

{

return name;

}

public int getAge()

{

return age;

}

}

 

class MyCompare implements Comparator

{

public int compare(Object o1,Object o2)

{

Student s1 = (Student)o1;

Student s2 = (Student)o2;

 

int num = s1.getName().compareTo(s2.getName());

 

if (num == 0)

{

if (s1.getAge() > s2.getAge())

return 1;

if (s1.getAge() == s2.getAge())

return 0;

return -1;

} 

return num;

}

}

 

 

1506  集合框架 泛型概述

 

 

 

集合定义时候指定要存放的数据类型

 

泛型在集合框架中很常见

import java.util.*;

/*

泛型:JDK1.5版本以后出现新特性,用于解决安全问题,是一个类型2012-10-21安全机制

 

好处:

1。将运行时期出现问题ClasscastException转移到了编译时期,提高安全性

2.避免了强制转换

*/

class GenericDemo 

{

public static void main(String[] args) 

{

ArrayList<String> al = new ArrayList<String>();//定义的时候就定义集合类型,用尖括号

 

al.add("abc001");

al.add("abc088");

al.add("abcd1");

//al.add(4);//这个添加暴露了安全问题,类型转换异常

 

Iterator<String> it = al.iterator();

while (it.hasNext())

{

String s = it.next();

 

System.out.println(s+":"+s.length());

}

}

}

 

 

1507 泛型使用

 

 

 

import java.util.*;

 

class  GenericDemo2

{

public static void main(String[] args) 

{

TreeSet<String> ts = new TreeSet<String>(new LenComparator());//传入比较器对象

 

ts.add("abcd");

ts.add("cc");

ts.add("cba");

ts.add("aaa");

ts.add("z");

ts.add("hahaha");

 

Iterator<String> it = ts.iterator();

 

while (it.hasNext())

{

String s = it.next();

System.out.println(s);

}

}

}

 

class LenComparator implements Comparator<String>//按长度排序

{

public int compare(String o1,String o2)

{

int num = new Integer(o2.length()).compareTo(new Integer(o1.length()));

 

if(num == 0)

return o2.compareTo(o1);

return num;

 

 

}

}

 

 

1508 集合框架-- 泛型类

 

class Worker

{

}

class Student

{

}

/*

什么时候定义泛型类?

当类中要操作的引用数据类型不确定的时候,早期定义Object来完成扩展

现在定义泛型来完成扩展

*/

class Utils<QQ>//泛型类  该类操作的是对象,所以可以加泛型

{

private QQ q;

public void setObject(QQ q)

{

this.q = q;

}

public QQ getObject()

{

return q;

}

}

 

class GenericDemo3 

{

public static void main(String[] args) 

{

Utils<Worker> u = new Utils<Worker>();

 

u.setObject(new Worker());  new Student() 就编译错误 让错误发生在了编译阶段

Worker w = u.getObject();//不用强制转换了  

}

}

 

 

class GenericDemo3 

{

public static void main(String[] args) 

{

Utils  u = new Utils();

 

u.setObject(new Worker());

Worker w =(Worker) u.getObject();//强制转换

}

}

 

 

 

 

1509 集合框架--泛型方法

 

 

import java.util.*;

/*

class Demo<T>

{

public void show(T t)

{

System.out.println("show:"+t);

}

public void print(T t)

{

System.out.println("print:"+t);

}

}

/*

/*

泛型类定义的泛型,在整个类中有效,如果被方法使用

那么泛型类的对象明确要操作的具体类型后,所有要操作的类型都已经固定了;

 

为了让不同方法可以操作不同类型,而且类型还不确定

可以将泛型定义在方法上

 

特殊之处:

静态方法不可以访问类上定义的泛型

如果静态方法操作的引用数据类型不确定,可以将泛型定义在方法上

 

位置:

泛型放在返回值前面,修饰符后面

*/

class Demo<T>  //T建立对象时候才明确,所以类中的静态方法不可以访问类上定义的泛型

{

public  void show(T t)

{

System.out.println("show:"+t);

}

public <QQ> void print(QQ q)

{

System.out.println("print:"+q);

}

public  static <W> void method(W t)

{

System.out.println("method:"+t);

}

}

class GenericDemo4 

{

public static void main(String[] args) 

{

Demo<String> d = new Demo<String>();

d.show("haha");

d.print("zzz");

d.method("new method");

 

/*Demo<Integer> d = new Demo<Integer>();

d.show(new Integer(4));

d.print(4);

 

Demo<String> d1 = new Demo<String>();

d1.print("haha");

d1.show("a");*/

}

}

 

 

 

1511 泛型接口

 

 

 

//泛型定义在接口上

interface Inter<T>

{

void show(T t);

}

 

/*

class InterImpl  implements Inter<String>

{

public void show(String t)

{

System.out.println("show:"+t);

}

}

*/

class InterImpl <T> implements Inter<T>

{

public void show(T t)

{

System.out.println("show:"+t);

}

}

class GenericDemo5 

{

public static void main(String[] args) 

{

InterImpl<Integer> i = new InterImpl<Integer>();

i.show(4);

}

}

 

 

 

 

1512泛型限定

 

 

import java.util.*;

/*

?通配符:也可以理解为占位符

泛型的限定:

? extends E:可以接收E类型或者E的子类型--》上限

? super E:可以接收E类型或者E的父类型--》下限限定

*/

class GenericDemo6 

{

public static void main(String[] args) 

{

ArrayList <Person> a1 = new ArrayList<Person>();

 

a1.add(new Person("abc1"));

a1.add(new Person("abc2"));

a1.add(new Person("abc3"));

 

 

ArrayList <Student> a11 = new ArrayList<Student>();

 

a11.add(new Student("--abc1"));

a11.add(new Student("--abc2"));

a11.add(new Student("--abc3"));

 

printColl(a1);

printColl(a11);

}

public static void printColl(ArrayList<? extends Person> a1)

{

Iterator <? extends Person> it = a1.iterator();

while (it.hasNext())

{

System.out.println(it.next().getName());//这时只能使用父类中的方法

}

}

}

class Person

{

private String name;

Person(String name)

{

this.name = name;

}

public String getName()

{

return name;

}

}

 

class Student extends Person

{

Student(String name)

{

super(name);

}

}

class Student implements Comparable<Person>

{

public int compareTo(Person s)

(

this.getName();

)

}

 

class Comp implements Comparator<Person>//可以接收Student及其父类

{

public int compare (Person s1,Person s2)

{

return s1.getName().compareTo(s2.getName());

}

}

 

TreeSet<Student> ts = new TreeSet<Student>();

ts.add(new Student("abc1"));

ts.add(new Student("abc2"));

ts.add(new Student("abc3"));

 

 

 

 

 

 

1513泛型限定

 

 

import java.util.*;

 

class GenericDemo7 

{

public static void main(String[] args) 

{

TreeSet<Worker> ts = new TreeSet<Worker>(new Comp());

 

ts.add(new Worker("abc01"));

ts.add(new Worker("abc03"));

ts.add(new Worker("abc02"));

ts.add(new Worker("abc00"));

 

Iterator<Worker> it = ts.iterator();

while (it.hasNext())

{

System.out.println(it.next().getName());

}

}

}

 

class Comp implements Comparator<Person>

{

public int compare(Person s1,Person s2)

{

return s1.getName().compareTo(s2.getName());

}

}

 

class Person

{

private String name;

Person(String name)

{

this.name = name;

}

public String getName()

{

return name;

}

}

 

class Student extends Person

{

Student(String name)

{

super(name);

}

}

 

class Worker extends Person

{

Worker(String name)

{

super(name);

}

}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。