📄 listtools.java
字号:
*** @return The Set containing the elements from specified array **/ public static <T> Set<T> toSet(T a[], Set<T> set) { return ListTools.toSet(a, 0, -1, set); } /** *** Copies the specified Object array to a new Set *** @param a The array to copy to the Set *** @param ofs The offset within 'a' to begin copying *** @param len The number of elements to copy to the new array *** @param set The Set to which the array elements will be copied *** @return The Set containing the elements from specified array **/ public static <T> Set<T> toSet(T a[], int ofs, int len, Set<T> set) { Set<T> v = (set != null)? set : new HashSet<T>(); int alen = (a != null)? a.length : 0; ofs = _constrainOffset(ofs, alen); len = _constrainLength(ofs, len, alen); for (int i = ofs; i < len; i++) { v.add(a[i]); } return v; } // ------------------------------------------------------------------------ /** *** Creates a new Map instance containing the elements from the specified array *** @param arry The array from which the Map instance will be created. For each row major *** element in the array, the first column will be the key, and the second column *** will be the value. *** @return The Map (OrderedMap) created from the specified array **/ public static Map<Object,Object> toMap(Object arry[][]) { return ListTools.toMap(arry, (Map<Object,Object>)null); } /** *** Creates a new Map instance containing the elements from the specified array *** @param arry The array from which the Map instance will be created. For each row major *** element in the array, the first column will be the key, and the second column *** will be the value. *** @param map The map instance to which key/value elements from the specified array will be *** copied. If null, a new OrderedMap will be created. *** @return The Map created from the specified array **/ public static Map<Object,Object> toMap(Object arry[][], Map<Object,Object> map) { Map<Object,Object> m = (map != null)? map : new OrderedMap<Object,Object>(); if (arry != null) { for (int i = 0; i < arry.length; i++) { if (arry[i].length >= 2) { Object key = arry[i][0], val = arry[i][1]; if ((key != null) && (val != null)) { m.put(key, val); } } } } return m; } // ------------------------------------------------------------------------ /** *** Creates a Map instance containing the elements from the specified array. *** The key for a given element is the value returned from the specified method *** on the array element. *** @param keyMethod The method name invoked on the array element to retrieve the *** element key. *** @param arry The array added to the map. *** @return A new OrderedMap instance containing the array elements **/ public static Map<Object,Object> toMap(String keyMethod, Object arry[]) { return ListTools.toMap(keyMethod, arry, (Map<Object,Object>)null); } /** *** Creates a Map instance containing the elements from the specified array. *** The key for a given element is the value returned from the specified method *** on the array element. *** @param keyMethod The method name invoked on the array element to retrieve the *** element key. *** @param arry The array added to the map. *** @param map The map to which the array elements will be added. If null, *** a new OrderedMap instance will be created. *** @return The Map instance containing the array elements **/ public static Map<Object,Object> toMap(String keyMethod, Object arry[], Map<Object,Object> map) { Map<Object,Object> m = (map != null)? map : new OrderedMap<Object,Object>(); if ((arry != null) && (keyMethod != null) && !keyMethod.equals("")) { for (int i = 0; i < arry.length; i++) { Object val = arry[i]; if (val != null) { try { MethodAction ma = new MethodAction(arry[i], keyMethod); Object key = ma.invoke(); if (key != null) { m.put(key, val); } } catch (Throwable th) { Print.logError("Error creating map: " + th); } } } } return m; } // ------------------------------------------------------------------------ /** *** Adds the specified Object to the specified List *** @param list The List to which the specified object will be added *** @param obj The Object to add to the specified List *** @return The 'list' instance to which the object was added **/ public static <T> java.util.List<T> add(java.util.List<T> list, T obj) { return ListTools.insert(list, obj, -1); } /** *** Inserts the specified Object to the specified List, at the specified location *** @param list The List to which the specified object will be inserted *** @param obj The Object to insert into the specified List *** @param ndx The location in the List instance where the object will be inserted *** @return The 'list' instance to which the object was inserted **/ public static <T> java.util.List<T> insert(java.util.List<T> list, T obj, int ndx) { if (list != null) { list.add(ndx, obj); } return list; } /** *** Removes the element at the specified index from the specified List *** @param list The List from which the element at the specified index will be removed *** @param ndx The index of the element to remove from the specified List *** @return The 'list' instance from which the element was removed **/ public static <T> java.util.List<T> remove(java.util.List<T> list, int ndx) { if (list != null) { list.remove(ndx); } return list; } /** *** Creates a new array with the specified object appended to the end of the specified array *** @param list The array to which the object will be appended *** @param obj The object to append to the specified array *** @return The new Object array to which the specified object instance was appended **/ public static <T> T[] add(T list[], T obj) { return ListTools.insert(list, obj, -1); } /** *** Creates a new array with the specified object inserted into specified array *** @param list The array to which the object will be inserted *** @param obj The object to insert into the specified array *** @param index The location where the object will be inserted *** @return The new Object array to which the specified object instance was inserted **/ public static <T> T[] insert(T list[], T obj, int index) // throws ArrayStoreException { if (list != null) { int ndx = ((index > list.length) || (index < 0))? list.length : index; Class type = list.getClass().getComponentType(); int size = (list.length > ndx)? (list.length + 1) : (ndx + 1); T array[] = (T[])Array.newInstance(type, size); // "unchecked cast" if (ndx > 0) { int maxLen = (list.length >= ndx)? ndx : list.length; System.arraycopy(list, 0, array, 0, maxLen); } array[ndx] = obj; // <-- may throw ArrayStoreException if (ndx < list.length) { int maxLen = list.length - ndx; System.arraycopy(list, ndx, array, ndx + 1, maxLen); } return array; } else { return null; } } /** *** Removes the element at the specified index from the specified array *** @param list The array from which the element will be removed *** @param ndx The index of the element to remove from the specified array *** @return The new Object array from which the specified element was removed **/ public static <T> T[] remove(T list[], int ndx) { if ((list != null) && (ndx >= 0) && (ndx < list.length)) { Class type = list.getClass().getComponentType(); T array[] = (T[])Array.newInstance(type, list.length - 1); // "unchecked cast" if (ndx > 0) { System.arraycopy(list, 0, array, 0, ndx); } if (ndx < (list.length - 1)) { System.arraycopy(list, ndx + 1, array, ndx, list.length - ndx - 1); } return array; } else { return null; } } // ------------------------------------------------------------------------ /** *** Returns the index of the specified element within the specified List *** @param list The List instance containing the specified element *** @param item The List element for which the index will be returned *** @return The index of the specified element within the specified List, or -1 if *** the specified element was not found in the specified List **/ public static <T> int indexOf(java.util.List<T> list, T item) { if (list == null) { return -1; } else { return list.indexOf(item); } } /** *** Returns the index of the specified case-insensitive String value within the specified List. *** @param list The List containing String values *** @param item The String value for which the index will be returned *** @return The index of the specified case-insensitive String value within the specified *** List, or -1 if the case-insensitive String value was not found. **/ public static int indexOfIgnoreCase(java.util.List<String> list, String item) { if (list == null) { return -1; } else { int index = 0; for (Iterator i = list.iterator(); i.hasNext(); index++) { Object listObj = i.next(); String listStr = (listObj != null)? listObj.toString() : null; if (listStr == item) { // also takes care of 'null == null' return index; } else if ((listStr != null) && listStr.equalsIgnoreCase(item)) { return index; } } return -1; } } /** *** Returns the index of the specified object within the specified array *** @param list The array containing the specified object *** @param item The element for which the index will be returned *** @return The index of the specified element within the specified array, or -1 if *** the specified element was not found in the specified array **/ public static <T> int indexOf(T list[], T item) { return ListTools.indexOf(list, 0, -1, item); } /** *** Returns the index of the specified object within the specified array *** @param list The array containing the specified object *** @param ofs The offset within the array to begin searching *** @param len The number of elements to search within the array *** @param item The element for which the index will be returned *** @return The index of the specified element within the specified array, or -1 if *** the specified element was not found in the specified array **/ public static <T> int indexOf(T list[], int ofs, int len, T item) { if (list == null) { /* no list */ return -1; } else { /* constrain offset/length */ int alen = (list != null)? list.length : 0; ofs = _constrainOffset(ofs, alen); len = _constrainLength(ofs, len, alen); /* loop through array checking for item */ for (int i = ofs; i < len; i++) { if (list[i] == item) { // also takes care of 'null == null' return i; } else if ((list[i] != null) && list[i].equals(item)) { return i; } } /* still not found */ return -1; } } /** *** Returns the index of the specified case-insensitive String value within the specified array. *** @param list The array containing String values *** @param item The String value for which the index will be returned *** @return The index of the specified case-insensitive String value within the specified *** array, or -1 if the case-insensitive String value was not found. **/ public static int indexOfIgnoreCase(String list[], String item) { if (list == null) { return -1; } else { for (int i = 0; i < list.length; i++) { if (list[i] == item) { // also takes care of 'null == null' return i; } else if ((list[i] != null) && list[i].equalsIgnoreCase(item)) { return i; } } return -1; } } // ------------------------------------------------------------------------ /** *** Returns true if the specified list contains the specified element *** @param list The List instance to search *** @param item The element which is tested for inclusion in the specified List *** @return True if the specified List contains the specified element **/ public static <T> boolean contains(java.util.List<T> list, T item) { return (ListTools.indexOf(list, item) >= 0); } /** *** Returns true if the specified list contains the specified case-insensitive String value *** @param list The List instance to search *** @param item The case-insensitive String which is tested for inclusion in the specified List *** @return True if the List contains the specified case-insensitive String value **/ public static boolean containsIgnoreCase(java.util.List<String> list, String item) { return (ListTools.indexOfIgnoreCase(list, item) >= 0); } /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -