Java集合

package cn.itcast_01;
 
import java.util.ArrayList;
import java.util.Collection;
 
/*
* 集合的由来:
* 我们学习的是 面向对象语言,而面向对象语言对事物的描述是通过对象体现的,为了方便对多个对象进行操作,我们就必须把这多个对象进行存储。
* 而要想存储多个对象,就不能是一个基本的变量,而应该是一个容器类型的变量,在我们目前所学过的知识里面,有哪些是容器类 型的呢?
* 数组和StringBuffer。但是呢?StringBuffer的结果是一个字符串,不一定满足我们的要求,所以我们只能选择数组,这就是对象数组。
* 而对象数组又不能适应变化的需求,因为数组的长度是固定的,这个时候,为了适应变化的需求,Java就提供了集合类供我们使用。
*
* 数组和集合的区别?
* A:长度区别
* 数组的长度固定
* 集合长度可变
* B:内容不同
* 数组存储的是同一种类型的元素
* 而集合可以存储不同类型的元素
* C:元素的数据类型问题
* 数组可以存储基本数据类型,也可以存储引用数据类型
* 集合只能存储引用类型
*
* 刚说过集合是存储多个元的,但是呢,存储多个元素我们也是有不同需求的:比如说,我要这多个元素中不能有相同的元素,
* 再比如说,我要这多个元素按照某种规则排序一下。针对不同的需求,Java就提供了不同的集合类,这样呢,Java就提供了很多个集合类。
* 这多个集合类的数据结构不同,结构不同不重要的,重要的是你要能够存储东西,并且还要能够使用这些东西,比如说判断,获取等。
* 既然这样,那么,这多个集合类是有共性的内容的,我们把这些集合类的共性内容不断的向上提取,最终就能形成集合的继承体系结构。
*
* 数据结构:数据的存储方式。
* 数据结构:数据的存储方式。
*
* Collection:是集合的顶层接口,它的子体系有重复的,有唯一的,有有序的,有无序的。(后面会慢慢的讲解)
*
* Collection的功能概述:
* 1:添加功能
* boolean add(Object obj):添加一个元素
* boolean addAll(Collection c):添加一个集合的元素
* 2:删除功能
* void clear():移除所有元素
* boolean remove(Object o):移除一个元素
* boolean removeAll(Collection c):移除一个集合的元素(是一个还是所有)
* 3:判断功能
* boolean contains(Object o):判断集合中是否包含指定的元素
* boolean containsAll(Collection c):判断集合中是否包含指定的集合元素(是一个还是所有)
* boolean isEmpty():判断集合是否为空
* 4:获取功能
* Iterator<E> iterator()(重点)
* 5:长度功能
* int size():元素的个数
* 面试题:数组有没有length()方法呢?字符串有没有length()方法呢?集合有没有length()方法呢?
* 6:交集功能
* boolean retainAll(Collection c):两个集合都有的元素?思考元素去哪了,返回的boolean又是什么意思呢?
* 7:把集合转换为数组
* Object[] toArray()
*/
1:对象数组(掌握)
(1)数组既可以存储基本数据类型,也可以存储引用类型。它存储引用类型的时候的数组就叫对象数组。
(2)案例:
用数组存储5个学生对象,并遍历数组。
 
2:集合(Collection)(掌握)
(1)集合的由来?
我们学习的是Java -- 面向对象 -- 操作很多对象 -- 存储 -- 容器(数组和StringBuffer) -- 数组
而数组的长度固定,所以不适合做变化的需求,Java就提供了集合供我们使用。
(2)集合和数组的区别?
A:长度区别
数组固定
集合可变
B:内容区别
数组可以是基本类型,也可以是引用类型
集合只能是引用类型
C:元素内容
数组只能存储同一种类型
集合可以存储不同类型(其实集合一般存储的也是同一种类型)
(3)集合的继承体系结构?
由于需求不同,Java就提供了不同的集合类。这多个集合类的数据结构不同,但是它们都是要提供存储和遍历功能的,
我们把它们的共性不断的向上提取,最终就形成了集合的继承体系结构图。
 
Collection
|--List
|--ArrayList
|--Vector
|--LinkedList
|--Set
|--HashSet
|--TreeSet
(4)Collection的功能概述(自己补齐)
A:添加功能
B:删除功能
C:判断功能
D:获取功能
E:长度功能
F:交集(了解)
G:把集合转数组(了解)
(5)Collection集合的遍历
A:把集合转数组(了解)
B:迭代器(集合专用方式)
(6)迭代器
A:是集合的获取元素的方式。
B:是依赖于集合而存在的。
C:迭代器的原理和源码。
a:为什么定义为了一个接口而不是实现类?
b:看了看迭代器的内部类实现。
(7)Collection集合的案例(遍历方式 迭代器)
集合的操作步骤:
A:创建集合对象
B:创建元素对象
C:把元素添加到集合
D:遍历集合
 
A:存储字符串并遍历
import java.util.Collection;
import java.util.ArrayList;
import java.util.Iterator;
 
public class CollectionDemo {
public static void main(String[] args) {
//创建集合对象
Collection c = new ArrayList();
 
//创建并添加元素
c.add("hello");
c.add("world");
c.add("java");
 
//遍历集合
Iterator it = c.iterator();
while(it.hasNext()) {
String s =(String) it.next();
System.out.println(s);
}
}
}
 
B:存储自定义对象并遍历
public class Student {
private String name;
private int age;
 
public Student(){}
 
public Student(String name,int age) {
this.name = name;
this.age = age;
}
 
//getXxx()/setXxx()
}
 
import java.util.Collection;
import java.util.ArrayList;
import java.util.Iterator;
 
public class StudentDemo {
public static void main(String[] args) {
//创建集合对象
Collection c = new ArrayList();
 
//创建学生对象
Student s1 = new Student("林青霞",27);
Student s2 = new Student("风清扬",30);
Student s3 = new Student("刘意",30);
Student s4 = new Student("武鑫",25);
Student s5 = new Student("刘晓曲",16);
 
//添加元素
c.add(s1);
c.add(s2);
c.add(s3);
c.add(s4);
c.add(s5);
 
//遍历集合
Iterator it = c.iterator();
while(it.hasNext()) {
Student s = (Student)it.next();
System.out.println(s.getName()+"---"+s.getAge());
}
}
}
 
3:集合(List)(掌握)
(1)List是Collection的子接口
特点:有序(存储顺序和取出顺序一致),可重复。
(2)List的特有功能:(自己补齐)
A:添加功能
B:删除功能
C:获取功能
D:迭代器功能
E:修改功能
(3)List集合的特有遍历功能
A:由size()和get()结合。
B:代码演示
//创建集合对象
List list = new ArrayList();
 
//创建并添加元素
list.add("hello");
list.add("world");
list.add("java");
 
//遍历集合
Iterator it = list.iterator();
while(it.hasNext()) {
String s =(String) it.next();
System.out.println(s);
}
System.out.println("----------");
 
for(int x=0; x<list.size(); x++) {
String s =(String) list.get(x);
System.out.println(s);
}
(4)列表迭代器的特有功能;(了解)
可以逆向遍历,但是要先正向遍历,所以无意义,基本不使用。
(5)并发修改异常
A:出现的现象
迭代器遍历集合,集合修改集合元素
B:原因
迭代器是依赖于集合的,而集合的改变迭代器并不知道。
C:解决方案
a:迭代器遍历,迭代器修改(ListIterator)
元素添加在刚才迭代的位置
b:集合遍历,集合修改(size()和get())
元素添加在集合的末尾
(6)常见数据结构
A:栈 先进后出
B:队列 先进先出
C:数组 查询快,增删慢
D:链表 查询慢,增删快
(7)List的子类特点(面试题)
ArrayList
底层数据结构是数组,查询快,增删慢。
线程不安全,效率高。
Vector
底层数据结构是数组,查询快,增删慢。
线程安全,效率低。
LinkedList
底层数据结构是链表,查询慢,增删快。
线程不安全,效率高。
 
到底使用谁呢?看需求?
分析:
要安全吗?
要:Vector(即使要,也不使用这个,后面再说)
不要:ArrayList或者LinkedList
查询多;ArrayList
增删多:LinkedList
 
什么都不知道,就用ArrayList。
(8)List集合的案例(遍历方式 迭代器和普通for)
A:存储字符串并遍历
B:存储自定义对象并遍历
/ System.out.println("remove:" + c.remove("hello"));
// System.out.println("remove:" + c.remove("javaee"));
 
// boolean contains(Object o):判断集合中是否包含指定的元素
// System.out.println("contains:"+c.contains("hello"));
// System.out.println("contains:"+c.contains("android"));
 
// boolean isEmpty():判断集合是否为空
// System.out.println("isEmpty:"+c.isEmpty());
 
//int size():元素的个数
System.out.println("size:"+c.size());
 
System.out.println("c:" + c);
}
}
====================================================================
package cn.itcast_01;
 
import java.util.ArrayList;
import java.util.Collection;
 
/*
* boolean addAll(Collection c):添加一个集合的元素
* boolean removeAll(Collection c):移除一个集合的元素(是一个还是所有)
* boolean containsAll(Collection c):判断集合中是否包含指定的集合元素(是一个还是所有)
* boolean retainAll(Collection c):两个集合都有的元素?思考元素去哪了,返回的boolean又是什么意思呢?
*/
public class CollectionDemo2 {
public static void main(String[] args) {
// 创建集合1
Collection c1 = new ArrayList();
c1.add("abc1");
c1.add("abc2");
c1.add("abc3");
c1.add("abc4");
 
// 创建集合2
Collection c2 = new ArrayList();
// c2.add("abc1");
// c2.add("abc2");
// c2.add("abc3");
// c2.add("abc4");
c2.add("abc5");
c2.add("abc6");
c2.add("abc7");
 
// boolean addAll(Collection c):添加一个集合的元素
// System.out.println("addAll:" + c1.addAll(c2));
 
//boolean removeAll(Collection c):移除一个集合的元素(是一个还是所有)
//只要有一个元素被移除了,就返回true。
//System.out.println("removeAll:"+c1.removeAll(c2));
 
//boolean containsAll(Collection c):判断集合中是否包含指定的集合元素(是一个还是所有)
//只有包含所有的元素,才叫包含
// System.out.println("containsAll:"+c1.containsAll(c2));
 
//boolean retainAll(Collection c):两个集合都有的元素?思考元素去哪了,返回的boolean又是什么意思呢?
//假设有两个集合A,B。
//A对B做交集,最终的结果保存在A中,B不变。
//返回值表示的是A是否发生过改变。
System.out.println("retainAll:"+c1.retainAll(c2));
 
System.out.println("c1:" + c1);
System.out.println("c2:" + c2);
}
}
============================================================
package cn.itcast_01;
import java.util.ArrayList;
import java.util.Collection;
 
/*
* 集合的遍历。其实就是依次获取集合中的每一个元素。
*
* Object[] toArray():把集合转成数组,可以实现集合的遍历
*/
public class CollectionDemo3 {
public static void main(String[] args) {
// 创建集合对象
Collection c = new ArrayList();
 
// 添加元素
c.add("hello"); // Object obj = "hello"; 向上转型
c.add("world");
c.add("java");
 
// 遍历
// Object[] toArray():把集合转成数组,可以实现集合的遍历
Object[] objs = c.toArray();
for (int x = 0; x < objs.length; x++) {
// System.out.println(objs[x]);
// 我知道元素是字符串,我在获取到元素的的同时,还想知道元素的长度。
// System.out.println(objs[x] + "---" + objs[x].length());
// 上面的实现不了,原因是Object中没有length()方法
// 我们要想使用字符串的方法,就必须把元素还原成字符串
// 向下转型
String s = (String) objs[x];
System.out.println(s + "---" + s.length());
}
}
}
===========================================================================
package cn.itcast_02;
 
import java.util.ArrayList;
import java.util.Collection;
 
/*
* 练习:用集合存储5个学生对象,并把学生对象进行遍历。
*
* 分析:
* A:创建学生类
* B:创建集合对象
* C:创建学生对象
* D:把学生添加到集合
* E:把集合转成数组
* F:遍历数组
*/
public class StudentDemo {
public static void main(String[] args) {
// 创建集合对象
Collection c = new ArrayList();
 
// 创建学生对象
Student s1 = new Student("林青霞", 27);
Student s2 = new Student("风清扬", 30);
Student s3 = new Student("令狐冲", 33);
Student s4 = new Student("武鑫", 25);
Student s5 = new Student("刘晓曲", 22);
 
// 把学生添加到集合
c.add(s1);
c.add(s2);
c.add(s3);
c.add(s4);
c.add(s5);
 
// 把集合转成数组
Object[] objs = c.toArray();
// 遍历数组
for (int x = 0; x < objs.length; x++) {
// System.out.println(objs[x]);
 
Student s = (Student) objs[x];
System.out.println(s.getName() + "---" + s.getAge());
}
}
}
 
package cn.itcast_02;
 
public class Student {
// 成员变量
private String name;
private int age;
 
// 构造方法
public Student() {
super();
}
 
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
 
// 成员方法
// getXxx()/setXxx()
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;
}
}
 
 
 
===========================================================================
package cn.itcast_03;
 
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
 
/*
* Iterator iterator():迭代器,集合的专用遍历方式
* Object next():获取元素,并移动到下一个位置。
* NoSuchElementException:没有这样的元素,因为你已经找到最后了。
* boolean hasNext():如果仍有元素可以迭代,则返回 true。(
*/
public class IteratorDemo {
public static void main(String[] args) {
// 创建集合对象
Collection c = new ArrayList();
 
// 创建并添加元素
// String s = "hello";
// c.add(s);
c.add("hello");
c.add("world");
c.add("j ava");
 
// Iterator iterator():迭代器,集合的专用遍历方式
Iterator it = c.iterator(); // 实际返回的肯定是子类对象,这里是多态
 
// Object obj = it.next();
// System.out.println(obj);
// System.out.println(it.next());
// System.out.println(it.next());
// System.out.println(it.next());
// System.out.println(it.next());
// 最后一个不应该写,所以,我们应该在每次获取前,如果有一个判断就好了
// 判断是否有下一个元素,有就获取,没有就不搭理它
 
// if (it.hasNext()) {
// System.out.println(it.next());
// }
// if (it.hasNext()) {
// System.out.println(it.next());
// }
// if (it.hasNext()) {
// System.out.println(it.next());
// }
// if (it.hasNext()) {
// System.out.println(it.next());
// }
// if (it.hasNext()) {
// System.out.println(it.next());
// }
 
// 最终版代码
while (it.hasNext()) {
// System.out.println(it.next());
String s = (String) it.next();
System.out.println(s);
}
}
}
 
===========================================================================
package cn.itcast_03;
 
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
 
