3 changed files with 133 additions and 0 deletions
@ -0,0 +1,62 @@ |
|||||
|
import java.util.Optional; |
||||
|
|
||||
|
public class Main { |
||||
|
|
||||
|
public static void main(String[] args) { |
||||
|
System.out.println("===== Pair<K,V> 测试 ====="); |
||||
|
testPair(); |
||||
|
|
||||
|
System.out.println(); |
||||
|
System.out.println("===== Cache<K,V> 测试 ====="); |
||||
|
testCache(); |
||||
|
} |
||||
|
|
||||
|
static void testPair() { |
||||
|
// 基本用法
|
||||
|
Pair<String, Integer> p1 = new Pair<>("age", 20); |
||||
|
System.out.println("原始: " + p1); |
||||
|
|
||||
|
Pair<Integer, String> p2 = p1.swap(); |
||||
|
System.out.println("swap: " + p2); |
||||
|
|
||||
|
// 不同类型组合
|
||||
|
Pair<String, Boolean> p3 = new Pair<>("isStudent", true); |
||||
|
Pair<Boolean, String> p4 = p3.swap(); |
||||
|
System.out.println("原始: " + p3 + " → swap: " + p4); |
||||
|
} |
||||
|
|
||||
|
static void testCache() { |
||||
|
// 容量为 3 的 LRU 缓存
|
||||
|
Cache<String, String> cache = new Cache<>(3); |
||||
|
|
||||
|
cache.put("a", "Apple"); |
||||
|
cache.put("b", "Banana"); |
||||
|
cache.put("c", "Cherry"); |
||||
|
System.out.println("初始状态: " + cache); |
||||
|
|
||||
|
// 访问 a,使其成为最近使用
|
||||
|
cache.get("a"); |
||||
|
|
||||
|
// 放入第4个,b 是最久未使用的,会被淘汰
|
||||
|
cache.put("d", "Durian"); |
||||
|
System.out.println("放入 d 后 (b 应被淘汰): " + cache); |
||||
|
System.out.println("b 是否存在: " + cache.containsKey("b")); // false
|
||||
|
System.out.println("a 是否存在: " + cache.containsKey("a")); // true
|
||||
|
|
||||
|
// Optional 用法演示
|
||||
|
Optional<String> val = cache.get("c"); |
||||
|
val.ifPresent(v -> System.out.println("获取 c: " + v)); |
||||
|
cache.get("x").ifPresentOrElse( |
||||
|
v -> System.out.println("获取 x: " + v), |
||||
|
() -> System.out.println("获取 x: 不存在(缓存未命中)") |
||||
|
); |
||||
|
|
||||
|
// 整数缓存
|
||||
|
Cache<Integer, Double> numCache = new Cache<>(2); |
||||
|
numCache.put(1, 3.14); |
||||
|
numCache.put(2, 2.71); |
||||
|
numCache.put(3, 1.41); // 1 被淘汰
|
||||
|
System.out.println("数值缓存: " + numCache); |
||||
|
System.out.println("key=1 存在: " + numCache.containsKey(1)); // false
|
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,26 @@ |
|||||
|
/** |
||||
|
* 泛型键值对 Pair<K, V> |
||||
|
* 支持 swap() 方法返回键值互换后的新 Pair |
||||
|
*/ |
||||
|
public class Pair<K, V> { |
||||
|
private K key; |
||||
|
private V value; |
||||
|
|
||||
|
public Pair(K key, V value) { |
||||
|
this.key = key; |
||||
|
this.value = value; |
||||
|
} |
||||
|
|
||||
|
public K getKey() { return key; } |
||||
|
public V getValue() { return value; } |
||||
|
|
||||
|
/** 返回键值互换的新 Pair<V, K> */ |
||||
|
public Pair<V, K> swap() { |
||||
|
return new Pair<>(value, key); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String toString() { |
||||
|
return "Pair(" + key + ", " + value + ")"; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,45 @@ |
|||||
|
package org.example; |
||||
|
|
||||
|
import java.io.BufferedReader; |
||||
|
import java.io.FileNotFoundException; |
||||
|
import java.io.FileReader; |
||||
|
import java.io.IOException; |
||||
|
|
||||
|
public class ScoreReader { |
||||
|
public static void main(String[] args) { |
||||
|
|
||||
|
int sum = 0; |
||||
|
int count = 0; |
||||
|
|
||||
|
// try-with-resources:自动关闭流,不需要手动 br.close()
|
||||
|
try (BufferedReader br = new BufferedReader(new FileReader("scores.txt"))) { |
||||
|
|
||||
|
String line; |
||||
|
while ((line = br.readLine()) != null) { |
||||
|
try { |
||||
|
sum += Integer.parseInt(line.trim()); |
||||
|
count++; |
||||
|
} catch (NumberFormatException e) { |
||||
|
// 处理数字格式错误
|
||||
|
System.out.println("跳过无效数据:" + line); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
if (count > 0) { |
||||
|
double average = (double) sum / count; |
||||
|
System.out.println("总分:" + sum); |
||||
|
System.out.println("人数:" + count); |
||||
|
System.out.printf("平均分:%.2f%n", average); |
||||
|
} else { |
||||
|
System.out.println("文件中没有有效成绩。"); |
||||
|
} |
||||
|
|
||||
|
} catch (FileNotFoundException e) { |
||||
|
// 处理文件不存在
|
||||
|
System.out.println("错误:找不到文件 scores.txt,请检查路径。"); |
||||
|
} catch (IOException e) { |
||||
|
// 处理读取错误
|
||||
|
System.out.println("错误:读取文件时发生异常:" + e.getMessage()); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue