Java语言的字典打开后读取其中的字典的内容,要求避免字典的内容出现重复的代码的实现的方法
看下哈
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class DictionaryReader<T> {
private String filePath;
public DictionaryReader(String filePath) {
this.filePath = filePath;
}
public Map<String, T> readDictionary() throws IOException {
Map<String, T> dictionary = new HashMap<>();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(filePath));
String line;
while ((line = reader.readLine()) != null) {
// 解析每一行的数据
T entry = parseLine(line);
if (entry != null) {
dictionary.put(getKey(entry), entry);
}
}
} finally {
if (reader != null) {
reader.close();
}
}
return dictionary;
}
// 解析一行数据并返回相应的对象
@SuppressWarnings("unchecked")
private T parseLine(String line) {
// 使用反射机制创建对象
try {
Class<T> clazz = (Class<T>) Class.forName("YourEntryClass");
T entry = clazz.getDeclaredConstructor().newInstance();
// TODO: 解析数据行,设置对象的属性值
return entry;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
// 根据对象获取唯一键值
private String getKey(T entry) {
// TODO: 根据对象的属性值生成唯一键值
return null;
}
}
在Java语言中,可以使用 HashMap
来实现字典(键值对集合)。为了避免代码中出现重复的部分,你可以将读取字典内容的逻辑封装为一个方法,然后在需要读取字典内容的地方调用这个方法。
以下是一个示例代码,演示如何读取字典内容并避免代码的重复:
import java.util.HashMap;
import java.util.Map;
public class DictionaryReader {
public static void main(String[] args) {
Map<String, String> dictionary = createDictionary();
readDictionary(dictionary);
}
public static Map<String, String> createDictionary() {
// 创建字典
Map<String, String> dictionary = new HashMap<>();
dictionary.put("key1", "value1");
dictionary.put("key2", "value2");
dictionary.put("key3", "value3");
return dictionary;
}
public static void readDictionary(Map<String, String> dictionary) {
// 读取字典内容
for (Map.Entry<String, String> entry : dictionary.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println("Key: " + key + ", Value: " + value);
}
}
}
在上述示例中,createDictionary()
方法用于创建字典并返回创建的字典对象。readDictionary()
方法接受一个 Map
对象作为参数,并读取字典内容。这样,无论你在程序的哪个位置需要读取字典内容,都可以调用 readDictionary()
方法而避免重复的代码。
注意,示例中使用了 HashMap
类来实现字典,你可以根据实际需求选择不同的 Map
实现类。
通常开发者都是利用 Executors 提供的通用线程池创建方法,去创建不同配置的线程池,主要区别在于不同的ExecutorService 类型或者不同的初始参数。
Executors 目前提供了 5 种不同的线程池创建配置:
(1)、newCachedThreadPool()
,它是一种用来处理大量短时间工作任务的线程池,具有几个鲜明特点:它会试图缓存线程并重用,当无缓存线程可用时,就会创建新的工作线程;如果线程闲置的时间超过 60 秒,则被终止并移出缓存;长时间闲置时,这种线程池,不会消耗什么资源。其内部使用 SynchronousQueue 作为工作队列。
(2)、newFixedThreadPool(int nThreads)
,重用指定数目(nThreads)的线程,其背后使用的是无界的工作队列,任何时候最多有 nThreads 个工作线程是活动的。这意味着,如果任务数量超过了活动队列数目,将在工作队列中等待空闲线程出现;如果有工作线程退出,将会有新的工作线程被创建,以补足指定的数目 nThreads。
(3)、newSingleThreadExecutor()
,它的特点在于工作线程数目被限制为 1,操作一个无界的工作队列,所以它保证了所有任务的都是被顺序执行,最多会有一个任务处于活动状态,并且不允许使用者改动线程池实例,因此可以避免其改变线程数目。
(4)、newSingleThreadScheduledExecutor()
和newScheduledThreadPool(int corePoolSize)
,创建的是个 ScheduledExecutorService
,可以进行定时或周期性的工作调度,区别在于单一工作线程还是多个工作线程。
(5)、newWorkStealingPool(int parallelism)
,这是一个经常被人忽略的线程池,Java 8 才加入这个创建方法,其内部会构建ForkJoinPool,利用Work-Stealing算法,并行地处理任务,不保证处理顺序。
TIP:另外,线程池这个定义就是个容易让人误解的术语,因为 ExecutorService 除了通常意义上“池”的功能,还提供了更全面的线程管理、任务提交等方法。
对于避免重复的代码实现,我们可以考虑使用封装和继承来实现代码复用,以及使用设计模式等解决方案。
对于涉及到字典或Map的情况下,我们可以考虑将Map的读取操作封装成方法,这样在需要读取Map内容时只需要调用这个方法即可,避免了代码的复制和粘贴。
示例代码如下:
public class MapUtil {
public static Object getMapValue(Map<String, Object> map, String key) {
if (map == null || key == null) {
return null;
}
return map.get(key);
}
}
在需要读取Map内容的地方,只需要调用 MapUtil.getMapValue
方法即可:
Map<String, Object> map = new HashMap<>();
map.put("name", "Tom");
map.put("age", 18);
String name = (String) MapUtil.getMapValue(map, "name");
int age = (int) MapUtil.getMapValue(map, "age");
这样,即使需要读取多个Map的内容,也可以重复使用 MapUtil.getMapValue
方法,避免了代码的重复。
另外,在使用自定义的对象作为Map的键时,需要注意对象的 equals()
和 hashCode()
方法的定义规则,保证插入到Map中的对象不会再改变了。如果这个自定义对象是不可变的,那么它已经满足了作为键的条件,因为当它创建之后就已经不能改变了。如果这个对象是可变的,当属性值改变了后,它的hash也会相应改变,这时get的时候将找不到原对象了。因此在使用自定义对象作为Map的键时,需要保证对象的属性是不可变的。
参考资料中还提供了Map的遍历方法,可以在遍历Map的同时获取数组中最大值和最大值的数组下标,具体代码可以参考参考资料中的示例。