您的位置:首页 > 产品设计 > UI/UE

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;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: