很不错的Map工具类
2016-09-28 19:35
337 查看
将需要操作(主要用于取值)的Map对象放入该类,然后可以自动的进行类型转换,获取你需要的类型值
package utils; import java.util.HashMap; import java.util.Map; /** * Created by adinlead on 16/09/28. */ public class MapShell { // 要被包装的Map Map map; public MapShell() { this.map = new HashMap(); } public MapShell(Map map) { if (map == null) { this.map = new HashMap(); } else { this.map = map; } } public MapShell(Map map, Map spare) { if (map == null) { this.map = spare; } else { this.map = map; } } public Map getMap() { return map; } public boolean notEmpty(){ return this.map.size() > 0; } public boolean notEmpty(Object key){ return this.map.containsKey(key) && this.map.get(key) != null && !TextUtil.isEmpty(this.getString(key)); } public boolean isEmpty(){ return this.map.size() <= 0; } public boolean isEmpty(Object key){ return !this.map.containsKey(key) || TextUtil.isEmpty(this.getString(key)); } public Object get(Object key){ return this.map.get(key); } public void put(Object key,Object value){ this.map.put(key,value); } public Integer getInteger(Object key) { return getInteger(key, null); } public Integer getInteger(Object key, Integer spare) { if (this.map.containsKey(key)) { Object o = this.map.get(key); if (o instanceof Integer) { r 4000 eturn (Integer) o; } else { try { return Integer.valueOf(o.toString()); } catch (NumberFormatException e) { return spare; } } } return spare; } public Long getLong(Object key) { return getLong(key, null); } public Long getLong(Object key, Long spare) { if (this.map.containsKey(key)) { Object o = this.map.get(key); if (o instanceof Long) { return (Long) o; } else { try { return Long.valueOf(o.toString()); } catch (NumberFormatException e) { return spare; } } } return spare; } public Double getDouble(Object key){ return getDouble(key,null); } public Double getDouble(Object key, Double spare) { if (this.map.containsKey(key)) { Object o = this.map.get(key); if (o instanceof Double) { return (Double) o; } else { try { return Double.valueOf(o.toString()); } catch (NumberFormatException e) { return spare; } } } return spare; } public String getString(Object key) { return getString(key, null); } public String getString(Object key, String spare) { if (this.map.containsKey(key)) { return String.valueOf(this.map.get(key)); } return spare; } public int size(){ return this.map.size(); } public boolean valueEquation(Object key, Object val) { Object o = this.map.get(key); if (o == val) return true; else return val.equals(val); } /** * @function 保留键存在于参数中的的值 * @param keys */ public void retain(Object... keys){ Map tmp = this.map; this.map.clear(); for(Object key:keys){ this.map.put(key,tmp.get(key)); } } } class TextUtil { /** * Returns true if the string is null or 0-length. * * @param str the string to be examined * @return true if str is null or zero length */ public static boolean isEmpty(CharSequence str) { if (str == null || str.length() == 0) return true; else return false; } /** * Returns the length that the specified CharSequence would have if * spaces and control characters were trimmed from the start and end, * as by {@link String#trim}. */ public static int getTrimmedLength(CharSequence s) { int len = s.length(); int start = 0; while (start < len && s.charAt(start) <= ' ') { start++; } int end = len; while (end > start && s.charAt(end - 1) <= ' ') { end--; } return end - start; } public static String stringOf(Object object) { if (object == null) return null; else return object.toString(); } public static Boolean isInteger(String str) { return str != null && str.matches("[0-9]*"); } public static Boolean isDecimal(String str){ return str!=null && str.matches("^[0-9]+\\.([0-9]+)?$"); } public static Boolean isNumber(String str){ return str!= null && str.matches("^[0-9]+(\\.[0-9]+)?$"); } public static Integer parseInt(String str,int standby){ if(str == null) return standby; try { return Integer.parseInt(str); }catch (NumberFormatException e){ return standby; } } public static String getFileRandomName(String superaddition){ String name = System.currentTimeMillis() + "_"; int random = (int) (Math.random() * 9000 + 1000); name += random; if(superaddition != null){ name += "_" + superaddition; } return name; } }
相关文章推荐
- RE:从零开始的数据结构生活
- 【JZOJ 4799】我的快乐时代
- 列表
- 微信应用号开发必备技能都在这里了啦!
- Linux workqueue工作原理
- Apache默认index页面
- oracle10G/11G官方下载地址集合 直接迅雷下载
- emmet使用简介
- macOS apache配置及开启虚拟服务器的开启,apache开启重写模式
- 什么是SOAP?
- navicat连接oracle数据库ORA-28547:connection to server failed, probable Oracle Net admin error错误,解决方法
- Axure RP 8.0 基础1-10
- Gym 100962A ABBA (高斯消元)
- C# Udp Socket例子 客户端
- 如何生成支付宝RSA密钥
- freemaker学习总结
- vim下各类批量替换
- js三种创建函数的方式
- hihocoder-1391&&北京网赛09 Countries(优先队列)
- 杂文