/*
* 练习:用集合存储5个学生对象,并把学生对象进行遍历。用迭代器遍历。
*
* 注意:
* A:自己的类名不要和我们学习的要使用的API中的类名相同。
* B:复制代码的时候,很容易把那个类所在的包也导入过来,容易出现不能理解的问题。
*/
public class IteratorTest {
public static void main(String[] args) {
// 创建集合对象
Collection c = new ArrayList();
 
// 创建学生对象
Student s1 = new Student("林青霞", 27);
Student s2 = new Student("风清扬", 30);
Student s3 = new Student("令狐冲", 33);
Student s4 = new Student("武鑫", 25);
Student s5 = new Student("刘晓曲", 22);
 
// 把学生添加到集合中
c.add(s1);
c.add(s2);
c.add(s3);
c.add(s4);
c.add(s5);
 
// 遍历
Iterator it = c.iterator();
while (it.hasNext()) {
// System.out.println(it.next());
Student s = (Student) it.next();
System.out.println(s.getName() + "---" + s.getAge());
}
}
}
===========================================================================package cn.itcast_03;
 
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
 
/*
* 问题1:能用while循环写这个程序,我能不能用for循环呢?
* 问题2:不要多次使用it.next()方法,因为每次使用都是访问一个对象。
*/
public class IteratorTest2 {
public static void main(String[] args) {
// 创建集合对象
Collection c = new ArrayList();
 
// 创建学生对象
Student s1 = new Student("林青霞", 27);
Student s2 = new Student("风清扬", 30);
Student s3 = new Student("令狐冲", 33);
Student s4 = new Student("武鑫", 25);
Student s5 = new Student("刘晓曲", 22);
 
// 把学生添加到集合中
c.add(s1);
c.add(s2);
c.add(s3);
c.add(s4);
c.add(s5);
 
// 遍历
Iterator it = c.iterator();
while (it.hasNext()) {
Student s = (Student) it.next();
System.out.println(s.getName() + "---" + s.getAge());
 
// NoSuchElementException 不要多次使用it.next()方法
// System.out.println(((Student) it.next()).getName() + "---"
// + ((Student) it.next()).getAge());
 
}
// System.out.println("----------------------------------");
 
// for循环改写
// for(Iterator it = c.iterator();it.hasNext();){
// Student s = (Student) it.next();
// System.out.println(s.getName() + "---" + s.getAge());
// }
}
}
========================================================
迭代器的源码
public interface Inteator {
boolean hasNext();
Object next();
}
 
public interface Iterable {
Iterator iterator();
}
 
public interface Collection extends Iterable {
Iterator iterator();
}
 
public interface List extends Collection {
Iterator iterator();
}
 
public class ArrayList implements List {
public Iterator iterator() {
return new Itr();
}
 
private class Itr implements Iterator {
public boolean hasNext() {}
public Object next(){}
}
}
 
 
Collection c = new ArrayList();
c.add("hello");
c.add("world");
c.add("java");
Iterator it = c.iterator(); //new Itr();
while(it.hasNext()) {
String s = (String)it.next();
System.out.println(s);
}
================================================================================
package cn.itcast_04;
 
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
 
/*
* 需求:存储字符串并遍历。
*
* 分析:
* A:创建集合对象
* B:创建字符串对象
* C:把字符串对象添加到集合中
* D:遍历集合
*/
public class CollectionTest {
public static void main(String[] args) {
// 创建集合对象
Collection c = new ArrayList();
 
// 创建字符串对象
// 把字符串对象添加到集合中
c.add("林青霞");
c.add("风清扬");
c.add("刘意");
c.add("武鑫");
c.add("刘晓曲");
 
// 遍历集合
// 通过集合对象获取迭代器对象
Iterator it = c.iterator();
// 通过迭代器对象的hasNext()方法判断有没有元素
while (it.hasNext()) {
// 通过迭代器对象的next()方法获取元素
String s = (String) it.next();
System.out.println(s);
}
}
}
=====================================================================================package cn.itcast_04;
 
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
 
/*
* 需求:存储自定义对象并遍历Student(name,age)
*
* 分析:
* A:创建学生类
* B:创建集合对象
* C:创建学生对象
* D:把学生对象添加到集合对象中
* E:遍历集合
*/
public class CollectionTest2 {
public static void main(String[] args) {
// 创建集合对象
Collection c = new ArrayList();
 
// 创建学生对象
Student s1 = new Student("貂蝉", 25);
Student s2 = new Student("小乔", 16);
Student s3 = new Student("黄月英", 20);
Student s4 = new Student();
s4.setName("大乔");
s4.setAge(26);
 
// 把学生对象添加到集合对象中
c.add(s1);
c.add(s2);
c.add(s3);
c.add(s4);
c.add(new Student("孙尚香", 18)); // 匿名对象
 
// 遍历集合
Iterator it = c.iterator();
while (it.hasNext()) {
Student s = (Student) it.next();
System.out.println(s.getName() + "---" + s.getAge());
}
}
}
===========================================================================
package cn.itcast_01;
 
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
 
/*
* 需求:List集合存储字符串并遍历。
*/
public class ListDemo {
public static void main(String[] args) {
// 创建集合对象
List list = new ArrayList();
 
// 创建字符串并添加字符串
list.add("hello");
list.add("world");
list.add("java");
 
// 遍历集合
Iterator it = list.iterator();
while (it.hasNext()) {
String s = (String) it.next();
System.out.println(s);
}
}
}
===========================================================================
package cn.itcast_01;
 
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
 
/*
* List集合的特点:
* 有序(存储和取出的元素一致),可重复的。
*/
public class ListDemo2 {
public static void main(String[] args) {
// 创建集合对象
List list = new ArrayList();
 
// 存储元素
list.add("hello");
list.add("world");
list.add("java");
list.add("javaee");
list.add("android");
list.add("javaee");
list.add("android");
 
// 遍历集合
Iterator it = list.iterator();
while (it.hasNext()) {
String s = (String) it.next();
System.out.println(s);
}
}
}
===========================================================================package cn.itcast_01;
 
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
 
/*
* List集合的特点:
* 有序(存储和取出的元素一致),可重复的。
*/
public class ListDemo2 {
public static void main(String[] args) {
// 创建集合对象
List list = new ArrayList();
 
// 存储元素
list.add("hello");
list.add("world");
list.add("java");
list.add("javaee");
list.add("android");
list.add("javaee");
list.add("android");
 
// 遍历集合
Iterator it = list.iterator();
while (it.hasNext()) {
String s = (String) it.next();
System.out.println(s);
}
}
}
===========================================================================
package cn.itcast_03;
 
import java.util.ArrayList;
import java.util.List;
 
/*
* List集合的特有功能:
 
* A:添加功能
* void add(int index,Object element):在指定位置添加元素
* B:获取功能
* Object get(int index):获取指定位置的元素
* C:列表迭代器
* ListIterator listIterator():List集合特有的迭代器
 
* D:删除功能
* Object remove(int index):根据索引删除元素,返回被删除的元素
 
* E:修改功能
* Object set(int index,Object element):根据索引修改元素,返回被修饰的元素
*/
 
public class ListDemo {
public static void main(String[] args) {
// 创建集合对象
List list = new ArrayList();
 
// 添加元素
list.add("hello");
list.add("world");
list.add("java");
 
// void add(int index,Object element):在指定位置添加元素
// list.add(1, "android");//没有问题
// IndexOutOfBoundsException
// list.add(11, "javaee");//有问题
// list.add(3, "javaee"); //没有问题
// list.add(4, "javaee"); //有问题
 
// Object get(int index):获取指定位置的元素
// System.out.println("get:" + list.get(1));
// IndexOutOfBoundsException
// System.out.println("get:" + list.get(11));
 
// Object remove(int index):根据索引删除元素,返回被删除的元素
// System.out.println("remove:" + list.remove(1));
// IndexOutOfBoundsException
// System.out.println("remove:" + list.remove(11));
 
// Object set(int index,Object element):根据索引修改元素,返回被修饰的元素
System.out.println("set:" + list.set(1, "javaee"));
 
System.out.println("list:" + list);
}
}
 
=========================================================================
package cn.itcast_03;
 
import java.util.ArrayList;
import java.util.List;
 
/*
* List集合的特有遍历功能:
* size()和get()方法结合使用
*/
public class ListDemo2 {
public static void main(String[] args) {
// 创建集合对象
List list = new ArrayList();
 
// 添加元素
list.add("hello");
list.add("world");
list.add("java");
 
// Object get(int index):获取指定位置的元素
// System.out.println(list.get(0));
// System.out.println(list.get(1));
// System.out.println(list.get(2));
// IndexOutOfBoundsException
// System.out.println(list.get(3));
 
// 用循环改进
// for (int x = 0; x < 3; x++) {
// System.out.println(list.get(x));
// }
// 如果元素过多,数起来就比较麻烦,所以我们使用集合的一个长度功能:size()
// 最终的遍历方式就是:size()和get()
for (int x = 0; x < list.size(); x++) {
// System.out.println(list.get(x));
 
String s = (String) list.get(x);
System.out.println(s);
}
}
}
=======================================================
package cn.itcast_03;
 
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
 
