java中 Set与Map排序输出到Writer详解及实例
作者:10km 时间:2022-05-01 07:23:56
java中 Set与Map排序输出到Writer详解及实例
一般来说java.util.Set,java.util.Map输出的内容的顺序并不是按key的顺序排列的,但是java.util.TreeMap,java.util.TreeSet的实现却可以让Map/Set中元素内容以key的顺序排序,所以利用这个特性,可以将Map/Set转为TreeMap,TreeSet然后实现排序输出。
以下是实现的代码片段:
/**
* 对{@link Map}中元素以key排序后,每行以{key}={value}形式输出到{@link Writer}<br>
* map为空或null时则不向writer写入任何内容
* @param map
* @param writer 为null抛出{@link IllegalArgumentException}
* @throws IOException
*/
public static void storeSortedMap(Map<String,String> map,Writer writer) throws IOException {
if(null==writer)
throw new IllegalArgumentException("the arugment 'writer' must not be null ");
TreeMap<String, String> sortedMap = new TreeMap<String,String>();
if(null!=map)
sortedMap.putAll(map);
BufferedWriter bw=(writer instanceof BufferedWriter)?(BufferedWriter)writer
: new BufferedWriter(writer);
for (Entry<String,String> e:sortedMap.entrySet()) {
bw.write(e.getKey() + "=" + e.getValue());
bw.newLine();
}
bw.flush();
}
/**
* 对 {@link Collection}中元素排序后(去除重复),元素分行输出到{@link Writer}<br>
* collection为空或null时则不向writer写入任何内容
* @param collection
* @param writer 为null抛出{@link IllegalArgumentException}
* @throws IOException
*/
public static void storeSortedSet(Collection<String> collection,Writer writer) throws IOException {
if(null==writer)
throw new IllegalArgumentException("the arugment 'writer' must not be null ");
TreeSet<String> sortedSet = new TreeSet<String>();
if(null!=collection)
sortedSet.addAll(collection);
BufferedWriter bw=(writer instanceof BufferedWriter)?(BufferedWriter)writer
: new BufferedWriter(writer);
for (String e:sortedSet) {
bw.write(e);
bw.newLine();
}
bw.flush();
}
调用示例如下:
Map<String,String> map;
//....
storeSortedMap(map,new FileWriter(new File("c:\\id.txt")));
Set<String,String> set;
//....
storeSortedSet(set,new FileWriter(new File("c:\\trainval.txt")));
生成结果已经是排序的了
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
标签:java,Set,Map,Writer
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
idea输入sout无法自动补全System.out.println()的问题
2023-11-28 21:34:03
![](https://img.aspxhome.com/file/2023/3/60603_0s.jpg)
Java多线程编程之读写锁ReadWriteLock用法实例
2021-10-13 17:01:14
![](https://img.aspxhome.com/file/2023/0/65620_0s.png)
Java如何实现自定义异常类
2023-06-21 23:44:01
Spring-boot 2.3.x源码基于Gradle编译过程详解
2022-04-07 15:41:36
![](https://img.aspxhome.com/file/2023/2/66032_0s.png)
时间处理函数工具分享(时间戳计算)
2021-07-24 05:06:18
Java技能点之SimpleDateFormat进行日期格式化问题
2023-09-09 01:50:26
![](https://img.aspxhome.com/file/2023/9/58339_0s.jpg)
c#实现的操作oracle通用类
2022-01-31 15:08:37
Spring MVC项目中的异常处理详解
2021-12-22 09:19:20
![](https://img.aspxhome.com/file/2023/9/69929_0s.jpg)
SpringBoot多数据源切换实现代码(Mybaitis)
2021-11-07 11:15:50
![](https://img.aspxhome.com/file/2023/6/92716_0s.png)
详解SpringBoot JPA常用注解的使用方法
2023-12-09 17:10:31
![](https://img.aspxhome.com/file/2023/5/72535_0s.png)
Java微信授权登陆的实现示例
2021-12-11 22:03:04
![](https://img.aspxhome.com/file/2023/1/62691_0s.png)
浅析java中Integer传参方式的问题
2023-10-27 03:35:43
Java线程休眠的5种方法
2022-02-21 04:49:48
![](https://img.aspxhome.com/file/2023/9/94219_0s.webp)
java + dom4j.jar提取xml文档内容
2023-11-29 03:55:10
Mybatis对mapper的加载流程深入讲解
2022-06-01 12:33:04
![](https://img.aspxhome.com/file/2023/7/62207_0s.png)
Java Web开发过程中登陆模块的验证码的实现方式总结
2022-01-29 19:33:16
![](https://img.aspxhome.com/file/2023/8/75248_0s.png)
JDBC连接mysql乱码异常问题处理总结
2023-11-06 14:57:27
![](https://img.aspxhome.com/file/2023/8/67068_0s.png)
Java中String类的常用方法总结
2021-11-26 10:39:20
Spring集成jedis的配置与使用简单实例
2023-07-02 04:11:39
spring mvc+localResizeIMG实现HTML5端图片压缩上传
2023-07-11 15:31:51