📄 collectionutil.java
字号:
/**
* $Id:CollectionUtil.java $
*
* Copyright 2004 ~ 2005 JingFei International Cooperation LTD. All rights reserved.
*
*/
package com.jfimagine.utils.commonutil;
import java.util.Vector;
import java.util.Collection;
import java.util.Map;
import java.util.Iterator;
import java.util.TreeSet;
import com.jfimagine.utils.commonutil.CommonUtil;
/**
* Collection, Map and List processes,especially for Integer Object List.
*
* @author CookieMaker
*
* @version $Revision: 1.00 $
*/
public class CollectionUtil{
/**
* Get the hashcode of an array.
*
* @param array a byte array.
*
* @return the hashcode
*
*/
public static int getHashCode(byte[] array){
if (array==null)
return 0;
int code=0;
for (int i=0; i<array.length; i++){
code ^=array[i];
}
return code;
}
/**
* Compare if two byte array is equal.
*
* @param array1 one byte array.
* @param array2 other byte array.
*
* @return True if equal, false otherwise.
*
*/
public static boolean compareArrays(byte[] array1, byte[] array2){
if (array1==null || array2==null)
return false;
if (array1.length!=array2.length)
return false;
if (array1==array2)
return true;
for (int i=0; i<array1.length; i++){
if (array1[i]!=array2[i])
return false;
}
return true;
}
/**
* Get a Vector object through a Collection
*
* @param c a Collection to be re-generated
*
* @return a new Vector object, an empty Vector if null, a casting reference if also a Vector, otherwise a new Vector through input
*
*/
public static Vector getVector(Collection c){
if (c==null){
return new Vector();
}else if (c instanceof Vector){
return (Vector)c;
}else {
return new Vector(c);
}
}
/**
* Get value string by an int key
*
* @param map a HashMap used to store key-value pair list, assuming all the keys are Integer objects
*
* @param key a finding key
*
* @param failedKey a default key for return if failed to find
*
* @return a String value found, null if map is null
*/
static public String getValueByKey(Map map, int key,int failedKey){
if (map==null){
System.err.println("Invalid map object!");
return null;
}
//Find value from map by key:
String val=(String) map.get(new Integer(key));
//If failed to find, use failed key for instead:
if (val==null){
val =(String)map.get(new Integer(failedKey));
}
return val;
}
/**
* Get int key by value string
*
* @param map a HashMap used to store key-value pair list, assuming all the keys are Integer objects
*
* @param val a value string to be found
*
* @param failedKey a default key for return if failed to find
*
* @return int key found which the first occurrence of the finding value string
*/
static public int getKeyByValue(Map map,String val,int failedKey){
if (map==null){
System.err.println("Invalid map object!");
return failedKey;
}
//Get a key iterator, we used the sort function of a TreeSet:
Iterator it = (new TreeSet(map.keySet())).iterator();
Integer itemId=null;
while ((it != null) && it.hasNext()) {
itemId = (Integer)it.next();
if (itemId!=null && ((String)map.get(itemId)).compareTo(val)==0){
return itemId.intValue();
}
}
return failedKey;
}
/**
* Get an Integer Key List
*
* @param map a HashMap used to store key-value pair list, assuming all the keys are Integer objects
*
* @param exceptKey An invalid key which should not be returned
*
* @return A key list found
*/
static public Collection getKeyList(Map map,int exceptKey){
if (map==null){
System.err.println("Invalid map object!");
return null;
}
//Get a key iterator, we used the sort function of a TreeSet:
Iterator it = (new TreeSet(map.keySet())).iterator();
Vector retList =new Vector();
Integer itemId=null;
while ((it != null) && it.hasNext()) {
itemId = (Integer)it.next();
if (itemId!=null && itemId.intValue()!=exceptKey){
retList.add(itemId);
}
}
return retList;
}
/**
* Get an value list
*
* @param map a HashMap used to store key-value pair list, assuming all the keys are Integer objects
*
* @param exceptKey An invalid key which should not be returned
*
* @return A value list found
*/
static public Collection getValueList(Map map,int exceptKey){
if (map==null){
System.err.println("Invalid map object!");
return null;
}
//get an Integer key list firstly:
Collection keyList=getKeyList(map,exceptKey);
//Get a value list by key list:
if (keyList!=null){
Iterator it=keyList.iterator();
Vector retList =new Vector();
Integer itemId =null;
while ((it != null) && it.hasNext()) {
itemId = (Integer)it.next();
if (itemId!=null && itemId.intValue()!=exceptKey){
retList.add(map.get(itemId));
}
}
return retList;
}
return null;
}
/**
* Get the number of occurrence of a letter within a string
*
* @param inStr The string contains the letters
*
* @param ch The letter to be found
*
* @return The number of occurrence
*
*/
public static int getCharCount(String inStr,char ch){
int retVal=0;
if (inStr!=null && inStr.length()>0){
for (int i=0; i<inStr.length(); i++){
if (inStr.charAt(i)==ch)
retVal++;
}
}
return retVal;
}
/**
* Get a string section seperated by a letter within a big string
*
* @param inStr A big string contains sections and seperators
*
* @param ch The seperator
*
* @param ind An index of a specific seperator
*
* @return The string section seperated before a seperator
*
*/
public static String getPiece(String inStr,char ch,int ind){
StringBuffer tmpStr=new StringBuffer(inStr);
StringBuffer retStr=new StringBuffer();
if (inStr!=null && inStr.length()>0 && ind>0){
//Firstly truncate the string before ind-1 times occurrence of ch
while (ind>1){
if (tmpStr.charAt(0)==ch)
ind--;
tmpStr.deleteCharAt(0);
}
//Then get the string section wanted:
int charInd =0;
while (charInd<tmpStr.length() && tmpStr.charAt(charInd)!=ch){
retStr.append(tmpStr.charAt(charInd));
charInd++;
}
}
return retStr.toString();
}
/**
* Get an Integer object list from a string which was filled by numbers and seperators
*
* @param Str A string filled by numbers and seperators
*
* @param delim The seperator
*
* @return An Integer object list
*
*/
public static Collection getIntListFromStr(String str,char delim){
int charCnt =getCharCount(str,delim);
Vector retList=new Vector();
int subint=0;
for (int i=1; i<=charCnt; i++){
subint =CommonUtil.s2i(getPiece(str,delim,i));
retList.add(new Integer(subint));
}
return retList;
}
/**
* Convert an Integer object list to String, seperated by delim
*
* @param intList An Integer object to be converted
*
* @param delim The seperator
*
* @return A string represents an integer list
*
*/
public static String getStrFromIntList(Collection intList,char delim){
StringBuffer retStr=new StringBuffer();
Object o=null;
if (intList != null){
Iterator it=intList.iterator();
while (it!=null && it.hasNext()){
o = it.next();
if (o instanceof Integer){
retStr.append(((Integer)o).intValue());
retStr.append(delim);
}
}
}
return retStr.toString();
}
/**
* Convert a String seperated by delimiter into a String Object List
*
* @param Str A String contains sections and seperator
*
* @param delim The seperator
*
* @return A String object list
*
*/
public static Collection getStrListFromStr(String str,char delim){
int charCnt =getCharCount(str,delim);
Vector retList=new Vector();
for (int i=1; i<=charCnt; i++){
retList.add(getPiece(str,delim,i));
}
return retList;
}
/**
* Convert a String object list to a String, seperated by delim
*
* @param StrList A String object list
*
* @param delim The seperator
*
* @return A String represents a String list
*
*/
public static String getStrFromStrList(Collection strList,char delim){
StringBuffer retStr=new StringBuffer();
Object o=null;
if (strList != null){
Iterator it=strList.iterator();
while (it!=null && it.hasNext()){
o = it.next();
if (o instanceof String){
retStr.append((String)o);
retStr.append(delim);
}
}
}
return retStr.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -