Map排序,获取map的第一值,根据value取key等操作(数据预处理)
2017-03-01 10:45
936 查看
这里为了以后自己再做数据预处理使用,做一下笔记。
package deal; import java.io.File; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import fileutil.FileUtil; /* * author:合肥工业大学 管院学院 钱洋 * email:1563178220@qq.com */ public class GetProductData { ArrayList<Document> docs; public Map<String, String> ProductMap; HashMap<String, Object> productandproduct; public GetProductData(){ docs = new ArrayList<Document>(); //一个产品对应的另外21个产品 ProductMap = new HashMap<String, String>(); productandproduct=new HashMap<String, Object>(); } public void readDocs(String docsPath){ /** * @author qianyang *获取目录下面文件的绝对目录,并循环 *即对每一篇文档进行处理,获得的ProductMap, indexToTermMap, termCountMap是针对所有文档合并的 */ for(File docFile : new File(docsPath).listFiles()){ Document doc = new Document(docFile.getAbsolutePath(), ProductMap,productandproduct); docs.add(doc); } } public static class Document { private String docName; int[] docWords; /** * @author qianyang * 这个函数的输入为docName,输出为ProductMap,indexToTermMap,termCountMap */ public Document(String docName, Map<String, String> ProductMap,HashMap<String, Object> productandproduct){ this.docName = docName; //Read file and initialize word index array ArrayList<String> docLines = new ArrayList<String>(); //获取所有文本的每一行,注意此处输入为docName。输入为docLines(每个文本的每一行文档) FileUtil.readLines(docName, docLines); HashMap<String, Integer> mapkey=new HashMap<String, Integer>(); for(String line : docLines){ HashMap<Integer,Double> words = new HashMap<Integer,Double>(); // System.out.println(line.split("\\s+")[0]); /** * @author qianyang * 注意此处输入 * words对应的是集合 * */ FileUtil.Split(line, words); //获取每个用户最高的主题是什么 int key=getMaxValue(words); mapkey.put(line.split("\\s+")[0], key); } //获取每个value下的,前20个key productandproduct=new HashMap<String, Object>(); Test mvg = new Test(mapkey); for (Map.Entry<String, Integer> entry : mapkey.entrySet()) { productandproduct.put(entry.getKey(), mvg.getKey(entry.getValue())); // System.out.println(mvg.getKey(entry.getValue())); // System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue()); } } } /** * 求Map<K,V>中Value(值)的最大值 * @param map * @return */ public static int getMaxValue(Map<Integer, Double> map) { List<Map.Entry<Integer, Double>> list= new ArrayList<Map.Entry<Integer, Double>>(map.entrySet()); Collections.sort(list, new Comparator<Map.Entry<Integer, Double>>() { //降序排序 public int compare(Entry<Integer, Double> o1, Entry<Integer, Double> o2) { //return o1.getValue().compareTo(o2.getValue()); return o2.getValue().compareTo(o1.getValue()); } }); //获取最大的概率对应的key值 int key=0; for (Map.Entry<Integer, Double> mapping : list) { key=mapping.getKey(); // System.out.println(mapping.getKey() + ":" + mapping.getValue()); break; } return key; } /** * 获取map中第一个数据值 * * @param <K> Key的类型 * @param <V> Value的类型 * @param map 数据源 * @return 返回的值 */ public static <K, V> K getFirstOrNull(Map<K, V> map) { K obj = null; for (Entry<K, V> entry : map.entrySet()) { obj = entry.getKey(); if (obj != null) { break; } } return obj; } //通过map获取key值 public class Map_ValueGetKey { HashMap map; public Map_ValueGetKey(HashMap map) { // 初始化操作 this.map = map; } public Object getKey(Object value) { Object o = null; ArrayList all = new ArrayList(); // 建一个数组用来存放符合条件的KEY值 /* * 这里关键是那个entrySet()的方法,它会返回一个包含Map.Entry集的Set对象. * Map.Entry对象有getValue和getKey的方法,利用这两个方法就可以达到从值取键的目的了 * */ Set set = map.entrySet(); Iterator it = set.iterator(); while (it.hasNext()) { Map.Entry entry = (Map.Entry) it.next(); if (entry.getValue().equals(value)) { o = entry.getKey(); all.add(o); // 把符合条件的项先放到容器中,下面再一次性打印出 } } return all; } } }
相关文章推荐
- 获取map中根据key进行排序的value数据
- java map遍历、排序,根据value获取key
- Java中Map根据键值(key)或者值(value)进行排序实现
- Java中Map根据键值(key)或者值(value)进行排序实现
- map中根据value获取key
- Map四种获取key和value值的方法,以及对map中的元素排序
- Map 根据value 获取key
- MyBatis根据Map中key作为字段名,value作为字段值修改数据
- Map转成JSON数据,根据key排序问题
- Map四种获取key和value值的方法,以及对map中的元素排序(转载)
- Java基础 —— 根据 Key 或是 Value 对 Map 进行排序
- Java中Map根据键值(key)或者值(value)进行排序实现
- Java 中 Map 根据键值 (key) 或者值 (value) 进行排序实现
- 四种Map获取key和value值的方法,及对map中的元素进行排序
- 从标准输入中读取两个字符串 name:表示姓名,hobby:爱好;name作为key;hobby作为Value存入Map 然后根据姓名取得Value并打印,再根据name值删除map中的数据再打印ma
- 算法实战:根据Key或Value对Map进行排序及其应用
- Java中Map根据键值(key)或者值(value)进行排序实现
- Map相关 - 根据value获取key
- Java中Map根据键值(key)或者值(value)进行排序实现
- 在map中根据value获取key