Java学习笔记 - 第019天
程序员文章站
2022-07-15 08:13:29
...
每日要点
Map
映射(字典)
Map
- HashMap
- TreeMap
/* Map<Integer, String> map = new HashMap<>();
map.put(1, "apple");
map.put(2, "grape");
map.put(100, "shit");
map.put(1, "banana");
System.out.println(map.size());
map.remove(100);
for (Integer key : map.keySet()) {
System.out.println(key + " ---> " + map.get(key));
}*/
// Map<String, String> map = new HashMap<>();
Map<String, String> map = new TreeMap<>();
map.put("apple", "*");
map.put("grape", "葡萄");
map.put("shit", "*");
map.put("banana", "香蕉");
map.put("apple", "苹果");
map.put("eggplant", "茄子");
System.out.println(map.size());
map.remove("shit");
for (String key : map.keySet()) {
System.out.println(key + " ---> " + map.get(key));
}
泛型
泛型(generic) - 让类型不再是程序中的硬代码(hard code)
此处的extends不是继承而是泛型限定 限定T类型必须是Comparable接口的子类型public static <T extends Comparable<T>> void bubbleSort(T[] array)
例子
package com.jack.test;
import java.util.Arrays;
import java.util.Comparator;
class Student implements Comparable<Student>{
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return name + ":" + age;
}
@Override
public int compareTo(Student o) {
return this.name.compareTo(o.name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class Test02 {
public static int max(int x, int y) {
return x > y ? x : y;
}
// 泛型(generic) - 让类型不再是程序中的硬代码(hard code)
// 此处的extends不是继承而是泛型限定 限定T类型必须是Comparable接口的子类型
/**
* 冒泡排序
* @param array 待排序的数组
*/
public static <T extends Comparable<T>> void bubbleSort(T[] array) {
boolean swapped = true;
for (int i = 1;swapped && i < array.length; i++) {
swapped = false;
for (int j = 0; j < array.length - i; j++) {
if (array[j].compareTo(array[j + 1]) > 0) {
T temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
swapped = true;
}
}
}
}
/**
* 冒泡排序
* @param array 待排序的数组
* @param comp Comparator接口对象(比较器)
*/
public static <T> void bubbleSort(T[] array, Comparator<T> comp) {
boolean swapped = true;
for (int i = 1;swapped && i < array.length; i++) {
swapped = false;
for (int j = 0; j < array.length - i; j++) {
if (comp.compare(array[j], array[j + 1]) > 0) {
T temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
swapped = true;
}
}
}
}
public static void main(String[] args) {
Integer[] x = {23, 13, 21, 56, 78, 62};
// String[] x = {"killer", "grape", "apple", "blue"};
Student[] student = {
new Student("Li bai", 56),
new Student("Dachui WANG", 22),
new Student("ZhiZhang", 33)
};
bubbleSort(x);
System.out.println(Arrays.toString(x));
bubbleSort(student, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.getName().compareTo(o2.getName());
}
});
bubbleSort(student, (o1, o2) -> {
return o1.getName().compareTo(o2.getName());
});
System.out.println(Arrays.toString(student));
}
}
策略模式
GoF设计模式 - 策略模式(用一个继承结构封装可变的打折策略)
练习
-
图书
计算机类:7.8折 体育类:8.8折 旅游美食类:6.8折
教材教辅:10元以上每本减2元
漫画:单价超过20元省3元
50元省10元
100元省30元
其他书一律不打折
收了 实际收 优惠多少
书类:
public class Book {
private String isbn;
private String title;
private double price;
private String type;
// GoF设计模式 - 策略模式(用一个继承结构封装可变的打折策略)
private DiscountStrategy discountStrategy;
public Book(String isbn, String title, double price, String type) {
this.isbn = isbn;
this.title = title;
this.price = price;
this.type = type;
}
public void setStrategy(DiscountStrategy discountStrategy) {
this.discountStrategy = discountStrategy;
}
public double getDiscountedPrice() {
return price - discountStrategy.getDiscount(price);
}
public String getIsbn() {
return isbn;
}
public String getTitle() {
return title;
}
public double getPrice() {
return price;
}
public String getType() {
return type;
}
}
打折策略接口:
/**
* 打折策略的接口
* @author Kygo
*
*/
public interface DiscountStrategy {
/**
* 获得折扣
* @param originalPrice 原价
* @return 折扣
*/
public double getDiscount(double originalPrice);
}
百分比折扣策略:
/**
* 百分比折扣策略
* @author Kygo
*
*/
public class PercentageDiscount implements DiscountStrategy {
private double rate;
public PercentageDiscount(double rate) {
this.rate = rate;
}
@Override
public double getDiscount(double originalPrice) {
return originalPrice * rate;
}
}
固定折扣策略:
/**
* 固定折扣策略
* @author Kygo
*
*/
public class FixedDiscount implements DiscountStrategy {
private double fixed;
private double minPrice;
public FixedDiscount(double fixed, double minPrice) {
this.fixed = fixed;
this.minPrice = minPrice;
}
@Override
public double getDiscount(double originalPrice) {
return originalPrice >= minPrice ? fixed : 0;
}
}
分段折扣策略:
/**
* 分段折扣策略
* @author Kygo
*
*/
public class SegmentedDiscount implements DiscountStrategy {
@Override
public double getDiscount(double originalPrice) {
if (originalPrice < 20) {
return 0;
}
else if (originalPrice < 50) {
return 3;
}
else if (originalPrice < 100) {
return 10;
}
else {
return 30;
}
}
}
不打折策略
/**
* 不打折策略
* @author Kygo
*
*/
public class NoDiscount implements DiscountStrategy {
@Override
public double getDiscount(double originalPrice) {
return 0;
}
}
上一篇: Java学习笔记 - 第027天
下一篇: C语言之二进制读写文件