/*
* 存储自定义对象并遍历,用普通for循环。(size()和get()结合)
*/
public class ListDemo3 {
public static void main(String[] args) {
// 创建集合对象
List list = new ArrayList();
 
// 创建学生对象
Student s1 = new Student("林黛玉", 18);
Student s2 = new Student("刘姥姥", 88);
Student s3 = new Student("王熙凤", 38);
 
// 把学生添加到集合中
list.add(s1);
list.add(s2);
list.add(s3);
 
// 遍历
// 迭代器遍历
Iterator it = list.iterator();
while (it.hasNext()) {
Student s = (Student) it.next();
System.out.println(s.getName() + "---" + s.getAge());
}
System.out.println("--------");
 
// 普通for循环
for (int x = 0; x < list.size(); x++) {
Student s = (Student) list.get(x);
System.out.println(s.getName() + "---" + s.getAge());
}
}
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package cn.itcast_04;
 
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
 
/*
* 列表迭代器:
* ListIterator listIterator():List集合特有的迭代器
* 该迭代器继承了Iterator迭代器,所以,就可以直接使用hasNext()和next()方法。
*
* 特有功能:
* Object previous():获取上一个元素
* boolean hasPrevious():判断是否有元素
*
* 注意:ListIterator可以实现逆向遍历,但是必须先正向遍历,才能逆向遍历,所以一般无意义,不使用。
*/
public class ListIteratorDemo {
public static void main(String[] args) {
// 创建List集合对象
List list = new ArrayList();
list.add("hello");
list.add("world");
list.add("java");
 
// ListIterator listIterator()
ListIterator lit = list.listIterator(); // 子类对象
// while (lit.hasNext()) {
// String s = (String) lit.next();
// System.out.println(s);
// }
// System.out.println("-----------------");
 
// System.out.println(lit.previous());
// System.out.println(lit.previous());
// System.out.println(lit.previous());
// NoSuchElementException
// System.out.println(lit.previous());
 
while (lit.hasPrevious()) {
String s = (String) lit.previous();
System.out.println(s);
}
System.out.println("-----------------");
 
// 迭代器
Iterator it = list.iterator();
while (it.hasNext()) {
String s = (String) it.next();
System.out.println(s);
}
System.out.println("-----------------");
 
}
}
==========================================================================
package cn.itcast_04;
 
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
 
/*
* 问题?
* 我有一个集合,如下,请问,我想判断里面有没有"world"这个元素,如果有,我就添加一个"javaee"元素,请写代码实现。
*
* ConcurrentModificationException:当方法检测到对象的并发修改,但不允许这种修改时,抛出此异常。
* 产生的原因:
* 迭代器是依赖于集合而存在的,在判断成功后,集合的中新添加了元素,而迭代器却不知道,所以就报错了,这个错叫并发修改异常。
* 其实这个问题描述的是:迭代器遍历元素的时候,通过集合是不能修改元素的。
* 如何解决呢?
* A:迭代器迭代元素,迭代器修改元素
* 元素是跟在刚才迭代的元素后面的。
* B:集合遍历元素,集合修改元素(普通for)
* 元素在最后添加的。
*/
public class ListIteratorDemo2 {
public static void main(String[] args) {
// 创建List集合对象
List list = new ArrayList();
// 添加元素
list.add("hello");
list.add("world");
list.add("java");
 
// 迭代器遍历
// Iterator it = list.iterator();
// while (it.hasNext()) {
// String s = (String) it.next();
// if ("world".equals(s)) {
// list.add("javaee");
// }
// }
 
// 方式1:迭代器迭代元素,迭代器修改元素
// 而Iterator迭代器却没有添加功能,所以我们使用其子接口ListIterator
// ListIterator lit = list.listIterator();
// while (lit.hasNext()) {
// String s = (String) lit.next();
// if ("world".equals(s)) {
// lit.add("javaee");
// }
// }
 
// 方式2:集合遍历元素,集合修改元素(普通for)
for (int x = 0; x < list.size(); x++) {
String s = (String) list.get(x);
if ("world".equals(s)) {
list.add("javaee");
}
}
 
System.out.println("list:" + list);
}
}
===============================================================================================
List:(面试题List的子类特点)
ArrayList:
底层数据结构是数组,查询快,增删慢。
线程不安全,效率高。
Vector:
底层数据结构是数组,查询快,增删慢。
线程安全,效率低。
LinkedList:
底层数据结构是链表,查询慢,增删快。
线程不安全,效率高。
 
List有三个儿子,我们到底使用谁呢?
看需求(情况)。
 
要安全吗?
要:Vector(即使要安全,也不用这个了,后面有替代的)
不要:ArrayList或者LinkedList
查询多:ArrayList
增删多:LinkedList
 
如果你什么都不懂,就用ArrayList。
==========================================================================
package cn.itcast_01;
 
/*
* 我有5个学生,请把这个5个学生的信息存储到数组中,并遍历数组,获取得到每一个学生信息。
* 学生:Student
* 成员变量:name,age
* 构造方法:无参,带参
* 成员方法:getXxx()/setXxx()
* 存储学生的数组?自己想想应该是什么样子的?
* 分析:
* A:创建学生类。
* B:创建学生数组(对象数组)。
* C:创建5个学生对象,并赋值。
* D:把C步骤的元素,放到数组中。
* E:遍历学生数组。
*/
public class ObjectArrayDemo {
public static void main(String[] args) {
// 创建学生数组(对象数组)。
Student[] students = new Student[5];
// for (int x = 0; x < students.length; x++) {
// System.out.println(students[x]);
// }
// System.out.println("---------------------");
 
// 创建5个学生对象,并赋值。
Student s1 = new Student("林青霞", 27);
Student s2 = new Student("风清扬", 30);
Student s3 = new Student("刘意", 30);
Student s4 = new Student("赵雅芝", 60);
Student s5 = new Student("王力宏", 35);
 
// 把C步骤的元素,放到数组中。
students[0] = s1;
students[1] = s2;
students[2] = s3;
students[3] = s4;
students[4] = s5;
 
// 看到很相似,就想循环改
// for (int x = 0; x < students.length; x++) {
// students[x] = s + "" + (x + 1);
// }
// 这个是有问题的
 
// 遍历
for (int x = 0; x < students.length; x++) {
//System.out.println(students[x]);
 
Student s = students[x];
System.out.println(s.getName()+"---"+s.getAge());
}
}
}
 
--ArrayList
package cn.itcast_01;
 
import java.util.ArrayList;
import java.util.Iterator;
 
/*
* ArrayList存储字符串并遍历
*
* 我们按照正常的写法来写这个程序, 结果确出错了。
* 为什么呢?
* 因为我们开始存储的时候,存储了String和Integer两种类型的数据。
* 而在遍历的时候,我们把它们都当作String类型处理的,做了转换,所以就报错了。
* 但是呢,它在编译期间却没有告诉我们。
* 所以,我就觉得这个设计的不好。
* 回想一下,我们的数组
* String[] strArray = new String[3];
* strArray[0] = "hello";
* strAr ray[1] = "world";
* strArray[2] = 10;
* 集合也模仿着数组的这种做法,在创建对象的时候明确元素的数据类型。这样就不会在有问题了。
* 而这种技术被称为:泛型。
*
* 泛型:是一种把类型明确的工作推迟到创建对象或者调用方法的时候才去明确的特殊的类型。参数化类型,把类型当作参数一样的传递。
* 格式:
* <数据类型>
* 此处的数据类型只能是引用类型。
* 好处:
* A:把运行时期的问题提前到了编译期间
* B:避免了强制类型转换
* C:优化了程序设计,解决了黄色警告线
*/
public class GenericDemo {
public static void main(String[] args) {
// 创建
ArrayList<String> array = new ArrayList<String>();
 
// 添加元素
array.add("hello");
array.add("world");
array.add("java");
// array.add(new Integer(100));
//array.add(10); // JDK5以后的自动装箱
// 等价于:array.add(Integer.valueOf(10));
 
// 遍历
Iterator<String> it = array.iterator();
while (it.hasNext()) {
// ClassCastException
// String s = (String) it.next();
String s = it.next();
System.out.println(s);
}
 
// 看下面这个代码
// String[] strArray = new String[3];
// strArray[0] = "hello";
// strArray[1] = "world";
// strArray[2] = 10;
}
}
===============================================
package cn.itcast_02;
 
import java.util.ArrayList;
import java.util.Iterator;
 
/*
* 泛型在哪些地方使用呢?
* 看API,如果类,接口,抽象类后面跟的有<E>就说要使用泛型。一般来说就是在集合中使用。
*/
 
 
public class ArrayListDemo {
public static void main(String[] args) {
// 用ArrayList存储字符串元素,并遍历。用泛型改进代码
ArrayList<String> array = new ArrayList<String>();
 
array.add("hello");
array.add("world");
array.add("java");
 
Iterator<String> it = array.iterator();
while (it.hasNext()) {
String s = it.next();
System.out.println(s);
}
System.out.println("-----------------");
 
for (int x = 0; x < array.size(); x++) {
String s = array.get(x);
System.out.println(s);
}
}
}
==================================================================
package cn.itcast_02;
 
import java.util.ArrayList;
import java.util.Iterator;
 
/*
* 需求:存储自定义对象并遍历。
*
* A:创建学生类
* B:创建集合对象
* C:创建元素对象
* D:把元素添加到集合
* E:遍历集合
*/
 
public class ArrayListDemo2 {
public static void main(String[] args) {
// 创建集合对象
// JDK7的新特性:泛型推断。
// ArrayList<Student> array = new ArrayList<>();
// 但是我不建议这样使用。
ArrayList<Student> array = new ArrayList<Student>();
 
// 创建元素对象
Student s1 = new Student("曹操", 40); // 后知后觉
Student s2 = new Student("蒋干", 30); // 不知不觉
Student s3 = new Student("诸葛亮", 26);// 先知先觉
 
// 添加元素
array.add(s1);
array.add(s2);
array.add(s3);
 
// 遍历
Iterator<Student> it = array.iterator();
while (it.hasNext()) {
Student s = it.next();
System.out.println(s.getName() + "---" + s.getAge());
}
System.out.println("------------------");
 
for (int x = 0; x < array.size(); x++) {
Student s = array.get(x);
System.out.println(s.getName() + "---" + s.getAge());
}
}
}
===========================================================================package cn.itcast_03;
 
/*
* 早期的时候,我们使用Object来代表任意的类型。
* 向上转型是没有任何问题的,但是在向下转型的时候其实隐含了类型转换的问题。
* 也就是说这样的程序其实并不是安全的。所以Java在JDK5后引入了泛型,提高程序的安全性。
*/
public class ObjectToolDemo {
public static void main(String[] args) {
ObjectTool ot = new ObjectTool();
 
// 正常使用
ot.setObj(new Integer(27));
Integer i = (Integer) ot.getObj();
System.out.println("年龄是:" + i);
 
ot.setObj(new String("林青霞"));
String s = (String) ot.getObj( );
System.out.println("姓名是:" + s);
 
System.out.println("---------");
ot.setObj(new Integer(30));
// ClassCastException
String ss = (String) ot.getObj();
System.out.println("姓名是:" + ss);
}
}
===========================================================================
package cn.itcast_04;
 
/*
* 泛型类的测试
*/
public class ObjectToolDemo {
public static void main(String[] args) {
// ObjectTool ot = new ObjectTool();
//
// ot.setObj(new String("风清扬"));
// String s = (String) ot.getObj();
// System.out.println("姓名是:" + s);
//
// ot.setObj(new Integer(30));
// Integer i = (Integer) ot.getObj();
// System.out.println("年龄是:" + i);
 
// ot.setObj(new String("林青霞"));
// // ClassCastException
// Integer ii = (Integer) ot.getObj();
// System.out.println("姓名是:" + ii);
 
System.out.println("-------------");
 
ObjectTool<String> ot = new ObjectTool<String>();
// ot.setObj(new Integer(27)); //这个时候编译期间就过不去
ot.setObj(new String("林青霞"));
String s = ot.getObj();
System.out.println("姓名是:" + s);
 
ObjectTool<Integer> ot2 = new ObjectTool<Integer>();
// ot2.setObj(new String("风清扬"));//这个时候编译期间就过不去
ot2.setObj(new Integer(27));
Integer i = ot2.getObj();
System.out.println("年龄是:" + i);
}
}
===========================================================
package cn.itcast_05;
 
public class ObjectToolDemo {
public static void main(String[] args) {
// ObjectTool ot = new ObjectTool();
// ot.show("hello");
// ot.show(100);
// ot.show(true);
 
// ObjectTool<String> ot = new ObjectTool<String>();
// ot.show("hello");
//
// ObjectTool<Integer> ot2 = new ObjectTool<Integer>();
// ot2.show(100);
//
// ObjectTool<Boolean> ot3 = new ObjectTool<Boolean>();
// ot3.show(true);
 
// 如果还听得懂,那就说明泛型类是没有问题的
// 但是呢,谁说了我的方法一定要和类的类型的一致呢?
// 我要是类上没有泛型的话,方法还能不能接收任意类型的参数了呢?
 
// 定义泛型方法后
ObjectTool ot = new ObjectTool();
ot.show("hello");
ot.show(100);
ot.show(true);
}
}
 
package cn.itcast_05;
 
//public class ObjectTool<T> {
// // public void show(String s) {
// // System.out.println(s);
// // }
// //
// // public void show(Integer i) {
// // System.out.println(i);
// // }
// //
// // public void show(Boolean b) {
// // System.out.println(b);
// // }
//
// public void show(T t) {
// System.out.println(t);
// }
// }
 
/*
* 泛型方法:把泛型定义在方法上
*/
public class ObjectTool {
public <T> void show(T t) {
System.out.println(t);
}
}
===========================================================================package cn.itcast_06;
 
public class InterDemo {
public static void main(String[] args) {
// 第一种情况的测试
// Inter<String> i = new InterImpl();
// i.show("hello");
 
// // 第二种情况的测试
Inter<String> i = new InterImpl<String>();
i.show("hello");
 
Inter<Integer> ii = new InterImpl<Integer>();
ii.show(100);
}
}
 
package cn.itcast_06;
 
//实现类在实现接口的时候
//第一种情况:已经知道该是什么类型的了
 
//public class InterImpl implements Inter<String> {
//
// @Override
// public void show(String t) {
// System.out.println(t);
// }
// }
 
//第二种情况:还不知道是什么类型的
public class InterImpl<T> implements Inter<T> {
 
@Override
public void show(T t) {
System.out.println(t);
}
}
 
package cn.itcast_06;
 
/*
* 泛型接口:把泛型定义在接口上
*/
public interface Inter<T> {
public abstract void show(T t);
}
 
package cn.itcast_06;
 
========================================================
package cn.itcast_07;
 
import java.util.ArrayList;
import java.util.Collection;
 
/*
* 泛型高级(通配符)
* ?:任意类型,如果没有明确,那么就是Object以及任意的Java类了
* ? extends E:向下限定,E及其子类
* ? super E:向上限定,E极其父类
*/
public class GenericDemo {
public static void main(String[] args) {
// 泛型如果明确的写的时候,前后必须一致
Collection<Object> c1 = new ArrayList<Object>();
// Collection<Object> c2 = new ArrayList<Animal>();
// Collection<Object> c3 = new ArrayList<Dog>();
// Collection<Object> c4 = new ArrayList<Cat>();
 
// ?表示任意的类型都是可以的
Collection<?> c5 = new ArrayList<Object>();
Collection<?> c6 = new ArrayList<Animal>();
Collection<?> c7 = new ArrayList<Dog>();
Collection<?> c8 = new ArrayList<Cat>();
 
// ? extends E:向下限定,E及其子类
// Collection<? extends Animal> c9 = new ArrayList<Object>();
Collection<? extends Animal> c10 = new ArrayList<Animal>();
Collection<? extends Animal> c11 = new ArrayList<Dog>();
Collection<? extends Animal> c12 = new ArrayList<Cat>();
 
// ? super E:向上限定,E极其父类
Collection<? super Animal> c13 = new ArrayList<Object>();
Collection<? super Animal> c14 = new ArrayList<Animal>();
// Collection<? super Animal> c15 = new ArrayList<Dog>();
// Collection<? super Animal> c16 = new ArrayList<Cat>();
}
}
 
class Animal {
}
 
class Dog extends Animal {
}
 
class Cat extends Animal {
}
===========================================================================
package cn.itcast_01;
 
import java.util.ArrayList;
import java.util.Iterator;
 
/*
* ArrayList存储字符串并遍历。要求加入泛型,并用增强for遍历。
* A:迭代器
* B:普通for
* C:增强for
*/
public class ArrayListDemo {
public static void main(String[] args) {
// 创建集合对象
ArrayList<String> array = new ArrayList<String>();
 
// 创建并添加元素
array.add("hello");
array.add("world");
array.add("java");
 
// 遍历集合
// 迭代器
Iterator<String> it = array.iterator();
while (it.hasNext()) {
String s = it.next();
System.out.println(s);
}
System.out.println("------------------");
 
// 普通for
for (int x = 0; x < array.size(); x++) {
String s = array.get(x);
System.out.println(s);
}
System.out.println("------------------");
 
// 增强for
for (String s : array) {
System.out.println(s);
}
}
}
 
========================================================
package cn.itcast_01;
 
import java.util.ArrayList;
import java.util.Iterator;
 
/*
* 需求:ArrayList存储自定义对象并遍历。要求加入泛型,并用增强for遍历。
* A:迭代器
* B:普通for
* C:增强for
*
* LinkedList,Vector,Colleciton,List等存储我还讲吗?不讲解了,但是要求你们练习。
*
* 增强for是用来替迭代器。
*/
public class ArrayListDemo2 {
public static void main(String[] args) {
// 创建集合对象
ArrayList<Student> array = new ArrayList<Student>();
 
// 创建学生对象
Student s1 = new Student("林青霞", 27);
Student s2 = new Student("貂蝉", 22);
Student s3 = new Student("杨玉环", 24);
Student s4 = new Student("西施", 21);
Student s5 = new Student("王昭君", 23);
 
// 把学生对象添加到集合中
array.add(s1);
array.add(s2);
array.add(s3);
array.add(s4);
array.add(s5);
 
// 迭代器
Iterator<Student> it = array.iterator();
while (it.hasNext()) {
Student s = it.next();
System.out.println(s.getName() + "---" + s.getAge());
}
System.out.println("---------------");
 
// 普通for
for (int x = 0; x < array.size(); x++) {
Student s = array.get(x);
System.out.println(s.getName() + "---" + s.getAge());
}
System.out.println("---------------");
 
// 增强for
for (Student s : array) {
System.out.println(s.getName() + "---" + s.getAge());
}
}
}
===========================================================================package cn.itcast_01;
 
import java.util.ArrayList;
import java.util.List;
 
/*
* JDK5的新特性:自动拆装箱,泛型,增强for,静态导入,可变参数,枚举
*
* 增强for:是for循环的一种。
*
* 格式:
* for(元素数据类型 变量 : 数组或者Collection集合) {
* 使用变量即可,该变量就是元素
* }
*
* 好处:简化了数组和集合的遍历。
*
* 弊端: 增强for的目标不能为null。
* 如何解决呢?对增强for的目标先进行不为null的判断,然后在使用。
*/
public class ForDemo {
public static void main(String[] args) {
// 定义一个int数组
int[] arr = { 1, 2, 3, 4, 5 };
for (int x = 0; x < arr.length; x++) {
System.out.println(arr[x]);
}
System.out.println("---------------");
// 增强for
for (int x : arr) {
System.out.println(x);
}
System.out.println("---------------");
// 定义一个字符串数组
String[] strArray = { "林青霞", "风清扬", "东方不败", "刘意" };
// 增强for
for (String s : strArray) {
System.out.println(s);
}
System.out.println("---------------");
// 定义一个集合
ArrayList<String> array = new ArrayList<String>();
array.add("hello");
array.add("world");
array.add("java");
// 增强for
for (String s : array) {
System.out.println(s);
}
System.out.println("---------------");
 
List<String> list = null;
// NullPointerException
// 这个s是我们从list里面获取出来的,在获取前,它肯定还好做一个判断
// 说白了,这就是迭代器的功能
if (list != null) {
for (String s : list) {
System.out.println(s);
}
}
 
// 增强for其实是用来替代迭代器的
//ConcurrentModificationException
// for (String s : array) {
// if ("world".equals(s)) {
// array.add("javaee");
// }
// }
// System.out.println("array:" + array);
}
}
===================================================================
package cn.itcast_02;
 
/*
* 静态导入:
* 格式:import static 包名….类名.方法名;
* 可以直接导入到方法的级别
*
* 静态导入的注意事项:
* A:方法必须是静态的
* B:如果有多个同名的静态方法,容易不知道使用谁?这个时候要使用,必须加前缀。由此可见,意义不大,所以一般不用,但是要能看懂。
*/
import static java.lang.Math.abs;
import static java.lang.Math.pow;
import static java.lang.Math.max;
 
//错误,不是静态
//import static java.util.ArrayList.add;
 
public class StaticImportDemo {
public static void main(String[] args) {
// System.out.println(java.lang.Math.abs(-100));
// System.out.println(java.lang.Math.pow(2, 3));
// System.out.println(java.lang.Math.max(20, 30));
// 太复杂,我们就引入到import
 
// System.out.println(Math.abs(-100));
// System.out.println(Math.pow(2, 3));
// System.out.println(Math.max(20, 30));
// 太复杂,有更简单
 
// System.out.println(abs(-100));
System.out.println(java.lang.Math.abs(-100));
System.out.println(pow(2, 3));
System.out.println(max(20, 30));
}
 
public static void abs(String s){
System.out.println(s);
}
}
 
=====================================================
package cn.itcast_03;
 
/*
* 可变参数:定义方法的时候不知道该定义多少个参数
* 格式:
* 修饰符 返回值类型 方法名(数据类型… 变量名){
*
* }
*
* 注意:
* 这里的变量其实是一个数组
* 如果一个方法有可变参数,并且有多个参数,那么,可变参数肯定是最后一个
*/
public class ArgsDemo {
public static void main(String[] args) {
// 2个数据求和
int a = 10;
int b = 20;
int result = sum(a, b);
System.out.println("result:" + result);
 
// 3个数据的求和
int c = 30;
result = sum(a, b, c);
System.out.println("result:" + result);
 
// 4个数据的求和
int d = 30;
result = sum(a, b, c, d);
System.out.println("result:" + result);
 
// 需求:我要写一个求和的功能,到底是几个数据求和呢,我不太清楚,但是我知道在调用的时候我肯定就知道了
// 为了解决这个问题,Java就提供了一个东西:可变参数
result = sum(a, b, c, d, 40);
System.out.println("result:" + result);
 
result = sum(a, b, c, d, 40, 50);
System.out.println("result:" + result);
}
 
public static int sum(int... a) {
// System.out.println(a);
//return 0;
 
int s = 0;
 
for(int x : a){
s +=x;
}
 
return s;
}
 
// public static int sum(int a, int b, int c, int d) {
// return a + b + c + d;
// }
//
// public static int sum(int a, int b, int c) {
// return a + b + c;
// }
//
// public static int sum(int a, int b) {
// return a + b;
// }
}
===========================================================================package cn.itcast_03;
 
import java.util.Arrays;
import java.util.List;
 
/*
* public static <T> List<T> asList(T... a):把数组转成集合
*
* 注意事项:
* 虽然可以把数组转成集合,但是集合的长度不能改变。
*/
public class ArraysDemo {
public static void main(String[] args) {
// 定义一个数组
// String[] strArray = { "hello", "world", "java" };
// List<String> list = Arrays.asList(strArray);
 
List<String> list = Arrays.asList("hello", "world", "java");
// UnsupportedOperationException
// list.add("javaee");
// UnsupportedOperationException
// list.remove(1);
list.set(1, "javaee");
 
for (String s : list) {
System.out.println(s);
}
}
}
==================================================================
package cn.itcast_01;
 
import java.util.ArrayList;
import java.util.Iterator;
 
/*
* List的子类特点:
* ArrayList:
* 底层数据结构是数组,查询快,增删慢
* 线程不安全,效率高
* Vector:
* 底层数据结构是数组,查询快,增删慢
* 线程安全,效率低
* LinkedList:
* 底层数据结构是链表,查询慢,增删快
* 线程不安全,效率高
*
* 案例:
* 使用List的任何子类存储字符串或者存储自定义对象并遍历。
*
* ArrayList的使用。
* 存储字符串并遍历
*/
public class ArrayListDemo {
public static void main(String[] args) {
// 创建集合对象
ArrayList array = new ArrayList();
 
// 创建元素对象,并添加元素
array.add("hello");
array.add("world");
array.add("java");
 
// 遍历
Iterator it = array.iterator();
while (it.hasNext()) {
String s = (String) it.next();
System.out.println(s);
}
 
System.out.println("-----------");
 
for (int x = 0; x < array.size(); x++) {
String s = (String) array.get(x);
System.out.println(s);
}
}
}
======================================================================
package cn.itcast_01;
 
import java.util.ArrayList;
import java.util.Iterator;
 
/*
* ArrayList存储自定义对象并遍历
*/
public class ArrayListDemo2 {
public static void main(String[] args) {
// 创建集合对象
ArrayList array = new ArrayList();
 
// 创建学生对象
Student s1 = new Student("武松", 30);
Student s2 = new Student("鲁智深", 40);
Student s3 = new Student("林冲", 36);
Student s4 = new Student("杨志", 38);
 
// 添加元素
array.add(s1);
array.add(s2);
array.add(s3);
array.add(s4);
 
// 遍历
Iterator it = array.iterator();
while (it.hasNext()) {
Student s = (Student) it.next();
System.out.println(s.getName() + "---" + s.getAge());
}
 
System.out.println("----------------");
 
for (int x = 0; x < array.size(); x++) {
// ClassCastException 注意,千万要搞清楚类型
// String s = (String) array.get(x);
// System.out.println(s);
 
Student s = (Student) array.get(x);
System.out.println(s.getName() + "---" + s.getAge());
}
}
}
=============================================================
package cn.itcast_02;
 
import java.util.Enumeration;
import java.util.Vector;
 
/*
* Vector的特有功能:
* 1:添加功能
* public void addElement(Object obj) -- add()
* 2:获取功能
* public Object elementAt(int index) -- get()
* public Enumeration elements() -- Iterator iterator()
* boolean hasMoreElements() hasNext()
* Object nextElement() next()
*
* JDK升级的原因:
* A:安全
* B:效率
* C:简化书写
*/
public class VectorDemo {
public static void main(String[] args) {
// 创建集合对象
Vector v = new Vector();
 
// 添加功能
v.addElement("hello");
v.addElement("world");
v.addElement("java");
 
// 遍历
for (int x = 0; x < v.size(); x++) {
String s = (String) v.elementAt(x);
System.out.println(s);
}
 
System.out.println("------------------");
 
Enumeration en = v.elements(); // 返回的是实现类的对象
while (en.hasMoreElements()) {
String s = (String) en.nextElement();
System.out.println(s);
}
}
}
======================================================================================================
package cn.itcast_03;
 
import java.util.LinkedList;
 
/*
* LinkedList的特有功能:
* A:添加功能
* public void addFirst(Object e)
* public void addLast(Object e)
* B:获取功能
* public Object getFirst()
* public Obejct getLast()
* C:删除功能
* public Object removeFirst()
* public Object removeLast()
*/
public class LinkedListDemo {
public static void main(String[] args) {
// 创建集合对象
LinkedList link = new LinkedList();
 
// 添加元素
link.add("hello");
link.add("world");
link.add("java");
 
// public void addFirst(Object e)
// link.addFirst("javaee");
// public void addLast(Object e)
// link.addLast("android");
 
// public Object getFirst()
// System.out.println("getFirst:" + link.getFirst());
// public Obejct getLast()
// System.out.println("getLast:" + link.getLast());
 
// public Object removeFirst()
System.out.println("removeFirst:" + link.removeFirst());
// public Object removeLast()
System.out.println("removeLast:" + link.removeLast());
 
// 输出对象名
System.out.println("link:" + link);
}
}
=====================================================================
package cn.itcast_04;
 
import java.util.ArrayList;
import java.util.Iterator;
 
/*
* ArrayList去除集合中字符串的重复值(字符串的内容相同)
*
* 分析:
* A:创建集合对象
* B:添加多个字符串元素(包含内容相同的)
* C:创建新集合
* D:遍历旧集合,获取得到每一个元素
* E:拿这个元素到新集合去找,看有没有
* 有:不搭理它
* 没有:就添加到新集合
* F:遍历新集合
*/
public class ArrayListDemo {
public static void main(String[] args) {
// 创建集合对象
ArrayList array = new ArrayList();
 
// 添加多个字符串元素(包含内容相同的)
array.add("hello");
array.add("world");
array.add("java");
array.add("world");
array.add("java");
array.add("world");
array.add("world");
array.add("world");
array.add("world");
array.add("java");
array.add("world");
 
// 创建新集合
ArrayList newArray = new ArrayList();
 
// 遍历旧集合,获取得到每一个元素
Iterator it = array.iterator();
while (it.hasNext()) {
String s = (String) it.next();
 
// 拿这个元素到新集合去找,看有没有
if (!newArray.contains(s)) {
newArray.add(s);
}
}
 
// 遍历新集合
for (int x = 0; x < newArray.size(); x++) {
String s = (String) newArray.get(x);
System.out.println(s);
}
}
}
============================================================================================================================================
package cn.itcast_04;
 
import java.util.ArrayList;
import java.util.Iterator;
 
/*
* 需求:ArrayList去除集合中字符串的重复值(字符串的内容相同)
* 要求:不能创建新的集合,就在以前的集合上做。
*/
public class ArrayListDemo2 {
public static void main(String[] args) {
// 创建集合对象
ArrayList array = new ArrayList();
 
// 添加多个字符串元素(包含内容相同的)
array.add("hello");
array.add("world");
array.add("java");
array.add("world");
array.add("java");
array.add("world");
array.add("world");
array.add("world");
array.add("world");
array.add("java");
array.add("world");
 
// 由选择排序思想引入,我们就可以通过这种思想做这个题目
// 拿0索引的依次和后面的比较,有就把后的干掉
// 同理,拿1索引...
for (int x = 0; x < array.size() - 1; x++) {
for (int y = x + 1; y < array.size(); y++) {
if (array.get(x).equals(array.get(y))) {
array.remove(y);
y--;
}
}
}
 
// 遍历集合
Iterator it = array.iterator();
while (it.hasNext()) {
String s = (String) it.next();
System.out.println(s);
}
}
}
===========================================================
package cn.itcast_04;
 
import java.util.ArrayList;
import java.util.Iterator;
 
/*
* 需求:去除集合中自定义对象的重复值(对象的成员变量值都相同)
*
* 我们按照和字符串一样的操作,发现出问题了。
* 为什么呢?
* 我们必须思考哪里会出问题?
* 通过简单的分析,我们知道问题出现在了判断上。
* 而这个判断功能是集合自己提供的,所以我们如果想很清楚的知道它是如何判断的,就应该去看源码。
* contains()方法的底层依赖的是equals()方法。
* 而我们的学生类中没有equals()方法,这个时候,默认使用的是它父亲Object的equals()方法
* Object()的equals()默认比较的是地址值,所以,它们进去了。因为new的东西,地址值都不同。
* 按照我们自己的需求,比较成员变量的值,重写equals()即可。
* 自动生成即可。
*/
public class ArrayListDemo3 {
public static void main(String[] args) {
// 创建集合对象
ArrayList array = new ArrayList();
 
// 创建学生对象
Student s1 = new Student("林青霞", 27);
Student s2 = new Student("林志玲", 40);
Student s3 = new Student("凤姐", 35);
Student s4 = new Student("芙蓉姐姐", 18);
Student s5 = new Student("翠花", 16);
Student s6 = new Student("林青霞", 27);
Student s7 = new Student("林青霞", 18);
 
// 添加元素
array.add(s1);
array.add(s2);
array.add(s3);
array.add(s4);
array.add(s5);
array.add(s6);
array.add(s7);
 
// 创建新集合
ArrayList newArray = new ArrayList();
 
// 遍历旧集合,获取得到每一个元素
Iterator it = array.iterator();
while (it.hasNext()) {
Student s = (Student) it.next();
 
// 拿这个元素到新集合去找,看有没有
if (!newArray.contains(s)) {
newArray.add(s);
}
}
 
// 遍历新集合
for (int x = 0; x < newArray.size(); x++) {
Student s = (Student) newArray.get(x);
System.out.println(s.getName() + "---" + s.getAge());
}
}
}
 
 
package cn.itcast_04;
 
public class Student {
private String name;
private int age;
 
public Student() {
super();
}
 
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
 
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;
}
 
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
 
}
===========================================================================
package cn.itcast_05;
 
