📄 collectionutils.java
字号:
package edu.stanford.nlp.util;import java.util.*;/** * Collection of useful static methods for working with Collections. Includes * methods to increment counts in maps and cast list/map elements to common types. * * @author Joseph Smarr (jsmarr@stanford.edu) */public class CollectionUtils{ /** Private constructor to prevent direct instantiation. */ private CollectionUtils() { } /** * Adds the given delta to the Integer value stored for the given key in * the given Map. Returns whether the entry had to be created (i.e. it wasn't * in the map), in which case it's inserted with delta as the initial value. * @param map Map from keys to Integer values representing key counts * @param key key in map for Integer to increment * @param delta amount to change Integer value count by * @return whether a new entry for the given key was created in the process (i.e. * if it wasn't in the map before). */ public static boolean incrementCount(Map map,Object key,int delta) { boolean created=false; Integer count=(Integer)map.get(key); if(count==null) { count=new Integer(0); created=true; } map.put(key,new Integer(count.intValue()+delta)); return(created); } /** * Increments the Integer count of the given key in the given Map by 1. * @see #incrementCount(Map,String,int) */ public static boolean incrementCount(Map map,Object key) { return(incrementCount(map,key,1)); } // list casting methods /** Returns <tt>(String)list.get(index)</tt>. */ public static String getString(List list,int index) { return((String)list.get(index)); } /** Returns <tt>(Integer)list.get(index)</tt>. */ public static Integer getInteger(List list,int index) { return((Integer)list.get(index)); } /** Returns <tt>((Integer)list.get(index)).intValue()</tt>. */ public static int getInt(List list,int index) { return(getInteger(list,index).intValue()); } /** Returns <tt>(Double)list.get(index)</tt>. */ public static Double getDouble(List list,int index) { return((Double)list.get(index)); } /** Returns <tt>((Double)list.get(index)).doubleValue()</tt>. */ public static double getdouble(List list,int index) { return(getDouble(list,index).doubleValue()); } /** Returns <tt>((Boolean)list.get(index)).booleanValue()</tt>. */ public static boolean getBoolean(List list,int index) { return(((Boolean)list.get(index)).booleanValue()); } // map casting methods /** Returns <tt>(String)map.get(key)</tt>. */ public static String getString(Map map,Object key) { return((String)map.get(key)); } /** Returns <tt>(Integer)map.get(key)</tt>. */ public static Integer getInteger(Map map,Object key) { return((Integer)map.get(key)); } /** Returns <tt>((Integer)map.get(key)).intValue()</tt>. */ public static int getInt(Map list,Object key) { return(getInteger(list,key).intValue()); } /** Returns <tt>(Double)map.get(key)</tt>. */ public static Double getDouble(Map map,Object key) { return((Double)map.get(key)); } /** Returns <tt>((Double)map.get(key)).doubleValue()</tt>. */ public static double getdouble(Map map,Object key) { return(getDouble(map,key).doubleValue()); } /** Returns <tt>((Boolean)map.get(key)).booleanValue()</tt>. */ public static boolean getBoolean(Map map,Object key) { return(((Boolean)map.get(key)).booleanValue()); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -