Browse Source

feat(w8):W8-孟鑫垚-202506010204

main
Mengxinyao 3 weeks ago
parent
commit
b68a7c797e
  1. BIN
      w8/.DS_Store
  2. 30
      w8/work8/.gitignore
  3. 10
      w8/work8/.idea/.gitignore
  4. 6
      w8/work8/.idea/misc.xml
  5. 8
      w8/work8/.idea/modules.xml
  6. 58
      w8/work8/src/Cache.java
  7. 41
      w8/work8/src/Pair.java
  8. 21
      w8/work8/src/Test.java
  9. 11
      w8/work8/work8.iml
  10. 38
      w8/work8/泛型作业

BIN
w8/.DS_Store

Binary file not shown.

30
w8/work8/.gitignore

@ -0,0 +1,30 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
.kotlin
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

10
w8/work8/.idea/.gitignore

@ -0,0 +1,10 @@
# 默认忽略的文件
/shelf/
/workspace.xml
# 已忽略包含查询文件的默认文件夹
/queries/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# 基于编辑器的 HTTP 客户端请求
/httpRequests/

6
w8/work8/.idea/misc.xml

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
w8/work8/.idea/modules.xml

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/work8.iml" filepath="$PROJECT_DIR$/work8.iml" />
</modules>
</component>
</project>

58
w8/work8/src/Cache.java

@ -0,0 +1,58 @@
import java.util.*;
/**
* 泛型缓存类支持基本的增删查操作
* @param <K> 键类型
* @param <V> 值类型
*/
public class Cache<K, V> {
private Map<K, V> map;
private int maxSize;
public Cache(int maxSize) {
this.maxSize = maxSize;
this.map = new LinkedHashMap<K, V>(maxSize, 0.75f, true) {
@Override
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
return size() > maxSize;
}
};
}
public void put(K key, V value) {
map.put(key, value);
}
public V get(K key) {
return map.get(key);
}
public boolean containsKey(K key) {
return map.containsKey(key);
}
public V remove(K key) {
return map.remove(key);
}
public int size() {
return map.size();
}
public void clear() {
map.clear();
}
public Set<K> keySet() {
return map.keySet();
}
public Collection<V> values() {
return map.values();
}
@Override
public String toString() {
return "Cache{" + map + "}";
}
}

41
w8/work8/src/Pair.java

@ -0,0 +1,41 @@
/**
* 泛型 Pair 用于存储两个相关联的对象
* @param <K> 键类型
* @param <V> 值类型
*/
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;
}
/**
* 交换 key value 的值
* 注意仅当 K V 类型可相互赋值时有效
*/
public void swap() {
// 使用 Object 临时存储,强制转换(假设类型兼容)
Object temp = key;
key = (K) value;
value = (V) temp;
}
@Override
public String toString() {
return "Pair{" +
"key=" + key +
", value=" + value +
'}';
}
}

21
w8/work8/src/Test.java

@ -0,0 +1,21 @@
public class Test {
public static void main(String[] args) {
// 测试 Pair
Pair<String, Integer> pair = new Pair<>("hello", 123);
System.out.println("原始: " + pair);
pair.swap();
System.out.println("交换后: " + pair); // 输出: Pair{key=123, value=hello}
// 测试 Cache
Cache<String, Integer> cache = new Cache<>(3);
cache.put("a", 1);
cache.put("b", 2);
cache.put("c", 3);
System.out.println("缓存内容: " + cache);
System.out.println("获取 a: " + cache.get("a"));
cache.put("d", 4); // 超出容量,移除最久未使用的 "a"
System.out.println("添加 d 后: " + cache);
}
}

11
w8/work8/work8.iml

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

38
w8/work8/泛型作业

@ -0,0 +1,38 @@
/**
* 泛型 Pair 类,用于存储两个相关联的对象
* @param <K> 键类型
* @param <V> 值类型
*/
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;
}
/**
* 交换 key 和 value 的值(注意:仅适用于可互换类型的场景)
* 注意:如果 K 和 V 类型不同,交换后可能语义不一致
*/
public void swap() {
// 使用 Object 临时存储,再交换
Object temp = key;
key = value;
value = (V) temp; // 强转,需确保类型兼容
}
@Override
public String toString() {
return "(" + key + ", " + value + ")";
}
}
Loading…
Cancel
Save