/*
* MyStack的测试
*/
public class MyStackDemo {
public static void main(String[] args) {
// 创建集合对象
MyStack ms = new MyStack();
 
// 添加元素
ms.add("hello");
ms.add("world");
ms.add("java");
 
// System.out.println(ms.get());
// System.out.println(ms.get());
// System.out.println(ms.get());
// NoSuchElementException
// System.out.println(ms.get());
 
while(!ms.isEmpty()){
System.out.println(ms.get());
}
}
}
 
package cn.itcast_05;
 
import java.util.LinkedList;
 
/**
* 自定义的栈集合
*
* @author 风清扬
* @version V1.0
*/
public class MyStack {
private LinkedList link;
 
public MyStack() {
link = new LinkedList();
}
 
public void add(Object obj) {
link.addFirst(obj);
}
 
public Object get() {
// return link.getFirst();
return link.removeFirst();
}
 
public boolean isEmpty() {
return link.isEmpty();
}
}
 
=========================
package cn.itcast_05;
 
import java.util.Iterator;
import java.util.LinkedList;
 
/*
*请用LinkedList模拟栈数据结构的集合,并测试
*题目的意思是:
* 你自己的定义一个集合类,在这个集合类内部可以使用LinkedList模拟。
*/
public class LinkedListDemo {
public static void main(String[] args) {
// A: LinkedList的特有添加功能addFirst()
// B:栈的特点先进后出
// 创建集合对象
// LinkedList link = new LinkedList();
//
// // 添加元素
// link.addFirst("hello");
// link.addFirst("world");
// link.addFirst("java");
//
// // 遍历
// Iterator it = link.iterator();
// while (it.hasNext()) {
// String s = (String) it.next();
// System.out.println(s);
// }
 
//为什么呢?
}
}
 
===========================================================================package cn.itcast_01;
 
import java.util.ArrayList;
 
/*
* 集合的嵌套遍历
* 需求:
* 我们班有学生,每一个学生是不是一个对象。所以我们可以使用一个集合表示我们班级的学生。ArrayList<Student>
* 但是呢,我们旁边是不是还有班级,每个班级是不是也是一个ArrayList<Student>。
* 而我现在有多个ArrayList<Student>。也要用集合存储,怎么办呢?
* 就是这个样子的:ArrayList<ArrayList<Student>>
*/
public class ArrayListDemo {
public static void main(String[] args) {
// 创建大集合
ArrayList<ArrayList<Student>> bigArrayList = new ArrayList<ArrayList<Student>>();
 
// 创建第一个班级的学生集合
ArrayList<Student> firstArrayList = new ArrayList<Student>();
// 创建学生
Student s1 = new Student("唐僧", 30);
Student s2 = new Student("孙悟空", 29);
Student s3 = new Student("猪八戒", 28);
Student s4 = new Student("沙僧", 27);
Student s5 = new Student("白龙马", 26);
// 学生进班
firstArrayList.add(s1);
firstArrayList.add(s2);
firstArrayList.add(s3);
firstArrayList.add(s4);
firstArrayList.add(s5);
// 把第一个班级存储到学生系统中
bigArrayList.add(firstArrayList);
 
// 创建第二个班级的学生集合
ArrayList<Student> secondArrayList = new ArrayList<Student>();
// 创建学生
Student s11 = new Student("诸葛亮", 30);
Student s22 = new Student("司马懿", 28);
Student s33 = new Student("周瑜", 26);
// 学生进班
secondArrayList.add(s11);
secondArrayList.add(s22);
secondArrayList.add(s33);
// 把第二个班级存储到学生系统中
bigArrayList.add(secondArrayList);
 
// 创建第三个班级的学生集合
ArrayList<Student> thirdArrayList = new ArrayList<Student>();
// 创建学生
Student s111 = new Student("宋江", 40);
Student s222 = new Student("吴用", 35);
Student s333 = new Student("高俅", 30);
Student s444 = new Student("李师师", 22);
// 学生进班
thirdArrayList.add(s111);
thirdArrayList.add(s222);
thirdArrayList.add(s333);
thirdArrayList.add(s444);
// 把第三个班级存储到学生系统中
bigArrayList.add(thirdArrayList);
 
// 遍历集合
for (ayArrList<Student> array : bigArrayList) {
for (Student s : array) {
System.out.println(s.getName() + "---" + s.getAge());
}
}
}
}
 
 
 
