//泛型
//在类上使用泛型,要求在使用集合时必须制定泛型
class Student{
private Object obj;
public Student() {
}
public Object getObj() {
return obj;
}
public void setObj(Object obj) {
this.obj = obj;
}
}
//使用泛型后
//E:代表任何一种数据类型,但是<>中不一定写E,任意的字母都可以
//<大写字母>相当于声明泛型
//在类上确定的泛型可以直接在方法上使用
class Student1<E>{
private E obj;
public Student1() {
}
public E getObj() {
return obj;
}
public void setObj(E obj) {
this.obj = obj;
}
}
class Tool{
private String name;
public Tool() {
super();
// TODO Auto-generated constructor stub
}
public Tool(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class Iphone extends Tool{
public Iphone(String name) {
super(name);
}
}
class Computer extends Tool{
public Computer(String name) {
super(name);
}
}
public class Demo1 {
public static void main(String[] args) {
//---------------使用泛型前------------
Iphone i=new Iphone("huawei");
Computer c=new Computer("lianxiang");
Student s1=new Student();
s1.setObj(i);//相当于obj=new Iphone("huawei");多态
//获取工具
Object obj=s1.getObj();//obj=new Iphone("huawei");多态
//向下转型
Iphone i2=(Iphone)obj;//向下转型相当于i2=new Iphone("huawei"),也就是,先向上转型,再向下转型,还是那个对象不变
Computer c2=(Computer)obj;//c2=new Iphone("huawei");这是一个错误,是运行时错误
//---------------使用泛型后------------
Iphone i4=new Iphone("huawei");
Computer c4=new Computer("lianxiang");
Student1<Computer> s4=new Student1<>();
// s4.setObj(i4);当泛型指定为Computer,再往里面放其他类型就会有编译错误
s4.setObj(c4);//通过设置泛型,参数的类型已经确定了
Computer c3=s4.getObj();
}
}
import java.util.*;
/*
* 在方法上使用泛型
*/
class Dog<E>{
//1当方法上的泛型与类的泛型保持一致
public E show(E e)
{
return e;
}
//2方法拥有属于自己的泛型
/*
* 注意:泛型在使用之前一定要进行声明
* 声明的方式:在修饰符的后面添加<大写字母>
* 作用:让方法与方法内部的泛型保持一致
*/
public <F> void play(F f)
{
ArrayList<F> list=null;
}
//3静态方法上使用泛型
//静态方法无法使用类上的泛型,类上的泛型必须通过创建对象使用
//静态方法想使用泛型要自己独立使用
public static <W> void song(W a){
}
}
public class Demo3 {
public static void main(String[] args) {
//1方法上的泛型与类的泛型保持一致
Dog<String> dog=new Dog<>();
dog.show("haha");
//2
dog.play("haha");
//3
Dog.<String>song("haha");
}
}
package day18;
/*
* 泛型应用于接口上
*/
public class Demo4 {
public static void main(String[] args)
{
Cat cat=new Cat();
cat.show("haha");
Pig<String> pig=new Pig<>();
pig.show("haha");
}
}
interface Inter<E>{
public abstract void show(E e);
}
//接口上使用了泛型
//子类使用泛型的情况
//第一种情况:接口有泛型,子类没有遵守对应的泛型,而是指定了具体的类型,使这个未知类型不再有效
/*在实现的接口位置必须指定一个具体的泛型
*
* 方法使用泛型的情况:
* 1如果是重写的方法,泛型与接口一致
* 2如果是子类自己的方法,可以与接口一致,也可以有自己的泛型
*/
class Cat implements Inter<String>{
//重新父接口中的方法
public void show(String e) {
System.out.println("show");
}
//自己的方法
public <F> void play(F f) {
}
}
//第二种情况:接口有泛型,子类遵守了对应的泛型
class Pig<E> implements Inter<E>{
@Override
public void show(E e) {
System.out.println("haha");
}
}
import java.util.*;
/*
* 输出为{}是map
* 输出为[]是list
* Map:接口
* HashMap:底层是哈希表,线程不安全的
* TreeMap:底层是二叉树,线程不安全的
*
* Map:本身是接口,存储的是键值对,一个元素就是一个键(key)值(value)对,key必须是唯一的,值随意,即可以重复
* Collection:直接存储的是值
*
*/
public class Demo7 {
public static void main(String[] args) {
//这里是同时给键值指定泛型
//一般充当key的是字符串或者是包装类
Map<String,String> map=new HashMap<String,String>();
//介绍Map接口的方法
//1.增加
//V put(K key,V value) 增加一个键值对
map.put("01", "java");//增加一个键值对
map.put("02", "php");
map.put("03", "ios");
//System.out.println(map.put("03", "ios"));
//put方法的返回值:
//如果当前的key之前没有添加过,返回null,如果当前的key之前已经添加过,返回之前的值
map.put("03", "BigData");//键相同的话。后面的键值对会覆盖前面的键值对
System.out.println(map);//输出:{01=java, 02=php, 03=BigData}
//void putAll(Map<? extends K,? extends V> map) 增加多个
//2.删除
//V remove(Object key) 根据key删除元素,该方法返回被删掉的元素
// System.out.println(map.remove("01"));
// System.out.println(map);
//void clear() 删除全部
// map.clear();
//3.获取
//V get(Object key) 根据key查找元素
System.out.println(map.get("01"));
//int size() 获取键值对的个数
System.out.println(map.size());
//Set<K> keySet() 遍历方法一
//Set<Map.Entry<K,V>> entrySet() 遍历方法二
//4.常用的判断
//boolean isEmpty()
System.out.println(map.isEmpty());
//boolean containsKey(K key) 是否包含当前的key
System.out.println(map.containsKey("01"));
//boolean containsValue(V value) 是否包含当前的value
System.out.println(map.containsValue("php"));
}
}
import java.util.*;
import java.util.Map.*;
//Set<K> keySet() 遍历方法一
//第一种
// 原理:先得到所有的key放到一个set中,利用set的迭代器进行遍历,得到key,再利用key获取value
//
//方法二
//Set<Map.Entry<K,V>> entrySet()遍历
//原理:先得到所有的entry,放入一个Set中,利用Set的迭代器进行遍历得到entry实体,再利用entry的方法获取key和value
//Map.Entry:其实Entry是Map的一个内部接口(可以参考内部类)
//相当于Map里的每一个键值对都是一个Entry,Map是由Entry组成的
//Map.Entry,Entry对象仅在迭代时有效,在迭代的时候可以直接通过这个对象.setValue()去修改Map中的值
//Entry的方法:getValue() getKey() setValue()
public class Demo8 {
public static void main(String[] args) {
Map<String,String> map=new HashMap<String,String>();
map.put("01", "java");//增加一个键值对
map.put("02", "php");
map.put("03", "ios");
map.put("03", "BigData");
System.out.println(map);
//使用KeySet实现遍历
//获取装着key的Set
Set set1=map.keySet();
//遍历Set,获取多有的Key
Iterator<String>it1 =set1.iterator();
while(it1.hasNext()) {
String key=it1.next();
System.out.println(key+" "+map.get(key));
}
//使用entrySet实现遍历
Set<Map.Entry<String,String>> set2=map.entrySet();
//遍历set,获取所有的entry
Iterator<Map.Entry<String,String>> it2=set2.iterator();
while(it2.hasNext()) {
Map.Entry<String, String> entry=it2.next();
//在迭代期间可以使用entry的value方法的Map的值进行修改
//entry.setValue("bingbing");
System.out.println(entry.getKey()+" "+entry.getValue());
}
}
}
import java.util.*;
import java.util.Map.*;
/*
* HashMap:去重,因为HashMap的底层与HashSet的底层实现一样,只是对HashMap去重的时候,操作的是key
* 作业:实现String和Person对象的去重
*/
class Person{
String name;
int age;
Person(String name,int age){
this.name=name;
this.age=age;
}
Person(){}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
public boolean equals(Object obj)//传进来的是Person对象
{
if(!(obj instanceof Person))
throw new ClassCastException("类型不对");
Person p=(Person)obj;
if(this.name==p.name && this.age==p.age)
return true;
else
return false;
}
public int hashCode() {
return name.hashCode()+age*10000;
}
}
public class Demo9 {
public static void main(String[] args) {
//HashMap:无序,去重。它会根据键去重,也就是重写hashCode()和equals方法()
//-----------------------1键为String该类的hashCode(),equals()已经重写过,所以直接用就行了
HashMap<String,Integer> mapStr=new HashMap<String,Integer>();
mapStr.put("hello",1);
mapStr.put("world",2);
mapStr.put("and",3);
mapStr.put("I love",4);
mapStr.put("I love",5);
mapStr.put("my Life",5);
System.out.println(mapStr);
//第一种遍历方法
Set<String> keys=mapStr.keySet();//得到所有键组成的Set
Iterator<String> it=keys.iterator();//用构造器遍历这个Set
while(it.hasNext())//遍历键的集合
{
String key=it.next();
Integer value=mapStr.get(key);
System.out.println(key+" "+value);
}
System.out.println("-------");
//第二种遍历方法,用Set存Map中的每一个键值对:Map.Entry
Set<Map.Entry<String, Integer>> setStr1=mapStr.entrySet();//把每一个Entry存Set里
Iterator<Map.Entry<String, Integer>> it2=setStr1.iterator();//遍历这个Set的,每一项
while(it2.hasNext())
{
Map.Entry<String, Integer> ky=it2.next();//得到一个Set中的值
String key=ky.getKey();
Integer value=ky.getValue();
System.out.println(key+" "+value);
}
//---------------------------1键为Person该类的hashCode(),equals()需要重写
HashMap<Person,String> hashPer=new HashMap<Person,String>();
hashPer.put(new Person("Killy",18),"boy");
hashPer.put(new Person("Jack",19),"trans");
hashPer.put(new Person("Killy",18),"boy");
hashPer.put(new Person("Zoey",20),"boy");
hashPer.put(new Person("Killy",18),"tomBoy");
System.out.println(hashPer);
//第一种遍历:遍历键
Set<Person> keys3=hashPer.keySet();//键的集合
Iterator<Person> it3=keys3.iterator();//迭代键
while(it3.hasNext()) {
Person key=it3.next();
String value=hashPer.get(key);
System.out.println(key+" "+value);
}
System.out.println("-----------");
//第二种遍历:遍历Map.Entry
Set<Map.Entry<Person, String>> ky2=hashPer.entrySet();//把Map.Entry存到Set集合
Iterator<Map.Entry<Person, String>> it4=ky2.iterator();//得到Set的一每元素,并迭代
while(it4.hasNext()) {
Map.Entry<Person, String> ky=it4.next();//得到每一个Set中存的值
Person key=ky.getKey();
String value=ky.getValue();
System.out.println(key+" "+value);
}
}
}
import java.util.*;
import java.util.Map.*;
/*
* TreeMap:去重和排序,底层与TreeSet一致,在进行排序去重的时候就是去操作key
* 作业:分别使用默认方法和手动方法实现String(key)和People(key)对象的排序去重
*/
class ComWithString implements Comparator<String>//String的构造器,实现手动排序
{
@Override
public int compare(String o1, String o2) {
int num=-(o1.compareTo(o2));//降序
return num;
}
}
class ComWithPeople implements Comparator<People>
{
public int compare(People o1, People o2) {
int num=o1.age-o2.age;
return num==0?o1.name.compareTo(o2.name):num;
}
}
class People implements Comparable<People>//实现该接口进行默认排序,这里指定泛型People,省很多事
{
String name;
int age;
People(String name,int age){
this.name=name;
this.age=age;
}
People(){}
@Override
public String toString() {
return "People [name=" + name + ", age=" + age + "]";
}
@Override
public int compareTo(People o) {
int num=this.name.compareTo(o.name);//该方法返回int型正负数,相等时返回0
return num==0?this.age-o.age:num;
}
}
public class Demo10 {
public static void main(String[] args) {
//HsahMap:实现去重排序,是对键的处理
//方法一自动排序:实现Comparable接口,重写CompareTo()方法
//方法二手动排序:实现Comparator接口,重写Compare()方法
//String方法一自动排序: 实现Comparable接口,重写CompareTo()方法,这个该类已经实现了(升序),直接用即可
TreeMap<String,Integer> tm1=new TreeMap<String,Integer>();
tm1.put("aaa",1);
tm1.put("aab",2);
tm1.put("aac",3);
tm1.put("abc",4);
tm1.put("bca",5);
tm1.put("aaa",2);
System.out.println(tm1);//输出{aaa=2, aab=2, aac=3, abc=4, bca=5}
//String方法二手动排序: 实现Comparator接口,重写Compare()方法,这里写成是降序的
//手动排序的优先级高于自动排序
ComWithString com1=new ComWithString();//创建比较器对象
TreeMap<String,Integer> tm2=new TreeMap<String,Integer>(com1);
//将比较器对象传给TreeMap的构造方法
tm2.put("aaa",1);
tm2.put("aab",2);
tm2.put("aac",3);
tm2.put("abc",4);
tm2.put("bca",5);
tm2.put("aaa",2);
System.out.println(tm2);//输出:{bca=5, abc=4, aac=3, aab=2, aaa=2}
//People方法一自动排序: 实现Comparable接口,重写CompareTo()方法,这里写成先按名字排序,再按年龄排序
TreeMap<People,String> treePeo=new TreeMap<People,String>();
treePeo.put(new People("aaaa",18),"boy");
treePeo.put(new People("bbbb",19),"trans");
treePeo.put(new People("bbbb",18),"boy");
treePeo.put(new People("aaaa",18),"boy");
treePeo.put(new People("cccc",18),"tomBoy");
System.out.println(treePeo);
//Peron方法二手动排序: 实现Comparator接口,重写Compare()方法,这里写成是先按年龄排序,再按名字排序
//手动排序的优先级高于自动排序
ComWithPeople com2=new ComWithPeople();
TreeMap<People,String> treePeo2=new TreeMap<People,String>(com2);
treePeo2.put(new People("aaaa",18),"boy");
treePeo2.put(new People("bbbb",19),"trans");
treePeo2.put(new People("bbbb",18),"boy");
treePeo2.put(new People("aaaa",18),"boy");
treePeo2.put(new People("cccc",20),"tomBoy");
System.out.println(treePeo2);
}
}
import java.util.*;
import java.util.Map.*;
//作业: *练习题二: 1.josgjsjagwajsogiseafgjwsjgvoier
//要求:1.转化成字符串 : a(字符的个数)b()c().. 2.区分大小写 3.只读取字母
public class Homework1 {
public static void main(String[] args) {
String s="josgjsjagwajsogiseafgjwsjgvoier";
TreeMap<Character,Integer> words=new TreeMap<Character,Integer>();//基本类型的包装类实现了方法
for(int i=0;i<=s.length()-1;i++)
{
Character tmp=s.charAt(i);
if((tmp>='a'&&tmp<='z') || (tmp>='A'&&tmp<='A'))//如果是大小写字母的话
{
if(words.containsKey(tmp))
{
Integer num=words.get(tmp);
words.put(tmp, num+1);
}
else
words.put(tmp,1);
}
}
//遍历TreeMap
Set<Map.Entry<Character,Integer>> kys=words.entrySet();
Iterator<Map.Entry<Character,Integer>> it =kys.iterator();
while(it.hasNext())
{
Map.Entry<Character,Integer> ky=it.next();
Character key=ky.getKey();
Integer value=ky.getValue();
System.out.print(key+"("+value+")");
}
}
}