使用Hashtable对字符串进行碰撞
2006-09-21 16:34
309 查看
1.在一些字符串数组中,常会有重复的记录,比如手机号码,我们可以通过Hashtable来对其进行过滤
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public String[] checkArray(String[] str)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Hashtable<String, String> hash=new Hashtable<String, String>();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(int i=0;i<str.length;i++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(!hash.containsKey(str[i]))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
hash.put(str[i], str[i]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Enumeration enumeration=hash.keys();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String[] str_new=new String[hash.size()];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int i=0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
while(enumeration.hasMoreElements())...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
str_new[i]=enumeration.nextElement().toString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
i++;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return str_new;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
示例:
String[] mobile={"13811071500","13811071500","13811071501","13811071503","13811071501"};
mobile=checkArray(mobile);
for(int i=0;i<mobile.length;i++)
System.out.println(mobile[i]);
输出结果为:
13811071503
13811071501
13811071500
2.A,B均为字符串数组,找出在A中存在,而在B中不存在的字符串
public String[] compareArray(String[] A,String[] B){
Hashtable<String, String> hash=new Hashtable<String, String>();
Hashtable<String, String> hash_new=new Hashtable<String, String>();
for(int i=0;i<B.length;i++)
hash.put(B[i], B[i]);
for(int i=0;i<A.length;i++){
if(!hash.containsKey(A[i]))
hash_new.put(A[i], A[i]);
}
String[] C=new String[hash_new.size()];
int i=0;
Enumeration enumeration=hash_new.keys();
while(enumeration.hasMoreElements()){
C[i]=enumeration.nextElement().toString();
i++;
}
return C;
}
示例:
String[] mobile1={"13811071500","13811071501","13811071502","13811071503","13811071504"};
String[] mobile2={"13811071500","13811071505","13811071502","13811071506","13811071504"};
String[] mobile3=compareArray(mobile1,mobile2);
for(int i=0;i<mobile3.length;i++)
System.out.println(mobile[i]);
输出结果:
13811071503
13811071501
存在的问题:
每次都是倒序,可以再对程序稍加改动,变成正序。
3.将一个字符串数组中某一个特定的字符串过滤掉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/** *//**检验一个字符串数组,若包含某一特定的字符串,则将该字符串从数组中删
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
除,返回剩余的字符串数组
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param str_array 字符串数组
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param str_remove 待删除的字符串
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @return 过滤后的字符串
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public String[] removeStrFromArray(String[] str_array,String
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
str_remove)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Hashtable<String, String> hash=new Hashtable<String, String>();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(int i=0;i<str_array.length;i++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(!str_array[i].equals(str_remove))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
hash.put(str_array[i], str_array[i]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//生成一个新的数组
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String[] str_new=new String[hash.size()];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int i=0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Enumeration enumeration=hash.keys();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
while(enumeration.hasMoreElements())...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
str_new[i]=enumeration.nextElement().toString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
i++;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return str_new;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public String[] checkArray(String[] str)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Hashtable<String, String> hash=new Hashtable<String, String>();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(int i=0;i<str.length;i++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(!hash.containsKey(str[i]))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
hash.put(str[i], str[i]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Enumeration enumeration=hash.keys();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String[] str_new=new String[hash.size()];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int i=0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
while(enumeration.hasMoreElements())...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
str_new[i]=enumeration.nextElement().toString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
i++;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return str_new;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
示例:
String[] mobile={"13811071500","13811071500","13811071501","13811071503","13811071501"};
mobile=checkArray(mobile);
for(int i=0;i<mobile.length;i++)
System.out.println(mobile[i]);
输出结果为:
13811071503
13811071501
13811071500
2.A,B均为字符串数组,找出在A中存在,而在B中不存在的字符串
public String[] compareArray(String[] A,String[] B){
Hashtable<String, String> hash=new Hashtable<String, String>();
Hashtable<String, String> hash_new=new Hashtable<String, String>();
for(int i=0;i<B.length;i++)
hash.put(B[i], B[i]);
for(int i=0;i<A.length;i++){
if(!hash.containsKey(A[i]))
hash_new.put(A[i], A[i]);
}
String[] C=new String[hash_new.size()];
int i=0;
Enumeration enumeration=hash_new.keys();
while(enumeration.hasMoreElements()){
C[i]=enumeration.nextElement().toString();
i++;
}
return C;
}
示例:
String[] mobile1={"13811071500","13811071501","13811071502","13811071503","13811071504"};
String[] mobile2={"13811071500","13811071505","13811071502","13811071506","13811071504"};
String[] mobile3=compareArray(mobile1,mobile2);
for(int i=0;i<mobile3.length;i++)
System.out.println(mobile[i]);
输出结果:
13811071503
13811071501
存在的问题:
每次都是倒序,可以再对程序稍加改动,变成正序。
3.将一个字符串数组中某一个特定的字符串过滤掉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/** *//**检验一个字符串数组,若包含某一特定的字符串,则将该字符串从数组中删
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
除,返回剩余的字符串数组
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param str_array 字符串数组
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param str_remove 待删除的字符串
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @return 过滤后的字符串
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public String[] removeStrFromArray(String[] str_array,String
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
str_remove)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Hashtable<String, String> hash=new Hashtable<String, String>();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(int i=0;i<str_array.length;i++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(!str_array[i].equals(str_remove))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
hash.put(str_array[i], str_array[i]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//生成一个新的数组
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String[] str_new=new String[hash.size()];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int i=0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Enumeration enumeration=hash.keys();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
while(enumeration.hasMoreElements())...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
str_new[i]=enumeration.nextElement().toString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
i++;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return str_new;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
相关文章推荐
- 使用Hashtable对字符串进行碰撞
- 使用Hashtable对字符串进行碰撞
- 使用Hashtable对字符串进行碰撞
- Logstash使用ruby和正则进行字符串处理
- Java之使用json-lib进行对象与json字符串之间的转换
- WPF中使用Hashtable剔除重复字符串(比如电话号码)
- 使用ClrProfiler对内存进行调试----字符串操作篇
- Java中的字符串比较,按照使用习惯进行比较
- 使用 SQL的 for xml path来进行字符串拼接
- Python使用collections的Counter类对字符串、列表、字典的数据元素进行计数
- 如何将iOS字符串使用MD5进行加密
- RPAD的使用--对字符串使用指定的字符进行填充
- 使用STL进行字符串分割(C++)
- 自己封装的三维AABB包围盒类,用于三维模型静态碰撞检测,可以直接调用,使用OpenGL进行绘制
- 黑马程序员-使用String类完成如下功能,对英文字符串进行加密处理。
- 不使用库函数,对字符串进行以空格划分的逆序排列.
- PHP中使用转义字符“\”对字符串进行转义
- oracle的常用函数 instr() 和substr()函数 博客分类: oracle 在Oracle中 可以使用instr函数对某个字符串进行判断,判断其是否含有指定的字符。 在一个
- 使用Java对字符串进行MD5加密以及验证
- 使用标注表达式,怎样用正则表达式对同类字符串进行修改 (copied)