泛型限定
/*
泛型的限定:
? extends E:可以接收E类型或者E类型的子类,上限。
? super E:可以接收E类型或者E类型的父类型。下限。
?:为通配符或者占位符。
由于迭代器中内容的类型不同,使用泛型输出不同类型内容的集合。
*/
import java.util.*;
class GenerationLimits
{
public static void main(String[] args)
{
myGeneration mg=new myGeneration();
//al容器只能添加Person类型的对象。
ArrayList<Person> al=new ArrayList<Person>();
al.add(new Person("Person 001"));
al.add(new Person("Person 002"));
al.add(new Person("Person 003"));
al.add(new Person("Person 004"));
al.add(new Person("Person 005"));
//将容器al放到含有泛型参数的方法中输出。
mg.printArrayList(al);
/*
Iterator itp=al.iterator();
while(itp.hasNext()){
Person p=(Person)itp.next();
System.out.println(p.getName());
}
*/
ArrayList<Student> as=new ArrayList<Student>();
as.add(new Student("Student 001"));
as.add(new Student("Student 002"));
as.add(new Student("Student 003"));
as.add(new Student("Student 004"));
as.add(new Student("Student 005"));
mg.printArrayList(as);
/*
Iterator its=as.iterator();
while(its.hasNext()){
Student s=(Student)its.next();
System.out.println(s.getName());
}
*/
}
}
class Person
{
private String name;
Person(String name){
this.name=name;
}
public String getName(){
return name;
}
}
class Student extends Person
{
Student(String name){
super(name);
}
}
//通过一个方法将不同类型内容(Person及其子类)的集合输出。
class myGeneration
{
//限定ArrayList类型的容器中的数据为Person类及其子类。
public void printArrayList(ArrayList<? extends Person> ag){ //将不同内容的集合泛型化。
//迭代器中获取的内容也为Person类或者其子类。
Iterator<? extends Person> it=ag.iterator();
while(it.hasNext()){
//由于it.next()获得是Person及其子类,Person及其子类中均有getName()方法。
System.out.println(it.next().getName());
}
}
}
泛型限定实例
/*
本实例实现了使用单一Person泛型从而构建了对其所有子类的泛型应用。
*/
import java.util.*;
class GenerationSuper
{
public static void main(String[] args)
{
//构建Student内容的集合对象。
TreeSet<Student> ts=new TreeSet<Student>(new myComp());
//填充内容。
ts.add(new Student("java 001"));
ts.add(new Student("java 008"));
ts.add(new Student("java 003"));
ts.add(new Student("java 006"));
ts.add(new Student("java 002"));
//迭代器从集合容器中取数据。
Iterator<Student> it=ts.iterator();
while(it.hasNext()){
//it.next()返回的即是Student类型的引用(对象)
System.out.println(it.next().getName());
}
//构建Worker内容的集合对象。
TreeSet<Worker> wts=new TreeSet<Worker>(new myComp());
wts.add(new Worker("worker 002"));
wts.add(new Worker("worker 001"));
wts.add(new Worker("worker 003"));
wts.add(new Worker("worker 007"));
wts.add(new Worker("worker 006"));
//迭代器从集合容器中取数据。
Iterator<Worker> wit=wts.iterator();
while(wit.hasNext()){
//wit.next()返回的即是Student类型的引用(对象)
System.out.println(wit.next().getName());
}
}
}
//构建比较器,比较器泛型参数为Person
class myComp implements Comparator<Person>
{
public int compare(Person p1,Person p2){
//按照姓名进行排序。
return p1.getName().compareTo(p2.getName());
}
}
//定义父类。
class Person
{
private String name;
Person(String name){
this.name=name;
}
public String getName(){
return name;
}
}
class Student extends Person
{
Student(String name){
super(name);
}
}
class Worker extends Person
{
Worker(String name){
super(name);
}
}
Map集合
Map:
将键映射到值的对象。一个映射不能包含重复的键;每个键最多只能映射到一个值。
Map中的方法:
1.增加
put(K key, V value) //添加元素时,如果键值相同,后添加的值会覆盖前面添加的值,之后put方法返回被覆盖的值。
putAll(Map<? extends K,? extends V> m)
2.删除
clear()
remove(Object key)
3.获取
entrySet()
get(Object key)
hashCode()
keySet()
size()
values()
4.判断
containsKey(Object key)
containsValue(Object value)
equals(Object o)
isEmpty()
Map
|---Hashtable:底层是哈希表数据结构,不可以存入null健和null值,线程同步。jdk1.0
|---HashMap:底层是哈希表数据结构,允许使用null值和null值,不同步,jdk1.2
|---TreeMap:底层是二叉树数据结构,线程不同步,可以用于给Map中的键进行排序。
Set底层就是使用了Map集合。
Map集合的取出方式:
1.keyset:将map中所有的键存入到set集合中,set具备迭代器,可通过迭代器取出所有的键,再取出值
2.entrySet:
取出Map集合(keySet方式)
:取出所有键
import java.util.*;
class MapKeySet
{
public static void main(String[] args)
{
//创建Map对象,并向Map容器中添加内容。
Map<String,String> m=new HashMap<String,String>();
m.put("01","cs001");
m.put("03","cs003");
m.put("05","cs005");
m.put("02","cs002");
m.put("06","cs006");
//获去Map容器中的所有键值,并存储在Set集合中。
Set<String> ts=m.keySet();
//通过Set集合中的迭代器依次获取键值。再通过依次获取的键值获取键值对应的值。
//该迭代器中只装取String类型的数据。
Iterator<String> it=ts.iterator();
while(it.hasNext()){
//获取键值。it.next()会抛出空值异常,所幸在while循环里面已经进行了限制,
//当it.hasNext为空即退出,此处不会抛空值异常。
String key=it.next();
//通过键值获取键值对应的值。
String value=m.get(key);
//输出。
System.out.println(key+"....."+value);
}
}
}
取出Map中的元素(entrySet方法)
:取出所有键和值
将map转化为set再通过迭代器取出数据。
将Map集合中的映射关系取出,存入到Set集合总。
Map中entrySet方法:
1.构建Map集合容器,并填充内容,该容器泛型为<String,String>(存储两个字符串)2.将Map集合中的内容转化为Set,通过Map容器对象的entrySet方法,该方法返回Set集合的对象。
3.有了set集合,再通过集合的迭代器取出集合中的内容,集合中的内容格式为Map.Entry4.若要获取Map.Entry中的键和值需要方法getKey()和getValue();
/*
将map转化为set再通过迭代器取出数据。
将Map集合中的映射关系取出,存入到Set集合总。
Map中entrySet方法:
1.构建Map集合容器,并填充内容,该容器泛型为<String,String>(存储两个字符串)
2.将Map集合中的内容转化为Set,通过Map容器对象的entrySet方法,该方法返回Set集合的对象。
3.有了set集合,再通过集合的迭代器取出集合中的内容,集合中的内容格式为Map.Entry
4.若要获取Map.Entry中的键和值需要方法getKey()和getValue();
*/
import java.util.*;
class entryMapDemo
{
public static void main(String[] args)
{
//向Map集合容器中存储固定两个字符串。
Map<String,String> m=new HashMap<String,String>();
m.put("03","Map 03");
m.put("02","Map 02");
m.put("05","Map 05");
m.put("01","Map 01");
m.put("04","Map 04");
m.put("06","Map 06");
//将Map集合转化为Set集合。
Set<Map.Entry<String,String>> sm=m.entrySet();
//通过Set集合中的迭代器取出集合中的内容。
Iterator<Map.Entry<String,String>> im=sm.iterator();
while(im.hasNext()){
//entrySet返回的内容为<Map.Entry<String,String>>
Map.Entry<String,String> me=im.next();
//通过getkey()和getValue()取出Map.Entry中的内容。
String key=me.getKey();
String value=me.getValue();
System.out.println(key+"..."+value);
}
}
}
泛型实例
构建学生类(姓名和年龄),学生为键,地址为值,向Hash表中存入该元素
/*
实例要求:
每个学生都有一个对应的地址,学生Student,地址String型
学生属性:姓名,年龄;
注意:姓名和年龄相同为同一学生,保证学生的唯一性。
做法:
1.描述学生
2.定义map容器,将学生做为键,地址作为值传入。
3.获取Map集合中的元素。
*/
import java.util.*;
class MapTest
{
public static void main(String[] args)
{
//构建HashMap。并限定存储的类型<Student,String>
HashMap<Student,String> ms=new HashMap<Student,String>();
ms.put(new Student("java 001",11),"beijing");
ms.put(new Student("java 002",12),"tianjin");
ms.put(new Student("java 003",13),"shanghai");
ms.put(new Student("java 003",13),"nanyang");//重写hashCode和equals保证键值唯一,新值覆盖旧值。
ms.put(new Student("java 007",17),"hainan");
ms.put(new Student("java 006",16),"beijing");
ms.put(new Student("java 005",15),"henan");
//将HashMap转化为Set集合,以便使用集合中的迭代器取出元素
Set<Map.Entry<Student,String>> setCon=ms.entrySet();
//注意迭代器中的泛型限定。
Iterator<Map.Entry<Student,String>> ite=setCon.iterator();
while(ite.hasNext()){
//Map.Entry若不加<Student,String>则下方的me.getKey();和me.getValue();将提示强转。
//此处加入泛型,即限制了存储的数据类型。
Map.Entry<Student,String> me=ite.next(); //此处的泛型需注意。
Student stu=me.getKey();
String addr=me.getValue();
System.out.println("Student:"+stu.getName()+",age:"+stu.getAge()+",address:"+addr);
//System.out.println("Student:"+stu+",address:"+addr);
}
}
}
//为使以后在二叉树结构中存储该类,引入比较器。
class Student implements Comparable<Student>{
private String name;
private int age;
Student(String name,int age){
this.name=name;
this.age=age;
}
public String getName(){
return name;
}
public int getAge(){
return age;
}
//通过重写hashCode和equals保证了键的唯一性。
//如果不写,则同键值的元素也将存入。
public int hashCode(){
return name.hashCode()+age*10;
}
//通过重写hashCode和equals保证了键的唯一性。
//如果不写,则同键值的元素也将存入。
public boolean equals(Object obj){
if(!(obj instanceof Student)){
//在运行时,直接抛出异常,中断程序操作。
throw new ClassCastException("传入的Student类型不匹配");
}
Student s=(Student)obj;
return (this.name.equals(s.name)) && (this.age==s.age);
}
//hash表的存储是无序的。
//有可能使用二叉树存储,所以设定自然存储顺序。
public int compareTo(Student s){
int num=(new Integer(this.getAge())).compareTo(new Integer(s.getAge()));
if(num==0){
return (this.getName()).compareTo(s.getName());
}
return num;
}
}
TreeMap实例
实现对学生的年龄/姓名进行排序:
重写Comparable中的compareTo方法实现对学生的年龄进行排序;
重新自定义比较器StudentComparator实现对学生的姓名进行排序。
/*
需求:对学生对象的年龄/姓名进行排序。
数据是以键值对的形式存在的,所以需使用可以排序的Map集合:TreeMap
重写集合中的compareTo方法,实现对学生的年龄进行排序。
重新自定义比较器StudentComparator,实现对学生的姓名进行排序。
*/
import java.util.*;
class TreeMapDemo
{
public static void main(String[] args)
{
//添加new studentComparator(),让容器自身具有比较性。
TreeMap<Student,String> tm=new TreeMap<Student,String>(new studentComparator());
//在Student类中已经重写了compareTo方法,按照年龄排序,年龄相同,则按照姓名;
//年龄和姓名共同租场键值,若键值相同,新的键值将会覆盖旧的键值。
tm.put(new Student("java 003",13),"beijing");
tm.put(new Student("java 002",12),"hunan");
tm.put(new Student("java 001",14),"shanghai");
tm.put(new Student("java 006",19),"beijing");
tm.put(new Student("java 008",18),"guangdong");
tm.put(new Student("java 007",18),"beijing");
//下边的键值将会覆盖掉"java 008",18中的内容。
tm.put(new Student("java 008",18),"guangxi");
//将TreeMap转化为Set
//EntrySet返回的集合中的内容为:Map.Entry<Student,String>
Set<Map.Entry<Student,String>> s=tm.entrySet();
//使用set中的迭代器,迭代器中的内容为:Map.Entry<Student,String>
Iterator<Map.Entry<Student,String>> it=s.iterator();
while(it.hasNext()){
//迭代器中的内容存放的是Map.Entry<Student,String>
Map.Entry<Student,String> me=it.next();
//获取Map.Entry<Student,String>中的键(Student stu)和值(String addr)。
String addr=me.getValue();
Student stu=me.getKey();
System.out.println("Name:"+stu.getName()+",age:"+stu.getAge()+"....addr:"+addr);
}
}
}
class Student implements Comparable<Student>{
private String name;
private int age;
Student(String name,int age){
this.name=name;
this.age=age;
}
public String getName(){
return name;
}
public int getAge(){
return age;
}
//通过重写hashCode和equals保证了键的唯一性。
//如果不写,则同键值的元素也将存入。
public int hashCode(){
return name.hashCode()+age;
}
//通过重写hashCode和equals保证了键的唯一性。
//如果不写,则同键值的元素也将存入。
public boolean equals(Object obj){
if(!(obj instanceof Student)){
//在运行时,直接抛出异常,中断程序操作。
throw new ClassCastException("传入的Student类型不匹配");
}
Student s=(Student)obj;
return (this.name.equals(s.name)) && (this.age==s.age);
}
//hash表的存储是无序的。
//有可能使用二叉树存储,所以设定自然存储顺序。
public int compareTo(Student s){
int num=(new Integer(this.getAge())).compareTo(new Integer(s.getAge()));
if(num==0){
return (this.getName()).compareTo(s.getName());
}
return num;
}
}
//构建自定义比较器,按照学生姓名进行排序。
//若没有该自定义的比较器,排序将按照上方重写的compareTo方法中定义的进行排序。
//自定义比较器为主要排序方式。
class studentComparator implements Comparator<Student>
{
public int compare(Student s1,Student s2){
int result=(s1.getName()).compareTo(s2.getName());
if(result==0){
return (new Integer(s1.getAge())).compareTo(new Integer(s2.getAge()));
}
return result;
}
}
字符串字符出现次数统计
统计字符串中字符出现的次数,并按照"字符(次数)字符(次数)字符(次数)...的方式输出实例要求:
获取字符串“fwhohsfhwiohfwohfw”字符串中字符出现的次数,并输出:
f(4)w(4)...
分析:字符串和次数有映射关系,使用Map集合。
什么时候使用Map集合
当数据之间存在映射关系时,可以考虑Map集合。
思路:
1.将字符串转化为字符
2.构建集合容器,集合中盛放字符和对应出现的次数
3.通过键判断值是否存在集合中,如果不存在,将键和值存入集合中。
如果存在,将值加1,随后将键和值存入集合中。
4.为输出设定的格式,逐个取出(需要用到迭代器)键和值。
5.使用StringBuilder存放打印的字符串。
/*
实例要求:
获取字符串“fwhohsfhwiohfwohfw”字符串中字符出现的次数,并输出:
f(4)w(4)...
分析:字符串和次数有映射关系,使用Map集合。
什么时候使用Map集合
当数据之间存在映射关系时,可以考虑Map集合。
思路:
1.将字符串转化为字符
2.构建集合容器,集合中盛放字符和对应出现的次数
3.通过键判断值是否存在集合中,如果不存在,将键和值存入集合中。
如果存在,将值加1,随后将键和值存入集合中。
4.为输出设定的格式,逐个取出(需要用到迭代器)键和值。
5.使用StringBuilder存放打印的字符串。
*/
import java.util.*;
class MapDemo2
{
public static void main(String[] args)
{
String s="fwhohsfhwiohfwohfw";
String resultString=charCount(s);
System.out.println(resultString);
}
//类中方法直接使用需static
public static String charCount(String str){
//将字符串转化为字符。
char[] ch=str.toCharArray();
//构建存放字符和次数的集合容器。
TreeMap<Character,Integer> mt=new TreeMap<Character,Integer>();
//循环判断字符对应的次数是否在集合中存在。
for(int x=0;x<ch.length;x++){
Integer value=mt.get(ch[x]);
//如果不存在则将对应的字符和次数存入集合中。
if(value==null){
mt.put(ch[x],1);
}
//如果存在则将次数加1,然后将字符和次数存入集合,覆盖原键值。
else{
value++;
mt.put(ch[x],value);
}
}
//构建StringBuilder存放指定格式的字符串。
StringBuilder sb=new StringBuilder();
Set<Map.Entry<Character,Integer>> entrySet=mt.entrySet();
//引入Set将集合中的内容存入迭代器。
Iterator<Map.Entry<Character,Integer>> it=entrySet.iterator();
//遍历迭代器取出其中的元素并依次存入StringBuilder。
while(it.hasNext()){
Map.Entry<Character,Integer> me=it.next();
Character chs=me.getKey();
Integer times=me.getValue();
sb.append(chs+"("+times+")");
}
//将StringBuilder转化为String输出。
return sb.toString();
}
}