package cn.itcast_01;
 
public class Student {
private String name;
private int age;
 
public Student() {
super();
}
 
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
 
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;
}
 
}
 
===============================================================================package cn.itcast_02;
 
import java.util.ArrayList;
import java.util.Random;
 
/*
* 获取10个1-20之间的随机数,要求不能重复
*
* 用数组实现,但是数组的长度是固定的,长度不好确定。
* 所以我们使用集合实现。
*
* 分析:
* A:创建产生随机数的对象
* B:创建一个存储随机数的集合。
* C:定义一个统计变量。从0开始。
* D:判断统计遍历是否小于10
* 是:先产生一个随机数,判断该随机数在集合中是否存在。
* 如果不存在:就添加,统计变量++。
* 如果存在:就不搭理它。
* 否:不搭理它
* E:遍历集合
*/
public class RandomDemo {
public static void main(String[] args) {
// 创建产生随机数的对象
Random r = new Random();
 
// 创建一个存储随机数的集合。
ArrayList<Integer> array = new ArrayList<Integer>();
 
// 定义一个统计变量。从0开始。
int count = 0;
 
// 判断统计遍历是否小于10
while (count < 10) {
//先产生一个随机数
int number = r.nextInt(20) + 1;
 
//判断该随机数在集合中是否存在。
if(!array.contains(number)){
//如果不存在:就添加,统计变量++。
array.add(number);
count++;
}
}
 
//遍历集合
for(Integer i : array){
System.out.println(i);
}
}
}
===========================================================================
package cn.itcast_03;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
 
/*
* 键盘录入多个数据,以0结束,要求在控制台输出这多个数据中的最大值
*
* 分析:
* A:创建键盘录入数据对象
* B:键盘录入多个数据,我们不知道多少个,所以用集合存储
* C:以0结束,这个简单,只要键盘录入的数据是0,我就不继续录入数据了
* D:把集合转成数组
* E:对数组排序
* F:获取该数组中的最大索引的值
*/
public class ArrayListDemo {
public static void main(String[] args) {
// 创建键盘录入数据对象
Scanner sc = new Scanner(System.in);
 
// 键盘录入多个数据,我们不知道多少个,所以用集合存储
ArrayList<Integer> array = new ArrayList<Integer>();
 
// 以0结束,这个简单,只要键盘录入的数据是0,我就不继续录入数据了
while (true) {
System.out.println("请输入数据:");
int number = sc.nextInt();
if (number != 0) {
array.add(number);
} else {
break;
}
}
 
// 把集合转成数组
// public <T> T[] toArray(T[] a)
Integer[] i = new Integer[array.size()];
// Integer[] ii = array.toArray(i);
array.toArray(i);
// System.out.println(i);
// System.out.println(ii);
 
// 对数组排序
// public static void sort(Object[] a)
Arrays.sort(i);
 
// 获取该数组中的最大索引的值
System.out.println("数组是:" + arrayToString(i) + "最大值是:"
+ i[i.length - 1]);
}
 
public static String arrayToString(Integer[] i) {
StringBuilder sb = new StringBuilder();
 
sb.append("[");
for (int x = 0; x < i.length; x++) {
if (x == i.length - 1) {
sb.append(i[x]);
} else {
sb.append(i[x]).append(", ");
}
}
sb.append("]");
 
return sb.toString();
}
}
====================================================================================================================
 
二.
package cn.itcast.test;
 
import java.util.Scanner;
 
import cn.itcast.dao.UserDao;
import cn.itcast.dao.impl.UserDaoImpl;
import cn.itcast.game.GuessNumber;
import cn.itcast.pojo.User;
 
/**
* 用户测试类
*
* @author 风清扬
* @version V1.0
*
* 新增加了两个小问题 A:多个对象共享同一个成员变量,用静态
* B:循环里面如果有switch,并且在switch里面有break,那么结束的不是循环,而是switch语句
*
*/
public class UserTest {
public static void main(String[] args) {
// 为了能够回来
while (true) {
// 欢迎界面,给出选择项
Syste m.out.println("--------------欢迎光临--------------");
System.out.println("1 登录");
System.out.println("2 注册");
System.out.println("3 退出");
System.out.println("请输入你的选择:");
// 键盘录入选择,根据选择做不同的操作
Scanner sc = new Scanner(System.in);
// 为了后面的录入信息的方便,我所有的数据录入全部用字符接收
String choiceString = sc.nextLine();
 
// switch语句的多个地方要使用,我就定义到外面
UserDao ud = new UserDaoImpl();
 
// 经过简单的思考,我选择了switch
switch (choiceString) {
case "1":
// 登录界面,请输入用户名和密码
System.out.println("--------------登录界面--------------");
System.out.println("请输入用户名:");
String username = sc.nextLine();
System.out.println("请输入密码:");
String password = sc.nextLine();
 
// 调用登录功能
// UserDao ud = new UserDaomImpl();
 
boolean flag = ud.isLogin(username, password);
if (flag) {
System.out.println("登录成功,可以开始玩游戏了");
 
System.out.println("你玩吗?y/n");
while (true) {
String resultString = sc.nextLine();
if (resultString.equalsIgnoreCase("y")) {
// 玩游戏
GuessNumber.start();
System.out.println("你还玩吗?y/n");
} else {
break;
}
}
System.out.println("谢谢使用,欢迎下次再来");
System.exit(0);
// break; //这里写break,结束的是switch
} else {
System.out.println("用户名或者密码有误,登录失败");
}
break;
case "2":
// 欢迎界面,请输入用户名和密码
System.out.println("--------------注册界面--------------");
System.out.println("请输入用户名:");
String newUsername = sc.nextLine();
System.out.println("请输入密码:");
String newPassword = sc.nextLine();
 
// 把用户名和密码封装到一个对象中
User user = new User();
user.setUsername(newUsername);
user.setPassword(newPassword);
 
// 调用注册功能
// 多态
// UserDao ud = new UserDaoImpl();
// 具体类使用
// UserDaoImpl udi = new UserDaoImpl();
 
ud.regist(user);
System.out.println("注册成功");
break;
case "3":
default:
System.out.println("谢谢使用,欢迎下次再来");
System.exit(0);
break;
}
}
}
}
 
===========================================================================package cn.itcast.pojo;
 
/**
* 这是用户基本描述类
*
* @author 风清扬
* @version V1.0
*
*/
public class User {
// 用户名
private String username;
// 密码
private String password;
 
public User() {
}
 
public String getUsername() {
return username;
}
 
public void setUsername(String username) {
this.username = username;
}
 
public String getPassword() {
return password;
}
 
public void setPassword(String password) {
this.password = password;
}
}
===========================================================================package cn.itcast.dao;
 
import cn.itcast.pojo.User;
 
/**
* 这是针对用户进行操作的接口
*
* @author 风清扬
* @version V1.0
*
*/
public interface UserDao {
/**
* 这是用户登录功能
*
* @param username
* 用户名
* @param password
* 密码
* @return 返回登录是否成功
*/
public abstract boolean isLogin(String username, String password);
 
/**
* 这是用户注册功能
*
* @param user
* 要注册的用户信息
*/
public abstract void regist(User user);
}
==============================================================
package cn.itcast.dao.impl;
 
import java.util.ArrayList;
 
import cn.itcast.dao.UserDao;
import cn.itcast.pojo.User;
 
/**
* 这是用户操作的具体实现类(集合版)
*
* @author 风清扬
* @version V1.0
*
*/
public class UserDaoImpl implements UserDao {
// 为了让多个方法能够使用同一个集合,就把集合定义为成员变量
// 为了不让外人看到,用private
// 为了让多个对象共享同一个成员变量,用static
private static ArrayList<User> array = new ArrayList<User>();
 
@Override
public boolean isLogin(String username, String password) {
// 遍历集合,获取每一个用户,并判断该用户的用户名和密码是否和传递过来的匹配
boolean flag = false;
 
for (User u : array) {
if (u.getUsername().equals(username)
&& u.getPassword().equals(password)) {
flag = true;
break;
}
}
 
return flag;
}
 
@Override
public void regist(User user) {
// 把用户信息存储集合
// ArrayList<User> array = new ArrayList<User>();
array.add(user);
}
}
===========================================================================package cn.itcast.game;
 
import java.util.Scanner;
 
/**
* 这是猜数字小游戏
*
* @author 风清扬
* @version V1.0
*
*/
public class GuessNumber {
private GuessNumber() {
}
 
public static void start() {
// 产生一个随机数
int number = (int) (Math.random() * 100) + 1;
 
// 定义一个统计变量
int count = 0;
 
while (true) {
// 键盘录入一个数据
Scanner sc = new Scanner(System.in);
System.out.println("请输入数据(1-100):");
int guessNumber = sc.nextInt();
 
count++;
 
// 判断
if (guessNumber > number) {
System.out.println("你猜的数据" + guessNumber + "大了");
} else if (guessNumber < number) {
System.out.println("你猜的数据" + guessNumber + "小了");
} else {
System.out.println("恭喜你," + count + "次就猜中了");
break;
}
}
}
}
 
================================================================================================================================
package cn.itcast_01;
 
import java.util.HashSet;
import java.util.Set;
 
/*
* Collection
* |--List
* 有序(存储顺序和取出顺序一致),可重复
* |--Set
* 无序(存储顺序和取出顺序不一致),唯一
*
* HashSet:它不保证 set 的迭代顺序;特别是它不保证该顺序恒久不变。
* 注意:虽然Set集合的元素无序,但是,作为集合来说,它肯定有它自己的存储顺序,
* 而你的顺序恰好和它的存储顺序一致,这代表不了有序,你可以多存储一些数据,就能看到效果。
*/
public class SetDemo {
public static void main(String[] args) {
// 创建集合对象
Set<String> set = new HashSet<String>();
 
// 创建并添加元素
set.add("hello");
set.add("java");
set.add("world");
set.add("java");
set.add("world");
 
// 增强for
for (String s : set) {
System.out.println(s);
}
}
}
java
world
hello
============================================================
package cn.itcast_02;
 
import java.util.HashSet;
 
/*
* HashSet:存储字符串并遍历
* 问题:为什么存储字符串的时候,字符串内容相同的只存储了一个呢?
* 通过查看add方法的源码,我们知 道这个方法底层依赖 两个方法:hashCode()和equals()。
* 步骤:
* 首先比较哈希值
* 如果相同,继续走,比较地址值或者走equals()
* 如果不同,就直接添加到集合中
* 按照方法的步骤来说:
* 先看hashCode()值是否相同
* 相同:继续走equals()方法
* 返回true: 说明元素重复,就不添加
* 返回false:说明元素不重复,就添加到集合
* 不同:就直接把元素添加到集合
* 如果类没有重写这两个方法,默认使用的Object()。一般来说不同相同。
* 而String类重写了hashCode()和equals()方法,所以,它就可以把内容相同的字符串去掉。只留下一个。
*/
public class HashSetDemo {
public static void main(String[] args) {
// 创建集合对象
HashSet<String> hs = new HashSet<String>();
 
// 创建并添加元素
hs.add("hello");
hs.add("world");
hs.add("java");
hs.add("world");
 
// 遍历集合
for (String s : hs) {
System.out.println(s);
}
}
}
 
=================================================================
package cn.itcast_02;
 
import java.util.HashSet;
 
/*
* 需求:存储自定义对象,并保证元素的唯一性
* 要求:如果两个对象的成员变量值都相同,则为同一个元素。
*
* 目前是不符合我的要求的:因为我们知道HashSet底层依赖的是hashCode()和equals()方法。
* 而这两个方法我们在学生类中没有重写,所以,默认使用的是Object类。
* 这个时候,他们的哈希值是不会一样的,根本就不会继续判断,执行了添加操作。
*/
public class HashSetDemo2 {
public static void main(String[] args) {
// 创建集合对象
HashSet<Student> hs = new HashSet<Student>();
 
// 创建学生对象
Student s1 = new Student("林青霞", 27);
Student s2 = new Student("柳岩", 22);
Student s3 = new Student("王祖贤", 30);
Student s4 = new Student("林青霞", 27);
Student s5 = new Student("林青霞", 20);
Student s6 = new Student("范冰冰", 22);
 
// 添加元素
hs.add(s1);
hs.add(s2);
hs.add(s3);
hs.add(s4);
hs.add(s5);
hs.add(s6);
 
// 遍历集合
for (Student s : hs) {
System.out.println(s.getName() + "---" + s.getAge());
}
}
}
===============================================================
package cn.itcast_02;
 
/**
* @author Administrator
*
*/
public class Student {
private String name;
private int age;
 
public Student() {
super();
}
 
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
 
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;
}
 
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
 
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
 
// @Override
// public int hashCode() {
// // return 0;
// // 因为成员变量值影响了哈希值,所以我们把成员变量值相加即可
// // return this.name.hashCode() + this.age;
// // 看下面
// // s1:name.hashCode()=40,age=30
// // s2:name.hashCode()=20,age=50
// // 尽可能的区分,我们可以把它们乘以一些整数
// return this.name.hashCode() + this.age * 15;
// }
//
// @Override
// public boolean equals(Object obj) {
// // System.out.println(this + "---" + obj);
// if (this == obj) {
// return true;
// }
//
// if (!(obj instanceof Student)) {
// return false;
// }
//
// Student s = (Student) obj;
// return this.name.equals(s.name) && this.age == s.age;
// }
//
// @Override
// public String toString() {
// return "Student [name=" + name + ", age=" + age + "]";
// }
 
}
===================================================================
package cn.itcast_03;
 
import java.util.HashSet;
 
/*
* HashSet集合存储自定义对象并遍历。如果对象的成员变量值相同即为同一个对象
*
* 注意了:
* 你使用的是HashSet集合,这个集合的底层是哈希表结构。
* 而哈希表结构底层依赖:hashCode()和equals()方法。
* 如果你认为对象的成员变量值相同即为同一个对象的话,你就应该重写这两个方法。
* 如何重写呢?不同担心,自动生成即可。
*/
public class DogDemo {
public static void main(String[] args) {
// 创建集合对象
HashSet<Dog> hs = new HashSet<Dog>();
 
// 创建狗对象
Dog d1 = new Dog("秦桧", 25, "红色", '男');
Dog d2 = new Dog("高俅", 22, "黑色", '女');
Dog d3 = new Dog("秦桧", 25, "红色", '男');
Dog d4 = new Dog("秦桧", 20, "红色", '女');
Dog d5 = new Dog("魏忠贤", 28, "白色", '男');
Dog d6 = new Dog("李莲英", 23, "黄色", '女');
Dog d7 = new Dog("李莲英", 23, "黄色", '女');
Dog d8 = new Dog("李莲英", 23, "黄色", '男');
 
// 添加元素
hs.add(d1);
hs.add(d2);
hs.add(d3);
hs.add(d4);
hs.add(d5);
hs.add(d6);
hs.add(d7);
hs.add(d8);
 
// 遍历
for (Dog d : hs) {
System.out.println(d.getName() + "---" + d.getAge() + "---"
+ d.getColor() + "---" + d.getSex());
}
}
}
 
