博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
利用map ,找出list里面string类型,长度最小的那个
阅读量:4677 次
发布时间:2019-06-09

本文共 2215 字,大约阅读时间需要 7 分钟。

package main;import java.util.ArrayList;import java.util.Arrays;import java.util.Collection;import java.util.Collections;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import java.util.TreeSet;import javax.print.attribute.HashAttributeSet;public class 发过的 {    public static void main(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( ArrayList
arrayList) { //定义一个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.Entry
str : hashMap.entrySet()){ if(obj[0].equals(str.getKey())){ resultValue = str.getValue(); } } return resultValue ; } }

 

转载于:https://www.cnblogs.com/xiaowoniulx/p/10607231.html

你可能感兴趣的文章
爬网页数据
查看>>
eclipse内存溢出 参数配置
查看>>
页面元素定位 XPath 简介
查看>>
贪心/poj 1017 Packets
查看>>
[转]loadrunner:系统的平均并发用户数和并发数峰值如何估算
查看>>
Linux下Tomcat重新启动
查看>>
时间复杂度
查看>>
HTML Table to Json
查看>>
Theano 学习笔记(一)
查看>>
1.7 节点进行排序显示
查看>>
web最佳实践
查看>>
spring 集成shiro 之 自定义过滤器
查看>>
验证密码不允许有连续三位重复的正则表达式
查看>>
python 中对list去重
查看>>
Mono Libgdiplus库
查看>>
js模糊查询案例
查看>>
c语言基础知识要点
查看>>
Android模拟器无法上网访问网络失败解决办法
查看>>
node启动时, listen EADDRINUSE 报错;
查看>>
vue学习链接
查看>>