泛型

泛型本质上是参数化类型,也就是说所操作的数据类型被指定为一个参数,即将类型由原来的具体的类型参数化,然后在使用/调用时传入具体的类型,这种参数类型可以用在类、方法和接口中,分别为泛型类、泛型方法、泛型接口

// 泛型类
public class GenericDemo <T>{
private T t; public T getT() {
return t;
} public void setT(T t) {
this.t = t;
}
}
// 测试类
public class Demo {
public static void main(String[] args) {
GenericDemo<String> gd1 = new GenericDemo<>();
gd1.setT("林青霞");
System.out.println(gd1.getT());
System.out.println("--------");
GenericDemo<Integer> gd2 = new GenericDemo<>();
gd2.setT(18);
System.out.println(gd2.getT());
}
}

在这里,测试类中创建泛型类对象时,最好指定数据类型

泛型方法:

public class GenericFunc {
public <T>void show(T t){
System.out.println(t);
}
}
public class Test {
public static void main(String[] args) {
GenericFunc gf = new GenericFunc();
gf.show("Hello");
}
}

泛型接口:

public interface Generic <T>{
void show(T t);
}
public class GenericImpl<T> implements Generic<T>{
@Override
public void show(T t) {
System.out.println(t);
}
}
public class Demo {
public static void main(String[] args) {
Generic<String> gc1 = new GenericImpl<>();
gc1.show("Hello"); Generic<Integer> gc2 = new GenericImpl<>();
gc2.show(100);
}
}

类型通配符

为了表示各种泛型List的父类,可以使用类型通配符

  • 类型通配符:<?>
  • List<?>表示元素类型未知的List,它的元素可以匹配任何的类型
  • 这种带通配符的List仅表示它是各种泛型List的父类,并不能把元素添加到其中
import java.util.ArrayList;
import java.util.List; public class MyGeneric {
public static void main(String[] args) {
List<?> list1 = new ArrayList<String>();
// 不能添加
// list1.add(12);
List<String> list2 = new ArrayList<>();
// 不能添加
list2.add("Hello");
System.out.println(list2);
}
}
public static <T> void test(List<T> c, T t){
// 可以添加
c.add(t);
}

那么类型通配符到底有什么用呢

import java.util.List;

public class MyGeneric {
public void test3(List<?> c){
for (int i = 0; i < c.size(); i++) {
System.out.println(c.get(i));
}
}
}
import java.util.ArrayList;
import java.util.List; public class Demo {
public static void main(String[] args) {
MyGeneric mg = new MyGeneric();
List<String> ls1 = new ArrayList<>();
ls1.add("Hello");
ls1.add("World");
mg.test3(ls1);
System.out.println("-------");
List<Integer> ls2 = new ArrayList<>();
ls2.add(66);
ls2.add(666);
mg.test3(ls2);
}
}

想了下,在不知道类型参数情况下,可以用类型通配符,且还可以使用类型通配符上限,以及类型通配符下限

可变参数:

  • python中用*,而java中用...
  • 可变的参数和python一样都是数组
  • 如果一个方法有多个参数,包含可变参数,可变参数要放在最后

Map集合

Map集合概述:

  • Interface Map<K,V> k是键的类型,V是值的类型
  • 将键映射到值的对象,不能包含重复的键,每个键可以映射到最多一个值

import java.util.HashMap;
import java.util.Map; public class MapDemo {
public static void main(String[] args) {
Map<String,String> map = new HashMap<>();
System.out.println(map.put("abc","123"));
// null
System.out.println(map);
}
}

Map的获取:

  • 根据键获取:get方法,返回V
  • 获得全部键:keySet方法,返回Set集合,因为键是不重复的
  • 获得全部值:values方法,返回Collection,值是可重复的
  • 获取键值对:entrySet方法
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set; public class MapDemo {
public static void main(String[] args) {
Map<String,String> map = new HashMap<>();
map.put("1","机器学习");
map.put("2","深度学期");
map.put("3","自然语言处理");
map.put("4","计算机视觉");
// 通过具体键获得值
String s = map.get("1");
System.out.println(s);
// 获得全部键,返回的是Set
Set<String> strings = map.keySet();
System.out.println(strings);
// 获得全部值,返回的是Collection
Collection<String> values = map.values();
System.out.println(values);
// 通过entrySet方法
Set<Map.Entry<String, String>> entries = map.entrySet();
for(Map.Entry<String, String> me:entries){
System.out.println(me.getKey()+" "+me.getValue());
}
}
}
机器学习
[1, 2, 3, 4]
[机器学习, 深度学期, 自然语言处理, 计算机视觉]
1 机器学习
2 深度学期
3 自然语言处理
4 计算机视觉

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set; public class Demo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
String[] strings = line.split("");
Map<String,Integer> map = new HashMap<>();
for(int i = 0;i<strings.length;i++){
String string = strings[i];
if(map.containsKey(string)){
Integer integer = map.get(string);
integer += 1;
map.put(string,integer);
}else{
map.put(string,1);
}
}
// 遍历
Set<String> strings1 = map.keySet();
for(String s:strings1){
Integer integer = map.get(s);
System.out.print(s+"("+integer+")");
}
}
}

之前学了个集合的工具类Arrays,现在学习另一个工具类Collections

有了这个工具类之后,连ArrayList也可以进行排序了(之前学习的是TreeSet)

// 学生类
public class Student {
private int age;
private String name; public Student(int age, String name) {
this.age = age;
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
}
// 测试类
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator; public class Demo {
public static void main(String[] args) {
Student s1 = new Student(18,"张三");
Student s2 = new Student(19,"李四");
Student s3 = new Student(18,"王五");
ArrayList<Student> arr = new ArrayList<>();
arr.add(s1);
arr.add(s2);
arr.add(s3);
Collections.sort(arr, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
int num1 = o1.getAge()-o2.getAge();
int num2 = num1 == 0?o1.getName().compareTo(o2.getName()):num1;
return num2;
}
});
for(Student s:arr){
System.out.println(s.getAge()+" "+s.getName());
}
}
}

// 实现1
import java.util.ArrayList;
import java.util.Collections; public class PokerDemo {
public static void main(String[] args) {
ArrayList<String> arr = new ArrayList<>();
String [] color = {"","","",""};
String [] point = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
for(int i = 0;i< color.length;i++){
for(int j = 0;j< point.length;j++){
String s = color[i] + point[j];
arr.add(s);
}
}
Collections.shuffle(arr);
ArrayList<String> p1 = new ArrayList<>();
ArrayList<String> p2 = new ArrayList<>();
ArrayList<String> p3 = new ArrayList<>();
ArrayList<String> p4 = new ArrayList<>();
for(int i = 0;i<arr.size();i++){
if(i> arr.size()-3){
p4.add(arr.get(i));
}else if(i%3==0){
p1.add(arr.get(i));
}else if(i%3==1){
p2.add(arr.get(i));
}else if(i%3==2){
p3.add(arr.get(i));
}
}
System.out.println(p1);
}
}
// 实现2
import java.util.*; public class Poker {
public static void main(String[] args) {
// 用HashMap存储一副扑克牌
HashMap<Integer,String> poker = new HashMap<>();
String [] color = {"","","",""};
String [] point = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
int index = 0;
ArrayList<Integer> arrIndex = new ArrayList<>();
for(int i = 0;i<color.length;i++){
for(int j =0;j< point.length;j++){
String s = color[i] + point[j];
poker.put(index,s);
arrIndex.add(index);
index++;
}
}
poker.put(index,"小王");
arrIndex.add(index);
index++;
poker.put(index,"大王");
arrIndex.add(index);
// 用ArrayList存储索引,等会打乱索引 // 打乱排序
Collections.shuffle(arrIndex);
// System.out.println(arrIndex);
// 为了让拿到的牌排好序,使用TreeSet集合
TreeSet<Integer> player1 = new TreeSet<>();
TreeSet<Integer> player2 = new TreeSet<>();
TreeSet<Integer> player3 = new TreeSet<>();
TreeSet<Integer> referee = new TreeSet<>();
// 给玩家发牌
for(int i = 0;i<arrIndex.size();i++){
Integer num = arrIndex.get(i);
if(i>=arrIndex.size()-3){
referee.add(num);
}else if(i%3 == 0){
player1.add(num);
}else if(i%3 == 1){
player2.add(num);
}else if(i%3 == 2){
player3.add(num);
}
}
// 看牌,封装成函数
showPoker("Player1",player1,poker);
showPoker("Player2",player2,poker);
showPoker("Player3",player3,poker);
showPoker("Referee",referee,poker);
}
// 传入进来玩家、扑克牌、Map
public static void showPoker(String name,TreeSet<Integer>ts,HashMap<Integer,String>map){
System.out.println(name+"的牌是:");
System.out.print("\t");
for(Integer i:ts){
String s = map.get(i);
System.out.print(s+", ");
}
System.out.println();
}
}

最新文章

  1. EasyPusher应用
  2. JAVA使用JDBC技术操作SqlServer数据库执行存储过程
  3. August 19th 2016 Week 34th Friday
  4. Level 4 A10: 飞张?
  5. oracle小测试
  6. unity, monodevelop 不安全的代码只会在使用/unsafe编译的情况下出现
  7. proguard 混淆android代码
  8. System.getProperties()对应的key/value列表
  9. C# 以嵌入到窗体的方式打开外部exe
  10. CSS input type=&quot;number&quot;出现上下箭头时解决方案
  11. Java内存模型与Java线程实现原理
  12. 深入理解Java虚拟机01--概述
  13. ARP【地址解析协议】理解
  14. [洛谷P3948]数据结构 题解(差分)
  15. 如何生成唯一的server Id,server_id为何不能重复?
  16. Pandas设置值
  17. Centos/Linux下调整分区大小(以home和根分区为例)
  18. Java面试题(原创)
  19. js上传文件带参数,并且,返回给前台文件路径,解析上传的xml文件,存储到数据库中
  20. RabbitMQ文档翻译——Hello World!(上)

热门文章

  1. Vue报错之&quot; [Vue warn]: Unknown custom element: &lt;wzwzihello&gt; - did you register the component correctly? For recursive components, make sure to provide the &quot;name&quot; option.&quot;
  2. DIANA算法
  3. labview和matlab区别
  4. 2_状态空间_State Space
  5. (6) 结论,摘要与题目_Conclusion, Abstract, and Title【论文写作】
  6. 从0搭建vue后台管理项目到颈椎病康复指南(一)
  7. idea 启动微服务 设置 run dashboard
  8. Jar 包下载以及 maven jar 包配置
  9. JavaScript实现有农历和节气节假日的日历
  10. 将PHPMailer整合到ThinkPHP 3.2 中实现SMTP发送邮件