高俅---22---黑色---女
李莲英---23---黄色---男
秦桧---25---红色---男
秦桧---20---红色---女
李莲英---23---黄色---女
魏忠贤---28---白色---男
 
============================================================
package cn.itcast_04;
 
import java.util.LinkedHashSet;
 
/*
* LinkedHashSet:底层数据结构由哈希表和链表组成。
* 哈希表保证元素的唯一性。
* 链表保证元素有素。(存储和取出是一致)
*/
public class LinkedHashSetDemo {
public static void main(String[] args) {
// 创建集合对象
LinkedHashSet<String> hs = new LinkedHashSet<String>();
 
// 创建并添加元素
hs.add("hello");
hs.add("world");
hs.add("java");
hs.add("world");
hs.add("java");
 
// 遍历
for (String s : hs) {
System.out.println(s);
}
}
}
 
package cn.itcast_set;
 
import java.util.LinkedHashSet;
 
class LinkedHashSetDemo {
public static void main(String[] args) {
 
 
// 创建集合对象
LinkedHashSet<String> hs = new LinkedHashSet<String>();
 
// 创建并添加元素
hs.add("hello");
hs.add("world");
hs.add("java");
hs.add("world");
hs.add("java");
 
// 遍历
for (String s : hs) {
System.out.println(s);
}
}
}
hello
world
java
 
================================================
package cn.itcast_05;
 
import java.util.TreeSet;
 
/*
* TreeSet:能够对元素按照某种规则进行排序。
* 排序有两种方式
* A:自然排序
* B:比较器排序
*
* TreeSet集合的特点:排序和唯一
*
* 通过观察TreeSet的add()方法,我们知道最终要看TreeMap的put()方法。
*/
public class TreeSetDemo {
public static void main(String[] args) {
// 创建集合对象
// 自然顺序进行排序
TreeSet<Integer> ts = new TreeSet<Integer>();
 
// 创建元素并添加
// 20,18,23,22,17,24,19,18,24
ts.add(20);
ts.add(18);
ts.add(23);
ts.add(22);
ts.add(17);
ts.add(24);
ts.add(19);
ts.add(18);
ts.add(24);
 
// 遍历
for (Integer i : ts) {
System.out.println(i);
}
}
}
 
17
18
19
20
22
23
24
========================================
package cn.itcast_05;
 
import java.util.TreeSet;
 
/*
* TreeSet存储自定义对象并保证排序和唯一。
*
* A:你没有告诉我们怎么排序
* 自然排序,按照年龄从小到大排序
* B:元素什么情况算唯一你也没告诉我
* 成员变量值都相同即为同一个元素
*/
public class TreeSetDemo2 {
public static void main(String[] args) {
// 创建集合对象
TreeSet<Student> ts = new TreeSet<Student>();
 
// 创建元素
Student s1 = new Student("linqingxia", 27);
Student s2 = new Student("zhangguorong", 29);
Student s3 = new Student("wanglihong", 23);
Student s4 = new Student("linqingxia", 27);
Student s5 = new Student("liushishi", 22);
Student s6 = new Student("wuqilong", 40);
Student s7 = new Student("fengqingy", 22);
 
// 添加元素
ts.add(s1);
ts.add(s2);
ts.add(s3);
ts.add(s4);
ts.add(s5);
ts.add(s6);
ts.add(s7);
 
// 遍历
for (Student s : ts) {
System.out.println(s.getName() + "---" + s.getAge());
}
}
}
 
fengqingy---22
liushishi---22
wanglihong---23
linqingxia---27
zhangguorong---29
wuqilong---40
 
package cn.itcast_05;
 
/*
* 如果一个类的元素要想能够进行自然排序,就必须实现自然排序接口
*/
public class Student implements Comparable<Student> {
private String name;
private int age;
 
public Student() {
super();
}
 
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
 
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;
}
 
@Override
public int compareTo(Student s) {
// return 0;
// return 1;
// return -1;
 
// 这里返回什么,其实应该根据我的排序规则来做
// 按照年龄排序,主要条件
int num = this.age - s.age;
// 次要条件
// 年龄相同的时候,还得去看姓名是否也相同
// 如果年龄和姓名都相同,才是同一个元素
int num2 = num == 0 ? this.name.compareTo(s.name) : num;
return num2;
}
}
 
=======================================================================================
package cn.itcast_06;
 
/*
* 如果一个类的元素要想能够进行自然排序,就必须实现自然排序接口
*/
public class Student implements Comparable<Student> {
private String name;
private int age;
 
public Student() {
super();
}
 
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
 
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;
}
 
@Override
public int compareTo(Student s) {
// 主要条件 姓名的长度
int num = this.name.length() - s.name.length();
// 姓名的长度相同,不代表姓名的内容相同
int num2 = num == 0 ? this.name.compareTo(s.name) : num;
// 姓名的长度和内容相同,不代表年龄相同,所以还得继续判断年龄
int num3 = num2 == 0 ? this.age - s.age : num2;
return num3;
}
}
 
package cn.itcast_06;
 
import java.util.TreeSet;
 
/*
* 需求:请按照姓名的长度排序
*/
public class TreeSetDemo {
public static void main(String[] args) {
// 创建集合对象
TreeSet<Student> ts = new TreeSet<Student>();
 
// 创建元素
Student s1 = new Student("linqingxia", 27);
Student s2 = new Student("zhangguorong", 29);
Student s3 = new Student("wanglihong", 23);
Student s4 = new Student("linqingxia", 27);
Student s5 = new Student("liushishi", 22);
Student s6 = new Student("wuqilong", 40);
Student s7 = new Student("fengqingy", 22);
Student s8 = new Student("linqingxia", 29);
 
// 添加元素
ts.add(s1);
ts.add(s2);
ts.add(s3);
ts.add(s4);
ts.add(s5);
ts.add(s6);
ts.add(s7);
ts.add(s8);
 
// 遍历
for (Student s : ts) {
System.out.println(s.getName() + "---" + s.getAge());
}
}
}
================================================
package cn.itcast_07;
 
public class Student {
private String name;
private int age;
 
public Student() {
super();
}
 
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
 
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;
}
}
 
 
package cn.itcast_07;
 
import java.util.Comparator;
 
public class MyComparator implements Comparator<Student> {
 
@Override
public int compare(Student s1, Student s2) {
// int num = this.name.length() - s.name.length();
// this -- s1
// s -- s2
// 姓名长度
int num = s1.getName().length() - s2.getName().length();
// 姓名内容
int num2 = num == 0 ? s1.getName().compareTo(s2.getName()) : num;
// 年龄
int num3 = num2 == 0 ? s1.getAge() - s2.getAge() : num2;
return num3;
}
 
}
 
package cn.itcast_07;
 
import java.util.Comparator;
import java.util.TreeSet;
 
/*
* 需求:请按照姓名的长度排序
*
* TreeSet集合保证元素排序和唯一性的原理
* 唯一性:是根据比较的返回是否是0来决定。
* 排序:
* A:自然排序(元素具备比较性)
* 让元素所属的类实现自然排序接口 Comparable
* B:比较器排序(集合具备比较性)
* 让集合的构造方法接收一个比较器接口的子类对象 Comparator
*/
public class TreeSetDemo {
public static void main(String[] args) {
// 创建集合对象
// TreeSet<Student> ts = new TreeSet<Student>(); //自然排序
// public TreeSet(Comparator comparator) //比较器排序
// TreeSet<Student> ts = new TreeSet<Student>(new MyComparator());
 
// 如果一个方法的参数是接口,那么真正要的是接口的实现类的对象
// 而匿名内部类就可以实现这个东西
TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
// 姓名长度
int num = s1.getName().length() - s2.getName().length();
// 姓名内容
int num2 = num == 0 ? s1.getName().compareTo(s2.getName())
: num;
// 年龄
int num3 = num2 == 0 ? s1.getAge() - s2.getAge() : num2;
return num3;
}
});
 
// 创建元素
Student s1 = new Student("linqingxia", 27);
Student s2 = new Student("zhangguorong", 29);
Student s3 = new Student("wanglihong", 23);
Student s4 = new Student("linqingxia", 27);
Student s5 = new Student("liushishi", 22);
Student s6 = new Student("wuqilong", 40);
Student s7 = new Student("fengqingy", 22);
Student s8 = new Student("linqingxia", 29);
 
// 添加元素
ts.add(s1);
ts.add(s2);
ts.add(s3);
ts.add(s4);
ts.add(s5);
ts.add(s6);
ts.add(s7);
ts.add(s8);
 
// 遍历
for (Student s : ts) {
System.out.println(s.getName() + "---" + s.getAge());
}
}
}
===========================================================================package cn.itcast_08;
 
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;
 
/*
* 键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低输出到控制台
*
* 分析:
* A:定义学生类
* B:创建一个TreeSet集合
* C:总分从高到底如何实现呢?
* D:键盘录入5个学生信息
* E:遍历TreeSet集合
*/
public class TreeSetDemo {
public static void main(String[] args) {
// 创建一个TreeSet集合
TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
// 总分从高到低
int num = s2.getSum() - s1.getSum();
// 总分相同的不一定语文相同
int num2 = num == 0 ? s1.getChinese() - s2.getChinese() : num;
// 总分相同的不一定数序相同
int num3 = num2 == 0 ? s1.getMath() - s2.getMath() : num2;
// 总分相同的不一定英语相同
int num4 = num3 == 0 ? s1.getEnglish() - s2.getEnglish() : num3;
// 姓名还不一定相同呢
int num5 = num4 == 0 ? s1.getName().compareTo(s2.getName())
: num4;
return num5;
}
});
 
System.out.println("学生信息录入开始");
// 键盘录入5个学生信息
for (int x = 1; x <= 5; x++) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第" + x + "个学生的姓名:");
String name = sc.nextLine();
System.out.println("请输入第" + x + "个学生的语文成绩:");
String chineseString = sc.nextLine();
System.out.println("请输入第" + x + "个学生的数学成绩:");
String mathString = sc.nextLine();
System.out.println("请输入第" + x + "个学生的英语成绩:");
String englishString = sc.nextLine();
 
// 把数据封装到学生对象中
Student s = new Student();
s.setName(name);
s.setChinese(Integer.parseInt(chineseString));
s.setMath(Integer.parseInt(mathString));
s.setEnglish(Integer.parseInt(englishString));
 
// 把学生对象添加到集合
ts.add(s);
}
System.out.println("学生信息录入完毕");
 
System.out.println("学习信息从高到低排序如下:");
System.out.println("姓名 语文成绩 数学成绩 英语成绩");
// 遍历集合
for (Student s : ts) {
System.out.println(s.getName() + " " + s.getChinese() + " "
+ s.getMath() + " " + s.getEnglish());
}
}
}
 
学习信息从高到低排序如下:
姓名 语文成绩 数学成绩 英语成绩
45 78 90 6767
23 56 78 909
778 66 77 88
99 77 66 88
56 78 67 66
 
package cn.itcast_08;
 
public class Student {
// 姓名
private String name;
// 语文成绩
private int chinese;
// 数学成绩
private int math;
// 英语成绩
private int english;
 
public Student(String name, int chinese, int math, int english) {
super();
this.name = name;
this.chinese = chinese;
this.math = math;
this.english = english;
}
 
public Student() {
super();
}
 
public String getName() {
return name;
}
 
public void setName(String name) {
this.name = name;
}
 
public int getChinese() {
return chinese;
}
 
public void setChinese(int chinese) {
this.chinese = chinese;
}
 
public int getMath() {
return math;
}
 
public void setMath(int math) {
this.math = math;
}
 
public int getEnglish() {
return english;
}
 
public void setEnglish(int english) {
this.english = english;
}
 
public int getSum() {
return this.chinese + this.math + this.english;
}
}
===========================================================================package cn.itcast_08;
 
import java.util.HashSet;
import java.util.Random;
 
/*
* 编写一个程序,获取10个1至20的随机数,要求随机数不能重复。
*
* 分析:
* A:创建随机数对象
* B:创建一个HashSet集合
* C:判断集合的长度是不是小于10
* 是:就创建一个随机数添加
* 否:不搭理它
* D:遍历HashSet集合
*/
public class HashSetDemo {
public static void main(String[] args) {
// 创建随机数对象
Random r = new Random();
 
// 创建一个Set集合
HashSet<Integer> ts = new HashSet<Integer>();
 
// 判断集合的长度是不是小于10
while (ts.size() < 10) {
int num = r.nextInt(20) + 1;
ts.add(num);
}
 
// 遍历Set集合
for (Integer i : ts) {
System.out.println(i);
}
}
}
 
 三.针对键值对的形式:
package cn.itcast_01;
 
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
 
/*
* Collections:是针对集合进行操作的工具类,都是静态方法。
*
* 面试题:
* Collection和Collections的区别?
* Collection:是单列集合的顶层接口,有子接口List和Set。
* Collections:是针对集合操作的工具类,有对集合进行排序和二分查找的方法
*
* 要知道的方法
* public static <T> void sort(List<T> list):排序 默认情况下是自然顺序。
* public static <T> int binarySearch(List<?> list,T key):二分查找
* public static <T> T max(Collection<?> coll):最大值
* public static void reverse(List<?> list):反转
* public static void shuffle(List<?> list):随机置换
*/
public class CollectionsDemo {
public static void main(String[] args) {
// 创建集合对象
List<Integer> list = new ArrayList<Integer>();
 
// 添加元素
list.add(30);
list.add(20);
list.add(50);
list.add(10);
list.add(40);
 
System.out.println("list:" + list);
 
// public static <T> void sort(List<T> list):排序 默认情况下是自然顺序。
// Collections.sort(list);
// System.out.println("list:" + list);
// [10, 20, 30, 40, 50]
 
// public static <T> int binarySearch(List<?> list,T key):二分查找
// System.out .println("binarySearch:" + Collections.binarySearch(list, 30));
// System.out.println("binarySearch:"
// + Collections.binarySearch(list, 300));
 
// public static <T> T max(Collection<?> coll):最大值
// System.out.println("max:"+Collections.max(list));
 
// public static void reverse(List<?> list):反转
// Collections.reverse(list);
// System.out.println("list:" + list);
 
//public static void shuffle(List<?> list):随机置换
Collections.shuffle(list);
System.out.println("list:" + list);
}
}
===========================================================================package cn.itcast_02;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
 
/*
* Collections可以针对ArrayList存储基本包装类的元素排序,存储自定义对象可不可以排序呢?
*/
public class CollectionsDemo {
public static void main(String[] args) {
// 创建集合对象
List<Student> list = new ArrayList<Student>();
 
// 创建学生对象
Student s1 = new Student("林青霞", 27);
Student s2 = new Student("风清扬", 30);
Student s3 = new Student("刘晓曲", 28);
Student s4 = new Student("武鑫", 29);
Student s5 = new Student("林青霞", 27);
 
// 添加元素对象
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
list.add(s5);
 
// 排序
// 自然排序
// Collections.sort(list);
// 比较器排序
// 如果同时有自然排序和比较器排序,以比较器排序为主
Collections.sort(list, new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
int num = s2.getAge() - s1.getAge();
int num2 = num == 0 ? s1.getName().compareTo(s2.getName())
: num;
return num2;
}
});
 
// 遍历集合
for (Student s : list) {
System.out.println(s.getName() + "---" + s.getAge());
}
}
}
 
package cn.itcast_02;
 
/**
* @author Administrator
*
*/
public class Student implements Comparable<Student> {
private String name;
private int age;
 
public Student() {
super();
}
 
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
 
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;
}
 
@Override
public int compareTo(Student s) {
int num = this.age - s.age;
int num2 = num == 0 ? this.name.compareTo(s.name) : num;
return num2;
}
}
===========================================================================package cn.itcast_03;
 
import java.util.ArrayList;
import java.util.Collections;
 
/*
* 模拟斗地主洗牌和发牌
*
* 分析:
* A:创建一个牌盒
* B:装牌
* C:洗牌
* D:发牌
* E:看牌
*/
public class PokerDemo {
public static void main(String[] args) {
// 创建一个牌盒
ArrayList<String> array = new ArrayList<String>();
 
// 装牌
// 黑桃A,黑桃2,黑桃3,...黑桃K
// 红桃A,...
// 梅花A,...
// 方块A,...
// 定义一个花色数组
String[] colors = { "♠", "♥", "♣", "♦" };
// 定义一个点数数组
String[] numbers = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"J", "Q", "K" };
// 装牌
for (String color : colors) {
for (String number : numbers) {
array.add(color.concat(number));
}
}
array.add("小王");
array.add("大王");
 
// 洗牌
Collections.shuffle(array);
 
// System.out.println("array:" + array);
 
// 发牌
ArrayList<String> fengQingYang = new ArrayList<String>();
ArrayList<String> linQingXia = new ArrayList<String>();
ArrayList<String> liuYi = new ArrayList<String>();
ArrayList<String> diPai = new ArrayList<String>();
 
for (int x = 0; x < array.size(); x++) {
if (x >= array.size() - 3) {
diPai.add(array.get(x));
} else if (x % 3 == 0) {
fengQingYang.add(array.get(x));
} else if (x % 3 == 1) {
linQingXia.add(array.get(x));
} else if (x % 3 == 2) {
liuYi.add(array.get(x));
}
}
 
// 看牌
lookPoker("风清扬", fengQingYang);
lookPoker("林青霞", linQingXia);
lookPoker("刘意", liuYi);
 
lookPoker("底牌", diPai);
}
 
public static void lookPoker(String name, ArrayList<String> array) {
System.out.print(name + "的牌是:");
for (String s : array) {
System.out.print(s + " ");
}
System.out.println();
}
}
===============================================================================================
package cn.itcast_04;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;
 
/*
* 思路:
* A:创建一个HashMap集合
* B:创建一个ArrayList集合
* C:创建花色数组和点数数组
* D:从0开始往HashMap里面存储编号,并存储对应的牌
* 同时往ArrayList里面存储编号即可。
* E:洗牌(洗的是编号)
* F:发牌(发的也是编号,为了保证编号是排序的,就创建TreeSet集合接收)
* G:看牌(遍历TreeSet集合,获取编号,到HashMap集合找对应的牌)
*/
public class PokerDemo {
public static void main(String[] args) {
// 创建一个HashMap集合
HashMap<Integer, String> hm = new HashMap<Integer, String>();
 
// 创建一个ArrayList集合
ArrayList<Integer> array = new ArrayList<Integer>();
 
// 创建花色数组和点数数组
// 定义一个花色数组
String[] colors = { "♠", "♥", "♣", "♦" };
// 定义一个点数数组
String[] numbers = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q",
"K", "A", "2", };
 
// 从0开始往HashMap里面存储编号,并存储对应的牌,同时往ArrayList里面存储编号即可。
int index = 0;
 
for (String number : numbers) {
for (String color : colors) {
String poker = color.concat(number);
hm.put(index, poker);
array.add(index);
index++;
}
}
hm.put(index, "小王");
array.add(index);
index++;
hm.put(index, "大王");
array.add(index);
 
// 洗牌(洗的是编号)
Collections.shuffle(array);
 
// 发牌(发的也是编号,为了保证编号是排序的,就创建TreeSet集合接收)
TreeSet<Integer> fengQingYang = new TreeSet<Integer>();
TreeSet<Integer> linQingXia = new TreeSet<Integer>();
TreeSet<Integer> liuYi = new TreeSet<Integer>();
TreeSet<Integer> diPai = new TreeSet<Integer>();
 
for (int x = 0; x < array.size(); x++) {
if (x >= array.size() - 3) {
diPai.add(array.get(x));
} else if (x % 3 == 0) {
fengQingYang.add(array.get(x));
} else if (x % 3 == 1) {
linQingXia.add(array.get(x));
} else if (x % 3 == 2) {
liuYi.add(array.get(x));
}
}
 
// 看牌(遍历TreeSet集合,获取编号,到HashMap集合找对应的牌)
lookPoker("风清扬", fengQingYang, hm);
lookPoker("林青霞", linQingXia, hm);
lookPoker("刘意", liuYi, hm);
lookPoker("底牌", diPai, hm);
}
 
// 写看牌的功能
public static void lookPoker(String name, TreeSet<Integer> ts,
HashMap<Integer, String> hm) {
System.out.print(name + "的牌是:");
for (Integer key : ts) {
String value = hm.get(key);
System.out.print(value + " ");
}
System.out.println();
}
}
==================================================
package cn.itcast_01;
 
import java.util.HashMap;
import java.util.Map;
 
/*
* 作为学生来说,是根据学号来区分不同的学生的,那么假设我现在已经知道了学生的学号,我要根据学号去获取学生姓名,请问怎么做呢?
* 如果采用前面讲解过的集合,我们只能把学号和学生姓名作为一个对象的成员,然后存储整个对象,将来遍历的时候,判断,获取对应的名称。
* 但是呢,如果我都能把学生姓名拿出来了,我还需要根据编号去找吗?d
* 针对我们目前的这665 种需求:仅仅知道学号,就想知道学生姓名的情况,Java就提供了一种新的集合 Map。
* 通过查看API,我们知道Map集合的一个最大的特点,就是它可以存储键值对的元素。这个时候存储我们上面的需求,就可以这样做
* 学号1 姓名1
* 学号2 姓名2
* 学号3 姓名3
* 学号2(不行)姓名4
* 学号4 姓名4
* Map集合的特点:
* 将键映射到值的对象。一个映射不能包含重复的键;每个键最多只能映射到一个值。
*
* Map集合和Collection集合的区别?
* Map集合存储元素是成对出现的,Map集合的键是唯一的,值是可重复的。可以把这个理解为:夫妻对
* Collection集合存储元素是单独出现的,Collection的儿子Set是唯一的,List是可重复的。可以把这个理解为:光棍(11.11)
*
* 注意:
* Map集合的数据结构值针对键有效,跟值无关
* HashMap,TreeMap等会讲。
* Collection集合的数据结构是针对元素有效
*
* Map集合的功能概述:
* 1:添加功能
* V put(K key,V value):添加元素。这个其实还有另一个功能?先不告诉你,等会讲
* 如果键是第一次存储,就直接存储元素,返回null
* 如果键不是第一次存在,就用值把以前的值替换掉,返回以前的值
* 2:删除功能
* void clear():移除所有的键值对元素
* V remove(Object key):根据键删除键值对9999999元素,并把值返回
* 3:判断功能
* boolean containsKey(Object key):判断集合是否包含指定的键
* boolean containsValue(Object value):判断集合是否包含指定的值
* boolean isEmpty():判断集合是否为空
* 4:获取功能
* Set<Map.Entry<K,V>> entrySet():???
* V get(Object key):根据键获取值
* Set<K> keySet():获取集合中所有键的集合
* Collection<V> values():获取集合中所有值的集合
* 5:长度功能
* int size():返回集合中的键值对的对数
*/
public class MapDemo {
public static void main(String[] args) {
// 创建集合对象
Map<String, String> map = new HashMap<String, String>();
 
// 添加元素
// V put(K key,V value):添加元素。这个其实还有另一个功能?先不告诉你,等会讲
// System.out.println("put:" + map.put("文章", "马伊俐"));
// System.out.println("put:" + map.put("文章", "姚笛"));
 
map.put("邓超", "孙俪");
map.put("黄晓明", "杨颖");
map.put("周杰伦", "蔡依林");
map.put("刘恺威", "杨幂");
 
// void clear():移除所有的键值对元素
// map.clear();
 
// V remove(Object key):根据键删除键值对元素,并把值返回
// System.out.println("remove:" + map.remove("黄晓明"));
// System.out.println("remove:" + map.remove("黄晓波"));
 
// boolean containsKey(Object key):判断集合是否包含指定的键
// System.out.println("containsKey:" + map.containsKey("黄晓明"));
// System.out.println("containsKey:" + map.containsKey("黄晓波"));
 
// boolean isEmpty():判断集合是否为空
// System.out.println("isEmpty:"+map.isEmpty());
 
//int size():返回集合中的键值对的对数
System.out.println("size:"+map.size());
 
// 输出集合名称
System.out.println("map:" + map);
}
}
===========================================================================package cn.itcast_01;
 
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
 
/*
* 获取功能:
* V get(Object key):根据键获取值
* Set<K> keySet():获取集合中所有键的集合
* Collection<V> values():获取集合中所有值的集合
*/
public class MapDemo2 {
public static void main(String[] args) {
// 创建集合对象
Map<String, String> map = new HashMap<String, String>();
 
// 创建元素并添加元素
map.put("邓超", "孙俪");
map.put("黄晓明", "杨颖");
map.put("周杰伦", "蔡依林");
map.put("刘恺威", "杨幂");
 
// V get(Object key):根据键获取值
System.out.println("get:" + map.get("周杰伦"));
System.out.println("get:" + map.get("周杰")); // 返回null
System.out.println("----------------------");
 
// Set<K> keySet():获取集合中所有键的集合
Set<String> set = map.keySet();
for (String key : set) {
System.out.println(key);
}
System.out.println("----------------------");
 
// Collection<V> values():获取集合中所有值的集合
Collection<String> con = map.values();
for (String value : con) {
System.out.println(value);
}
}
}
==============================================================================================
package cn.itcast_01;
 
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
 
/*
* Map集合的遍历。
* Map -- 夫妻对
* 思路:
* A:把所有的丈夫给集中起来。
* B:遍历丈夫的集合,获取得到每一个丈夫。
* C:让丈夫去找自己的妻子。
*
* 转换:
* A:获取所有的键
* B:遍历键的集合,获取得到每一个键
* C:根据键去找值
*/
public class MapDemo3 {
public static void main(String[] args) {
// 创建集合对象
Map<String, String> map = new HashMap<String, String>();
 
// 创建元素并添加到集合
map.put("杨过", "小龙女");
map.put("郭靖", "黄蓉");
map.put("杨康", "穆念慈");
map.put("陈玄风", "梅超风");
 
// 遍历
// 获取所有的键
Set<String> set = map.keySet();
// 遍历键的集合,获取得到每一个键
for (String key : set) {
// 根据键去找值
String value = map.get(key);
System.out.println(key + "---" + value);
}
}
}
=================================================================
package cn.itcast_01;
 
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
 
/*
* Map集合的遍历。
* Map -- 夫妻对
*
* 思路:
* A:获取所有结婚证的集合
* B:遍历结婚证的集合,得到每一个结婚证
* C:根据结婚证获取丈夫和妻子
*
* 转换:
* A:获取所有键值对对象的集合
* B:遍历键值对对象的集合,得到每一个键值对对象
* C:根据键值对对象获取键和值
*
* 这里面最麻烦的就是键值对对象如何表示呢?
* 看看我们开始的一个方法:
* Set<Map.Entry<K,V>> entrySet():返回的是键值对对象的集合
*/
public class MapDemo4 {
public static void main(String[] args) {
// 创建集合对象
Map<String, String> map = new HashMap<String, String>();
 
// 创建元素并添加到集合
map.put("杨过", "小龙女");
map.put("郭靖", "黄蓉");
map.put("杨康", "穆念慈");
map.put("陈玄风", "梅超风");
 
// 获取所有键值对对象的集合
Set<Map.Entry<String, String>> set = map.entrySet();
// 遍历键值对对象的集合,得到每一个键值对对象
for (Map.Entry<String, String> me : set) {
// 根据键值对对象获取键和值
String key = me.getKey();
String value = me.getValue();
System.out.println(key + "---" + value);
}
}
}
==========================================================
package cn.itcast_02;
 
import java.util.HashMap;
import java.util.Set;
 
/*
* HashMap:是基于哈希表的Map接口实现。
* 哈希表的作用是用来保证键的唯一性的。
*
* HashMap<String,String>
* 键:String
* 值:String
*/
public class HashMapDemo {
public static void main(String[] args) {
// 创建集合对象
HashMap<String, String> hm = new HashMap<String, String>();
 
// 创建元素并添加元素
// String key1 = "it001";
// String value1 = "马云";
// hm.put(key1, value1);
 
hm.put("it001", "马云");
hm.put("it003", "马化腾");
hm.put("it004", "乔布斯");
hm.put("it005", "张朝阳");
hm.put("it002", "裘伯君"); // wps
hm.put("it001", "比尔盖茨");
 
// 遍历
Set<String> set = hm.keySet();
for (String key : set) {
String value = hm.get(key);
System.out.println(key + "---" + value);
}
}
}
===========================================================================
package cn.itcast_02;
 
import java.util.HashMap;
import java.util.Set;
 
/*
* HashMap<Integer,String>
* 键:Integer
* 值:String
*/
public class HashMapDemo2 {
public static void main(String[] args) {
// 创建集合对象
HashMap<Integer, String> hm = new HashMap<Integer, String>();
 
// 创建元素并添加元素
// Integer i = new Integer(27);
// Integer i = 27;
// String s = "林青霞";
// hm.put(i, s);
 
hm.put(27, "林青霞");
hm.put(30, "风清扬");
hm.put(28, "刘意");
hm.put(29, "林青霞");
 
// 下面的写法是八进制,但是不能出现8以上的单个数据
// hm.put(003, "hello");
// hm.put(006, "hello");
// hm.put(007, "hello");
// hm.put(008, "hello");
 
// 遍历
Set<Integer> set = hm.keySet();
for (Integer key : set) {
String value = hm.get(key);
System.out.println(key + "---" + value);
}
 
// 下面这种方式仅仅是集合的元素的字符串表示
// System.out.println("hm:" + hm);
}
}
===============================================================
package cn.itcast_02;
 
import java.util.HashMap;
import java.util.Set;
 
