10 changed files with 223 additions and 0 deletions
Binary file not shown.
@ -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 |
||||
@ -0,0 +1,10 @@ |
|||||
|
# 默认忽略的文件 |
||||
|
/shelf/ |
||||
|
/workspace.xml |
||||
|
# 已忽略包含查询文件的默认文件夹 |
||||
|
/queries/ |
||||
|
# Datasource local storage ignored files |
||||
|
/dataSources/ |
||||
|
/dataSources.local.xml |
||||
|
# 基于编辑器的 HTTP 客户端请求 |
||||
|
/httpRequests/ |
||||
@ -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> |
||||
@ -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> |
||||
@ -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 + "}"; |
||||
|
} |
||||
|
} |
||||
@ -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 + |
||||
|
'}'; |
||||
|
} |
||||
|
} |
||||
@ -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); |
||||
|
} |
||||
|
} |
||||
@ -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> |
||||
@ -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…
Reference in new issue