packagemain;importjava.util.ArrayList;importjava.util.Arrays;importjava.util.Collection;importjava.util.Collections;importjava.util.HashMap;importjava.util.Iterator;importjava.util.List;importjava.util.Map;importjava.util.Set;importjava.util.TreeSet;importjavax.print.attribute.HashAttributeSet;public class发过的 {public static voidmain(String[] args) {
ArrayList arrayList =new ArrayList<>();
arrayList.add("sdfsdfs");
arrayList.add("sdwerwerfsdfs");
arrayList.add("werwr");
arrayList.add("we");
arrayList.add("sdfswewdfs");
arrayList.add("werwewrwerwerwrw");
arrayList.add("wwerwerwewesfsdrwrew");
getMinValue(arrayList);
System.out.println("最小Value是:::::::::::::"+getMinValue(arrayList));//HashMap hashMap=new HashMap();//for (String string : arrayList) {//int len=string.length();//hashMap.put(string.length(), string);//}//
//Iterator> entries = hashMap.entrySet().iterator();//while (entries.hasNext()) {//Map.Entry entry = entries.next();//System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());//}//
//Set set = hashMap.keySet();//Object[] obj = set.toArray();//Arrays.sort(obj);//System.out.println(obj[0]);//
//String resultValue = "";// //根据key ,找Value//for(Map.Entry str : hashMap.entrySet()){//if(obj[0].equals(str.getKey())){//resultValue = str.getValue();//}//}//System.out.println("最小Value是:::::::::::::"+resultValue);
}private static String getMinValue( ArrayListarrayList) {//定义一个map来存截取的数据
HashMap hashMap=new HashMap();for(String string : arrayList) {int len=string.length();
hashMap.put(string.length(), string);
}//获取所有的key值
Set set =hashMap.keySet();
Object[] obj=set.toArray();//排序
Arrays.sort(obj);//最小的Value值的变量
String resultValue = "";//根据key ,找Value
for(Map.Entrystr : hashMap.entrySet()){if(obj[0].equals(str.getKey())){
resultValue=str.getValue();
}
}returnresultValue ;
}
}