/*
* HashMap<String,Student>
* 键:String 学号
* 值:Student 学生对象
*/
public class HashMapDemo3 {
public static void main(String[] args) {
// 创建集合对象
HashMap<String, Student> hm = new HashMap<String, Student>();
 
// 创建学生对象
Student s1 = new Student("周星驰", 58);
Student s2 = new Student("刘德华", 55);
Student s3 = new Student("梁朝伟", 54);
Student s4 = new Student("刘嘉玲", 50);
 
// 添加元素
hm.put("9527", s1);
hm.put("9522", s2);
hm.put("9524", s3);
hm.put("9529", s4);
 
// 遍历
Set<String> set = hm.keySet();
for (String key : set) {
// 注意了:这次值不是字符串了
// String value = hm.get(key);
Student value = hm.get(key);
System.out.println(key + "---" + value.getName() + "---"
+ value.getAge());
}
}
}
 
=========================================================================
package cn.itcast_02;
 
import java.util.HashMap;
import java.util.Set;
 
/*
* HashMap<Student,String>
* 键:Student
* 要求:如果两个对象的成员变量值都相同,则为同一个对象。
* 值:String
*/
public class HashMapDemo4 {
public static void main(String[] args) {
// 创建集合对象
HashMap<Student, String> hm = new HashMap<Student, String>();
 
// 创建学生对象
Student s1 = new Student("貂蝉", 27);
Student s2 = new Student("王昭君", 30);
Student s3 = new Student("西施", 33);
Student s4 = new Student("杨玉环", 35);
Student s5 = new Student("貂蝉", 27);
 
// 添加元素
hm.put(s1, "8888");
hm.put(s2, "6666");
hm.put(s3, "5555");
hm.put(s4, "7777");
hm.put(s5, "9999");
 
// 遍历
Set<Student> set = hm.keySet();
for (Student key : set) {
String value = hm.get(key);
System.out.println(key.getName() + "---" + key.getAge() + "---"
+ value);
}
}
}
=================================================================
package cn.itcast_04;
 
import java.util.Set;
import java.util.TreeMap;
 
/*
* TreeMap:是基于红黑树的Map接口的实现。
*
* HashMap<String,String>
* 键:String
* 值:String
*/
public class TreeMapDemo {
public static void main(String[] args) {
// 创建集合对象
TreeMap<String, String> tm = new TreeMap<String, String>();
 
// 创建元素并添加元素
tm.put("hello", "你好");
tm.put("world", "世界");
tm.put("java", "爪哇");
tm.put("world", "世界2");
tm.put("javaee", "爪哇EE");
 
// 遍历集合
Set<String> set = tm.keySet();
for (String key : set) {
String value = tm.get(key);
System.out.println(key + "---" + value);
}
}
}
===========================================================================
package cn.itcast_04;
 
import java.util.Comparator;
import java.util.Set;
import java.util.TreeMap;
 
/*
* TreeMap<Student,String>
* 键:Student
* 值:String
*/
public class TreeMapDemo2 {
public static void main(String[] args) {
// 创建集合对象
TreeMap<Student, String> tm = new TreeMap<Student, String>(
new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
// 主要条件
int num = s1.getAge() - s2.getAge();
// 次要条件
int num2 = num == 0 ? s1.getName().compareTo(
s2.getName()) : num;
return num2;
}
});
 
// 创建学生对象
Student s1 = new Student("潘安", 30);
Student s2 = new Student("柳下惠", 35);
Student s3 = new Student("唐伯虎", 33);
Student s4 = new Student("燕青", 32);
Student s5 = new Student("唐伯虎", 33);
 
// 存储元素
tm.put(s1, "宋朝");
tm.put(s2, "元朝");
tm.put(s3, "明朝");
tm.put(s4, "清朝");
tm.put(s5, "汉朝");
 
// 遍历
Set<Student> set = tm.keySet();
for (Student key : set) {
String value = tm.get(key);
System.out.println(key.getName() + "---" + key.getAge() + "---"
+ value);
}
}
}
 
package cn.itcast_04;
 
public class Student {
private String name;
private int age;
 
public Student() {
super();
}
 
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
 
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;
}
}
 
===============================
package cn.itcast_05;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
 
/*
*需求:
*假设HashMap集合的元素是ArrayList。有3个。
*每一个ArrayList集合的值是字符串。
*元素我已经完成,请遍历。
*结果:
* 三国演义
* 吕布
* 周瑜
* 笑傲江湖
* 令狐冲
* 林平之
* 神雕侠侣
* 郭靖
* 杨过
*/
public class HashMapIncludeArrayListDemo {
public static void main(String[] args) {
// 创建集合对象
HashMap<String, ArrayList<String>> hm = new HashMap<String, ArrayList<String>>();
 
// 创建元素集合1
ArrayList<String> array1 = new ArrayList<String>();
array1.add("吕布");
array1.add("周瑜");
hm.put("三国演义", array1);
 
// 创建元素集合2
ArrayList<String> array2 = new ArrayList<String>();
array2.add("令狐冲");
array2.add("林平之");
hm.put("笑傲江湖", array2);
 
// 创建元素集合3
ArrayList<String> array3 = new ArrayList<String>();
array3.add("郭靖");
array3.add("杨过");
hm.put("神雕侠侣", array3);
 
//遍历集合
Set<String> set = hm.keySet();
for(String key : set){
System.out.println(key);
ArrayList<String> value = hm.get(key);
for(String s : value){
System.out.println(" "+s);
}
}
}
}
 
神雕侠侣
郭靖
杨过
三国演义
吕布
周瑜
笑傲江湖
令狐冲
林平之
 
====================================
package cn.itcast_05;
 
import java.util.HashMap;
import java.util.Set;
 
/*
* HashMap嵌套HashMap
*
* 传智播客
* jc 基础班
* 陈玉楼 20
* 高跃 22
* jy 就业班
* 李杰 21
* 曹石磊 23
*
* 先存储元素,然后遍历元素
*/
public class HashMapDemo2 {
public static void main(String[] args) {
// 创建集合对象
HashMap<String, HashMap<String, Integer>> czbkMap = new HashMap<String, HashMap<String, Integer>>();
 
// 创建基础班集合对象
HashMap<String, Integer> jcMap = new HashMap<String, Integer>();
// 添加元素
jcMap.put("陈玉楼", 20);
jcMap.put("高跃", 22);
// 把基础班添加到大集合
czbkMap.put("jc", jcMap);
 
// 创建就业班集合对象
HashMap<String, Integer> jyMap = new HashMap<String, Integer>();
// 添加元素
jyMap.put("李杰", 21);
jyMap.put("曹石磊", 23);
// 把基础班添加到大集合
czbkMap.put("jy", jyMap);
 
//遍历集合
Set<String> czbkMapSet = czbkMap.keySet();
for(String czbkMapKey : czbkMapSet){
System.out.println(czbkMapKey);
HashMap<String, Integer> czbkMapValue = czbkMap.get(czbkMapKey);
Set<String> czbkMapValueSet = czbkMapValue.keySet();
for(String czbkMapValueKey : czbkMapValueSet){
Integer czbkMapValueValue = czbkMapValue.get(czbkMapValueKey);
System.out.println(" "+czbkMapValueKey+"---"+czbkMapValueValue);
}
}
}
}
jc
高跃---22
陈玉楼---20
jy
曹石磊---23
李杰---21
=====================================================
package cn.itcast_05;
 
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
 
/*
* 需求 :"aababcabcdabcde",获取字符串中每一个字母出现的次数要求结果:a(5)b(4)c(3)d(2)e(1)
*
* 分析:
* A:定义一个字符串(可以改进为键盘录入)
* B:定义一个TreeMap集合
* 键:Character
* 值:Integer
* C:把字符串转换为字符数组
* D:遍历字符数组,得到每一个字符
* E:拿刚才得到的字符作为键到集合中去找值,看返回值
* 是null:说明该键不存在,就把该字符作为键,1作为值存储
* 不是null:说明该键存在,就把值加1,然后重写存储该键和值
* F:定义字符串缓冲区变量
* G:遍历集合,得到键和值,进行按照要求拼接
* H:把字符串缓冲区转换为字符串输出
*
* 录入:linqingxia
* 结果:result:a(1)g(1)i(3)l(1)n(2)q(1)x(1)
*/
public class TreeMapDemo {
public static void main(String[] args) {
// 定义一个字符串(可以改进为键盘录入)
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String line = sc.nextLine();
 
// 定义一个TreeMap集合
TreeMap<Character, Integer> tm = new TreeMap<Character, Integer>();
 
//把字符串转换为字符数组
char[] chs = line.toCharArray();
 
//遍历字符数组,得到每一个字符
for(char ch : chs){
//拿刚才得到的字符作为键到集合中去找值,看返回值
Integer i = tm.get(ch);
 
//是null:说明该键不存在,就把该字符作为键,1作为值存储
if(i == null){
tm.put(ch, 1);
}else {
//不是null:说明该键存在,就把值加1,然后重写存储该键和值
i++;
tm.put(ch,i);
}
}
 
//定义字符串缓冲区变量
StringBuilder sb= new StringBuilder();
 
//遍历集合,得到键和值,进行按照要求拼接
Set<Character> set = tm.keySet();
for(Character key : set){
Integer value = tm.get(key);
sb.append(key).append("(").append(value).append(")");
}
 
//把字符串缓冲区转换为字符串输出
String result = sb.toString();
System.out.println("result:"+result);
}
}
 
================================
package cn.itcast_05;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
 
/*
ArrayList集合嵌套HashMap集合并遍历。
需求:
假设ArrayList集合的元素是HashMap。有3个。
每一个HashMap集合的键和值都是字符串。
元素我已经完成,请遍历。
结果:
周瑜---小乔
吕布---貂蝉
 
郭靖---黄蓉
杨过---小龙女
 
令狐冲---任盈盈
林平之---岳灵珊
*/
public class ArrayListIncludeHashMapDemo {
public static void main(String[] args) {
// 创建集合对象
ArrayList<HashMap<String, String>> array = new ArrayList<HashMap<String, String>>();
 
// 创建元素1
HashMap<String, String> hm1 = new HashMap<String, String>();
hm1.put("周瑜", "小乔");
hm1.put("吕布", "貂蝉");
// 把元素添加到array里面
array.add(hm1);
 
// 创建元素1
HashMap<String, String> hm2 = new HashMap<String, String>();
hm2.put("郭靖", "黄蓉");
hm2.put("杨过", "小龙女");
// 把元素添加到array里面
array.add(hm2);
 
// 创建元素1
HashMap<String, String> hm3 = new HashMap<String, String>();
hm3.put("令狐冲", "任盈盈");
hm3.put("林平之", "岳灵珊");
// 把元素添加到array里面
array.add(hm3);
 
// 遍历
for (HashMap<String, String> hm : array) {
Set<String> set = hm.keySet();
for (String key : set) {
String value = hm.get(key);
System.out.println(key + "---" + value);
}
}
}
}
==================================
package cn.itcast_06;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
 
/*
* 为了更符合要求:
* 这次的数据就看成是学生对象。
*
* 传智播客
* bj 北京校区
* jc 基础班
* 林青霞 27
* 风清扬 30
* jy 就业班
* 赵雅芝 28
* 武鑫 29
* sh 上海校区
* jc 基础班
* 郭美美 20
* 犀利哥 22
* jy 就业班
* 罗玉凤 21
* 马征 23
* gz 广州校区
* jc 基础班
* 王力宏 30
* 李静磊 32
* jy 就业班
* 郎朗 31
* 柳岩 33
* xa 西安校区
* jc 基础班
* 范冰冰 27
* 刘意 30
* jy 就业班
* 李冰冰 28
* 张志豪 29
*/
public class HashMapDemo {
public static void main(String[] args) {
// 创建大集合
HashMap<String, HashMap<String, ArrayList<Student>>> czbkMap = new HashMap<String, HashMap<String, ArrayList<Student>>>();
 
// 北京校区数据
HashMap<String, ArrayList<Student>> bjCzbkMap = new HashMap<String, ArrayList<Student>>();
ArrayList<Student> array1 = new ArrayList<Student>();
Student s1 = new Student("林青霞", 27);
Student s2 = new Student("风清扬", 30);
array1.add(s1);
array1.add(s2);
ArrayList<Student> array2 = new ArrayList<Student>();
Student s3 = new Student("赵雅芝", 28);
Student s4 = new Student("武鑫", 29);
array2.add(s3);
array2.add(s4);
bjCzbkMap.put("基础班", array1);
bjCzbkMap.put("就业班", array2);
czbkMap.put("北京校区", bjCzbkMap);
 
// 晚上可以自己练习一下
// 上海校区数据自己做
// 广州校区数据自己做
 
// 西安校区数据
HashMap<String, ArrayList<Student>> xaCzbkMap = new HashMap<String, ArrayList<Student>>();
ArrayList<Student> array3 = new ArrayList<Student>();
Student s5 = new Student("范冰冰", 27);
Student s6 = new Student("刘意", 30);
array3.add(s5);
array3.add(s6);
ArrayList<Student> array4 = new ArrayList<Student>();
Student s7 = new Student("李冰冰", 28);
Student s8 = new Student("张志豪", 29);
array4.add(s7);
array4.add(s8);
xaCzbkMap.put("基础班", array3);
xaCzbkMap.put("就业班", array4);
czbkMap.put("西安校区", xaCzbkMap);
 
// 遍历集合
Set<String> czbkMapSet = czbkMap.keySet();
for (String czbkMapKey : czbkMapSet) {
System.out.println(czbkMapKey);
HashMap<String, ArrayList<Student>> czbkMapValue = czbkMap
.get(czbkMapKey);
Set<String> czbkMapValueSet = czbkMapValue.keySet();
for (String czbkMapValueKey : czbkMapValueSet) {
System.out.println(" " + czbkMapValueKey);
ArrayList<Student> czbkMapValueValue = czbkMapValue
.get(czbkMapValueKey);
for (Student s : czbkMapValueValue) {
System.out.println(" " + s.getName() + "---"
+ s.getAge());
}
}
}
}
}
 
 
 
 
package cn.itcast_06;
 
public class Student {
private String name;
private int age;
 
public Student() {
super();
}
 
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
 
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;
}
 
}
 
===============================================================
package cn.itcast_07;
 
import java.util.Hashtable;
 
/*
* 1:Hashtable和HashMap的区别?
* Hashtable:线程安全,效率低。不允许null键和null值
* HashMap:线程不安全,效率高。允许null键和null值
*
* 2:List,Set,Map等接口是否都继承子Map接口?
* List,Set不是继承自Map接口,它们继承自Collection接口
* Map接口本身就是一个顶层接口
*/
public class HashtableDemo {
public static void main(String[] args) {
// HashMap<String, String> hm = new HashMap<String, String>();
Hashtable<String, String> hm = new Hashtable<String, String>();
 
hm.put("it001", "hello");
// hm.put(null, "world"); //NullPointerException
// hm.put("java", null); // NullPointerException
 
System.out.println(hm);
}
}
========================================================
 
 
原文地址:https://www.cnblogs.com/yejibigdata/p/7835159.html