下载安卓APP箭头
箭头给我发消息

客服QQ:3315713922

如何使用Hashtable对字符串进行碰撞

作者:课课家教育     来源: http://www.kokojia.com点击数:959发布时间: 2016-02-29 14:01:54

标签: Java设计Java开发Java编程

大神带你学编程,欢迎选课

如何使用Hashtable对字符串进行碰撞_Java设计_Java开发_课课家

声明:以下的测试是用的JDk xalan对于如下XML文档片断:

  GUID2006102000002

  1.0.1R

  2006-10-25 13:12:10

  SOBEY_NEWS

  SOBEY新闻系统

  DAYANG_MAM

  1.在一些字符串数组中,常会有重复的记录,比如手机号码,我们可以通过Hashtable来对其进行过滤 public String[] checkArray(String[] str)...{

  Hashtable hash=new Hashtable();

  for(int i=0;i

  if(!hash.containsKey(str[i]))

  hash.put(str[i], str[i]);

  }

  Enumeration enumeration=hash.keys();

  String[] str_new=new String[hash.size()];

  int i=0;

  while(enumeration.hasMoreElements())...{

  str_new[i]=enumeration.nextElement().toString();

  i++;

  }

  return str_new;

  }

  示例:

  String[] mobile={"13811071500","13811071500","13811071501","13811071503","13811071501"};

  mobile=checkArray(mobile);

  for(int i=0;i

  System.out.println(mobile[i]);

  输出结果为:

  13811071503

  13811071501

  13811071500

  2.A,B均为字符串数组,找出在A中存在,而在B中不存在的字符串

  public String[] compareArray(String[] A,String[] B){

  Hashtable hash=new Hashtable();

  Hashtable hash_new=new Hashtable();

  for(int i=0;i

  hash.put(B[i], B[i]);

  for(int i=0;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

  System.out.println(mobile[i]);

  输出结果:

  13811071503

  13811071501

  存在的问题:

  每次都是倒序,可以再对程序稍加改动,变成正序。

  3.将一个字符串数组中某一个特定的字符串过滤掉

  /** *//**检验一个字符串数组,若包含某一特定的字符串,则将该字符串从数组中删

  除,返回剩余的字符串数组

  * @param str_array 字符串数组

  * @param str_remove 待删除的字符串

  * @return 过滤后的字符串

  */

  public String[] removeStrFromArray(String[] str_array,String

  str_remove)...{

  Hashtable hash=new Hashtable();

  for(int i=0;i

  if(!str_array[i].equals(str_remove))

  hash.put(str_array[i], str_array[i]);

  }

  //生成一个新的数组

  String[] str_new=new String[hash.size()];

  int i=0;

  Enumeration enumeration=hash.keys();

  while(enumeration.hasMoreElements())...{

  str_new[i]=enumeration.nextElement().toString();

  i++;

  }

  return str_new;

  }

  DAYANG媒资系统

  2

  REQUEST_ID_01

  4

  节目GUID

  3

  管理信息实体ID

  2

  对于上述含有命名空间的xml文档,如果想通过xpath查找EnvelopID的值,有两种方式:1.通过利用xpath的函数local-name()

  如上述查找内容的xpath表达式可以写为://*[local-name()='MREML']/EnvelopEntity/EnvelopID/text()

  2.通过在java程序中处理上述文档的命名空间是定义的,如果要使xpath表达式能正确地被解析需要在java程序中建立起prefix和uri二者的映射关系

  public static Node parseXPath(String expression, Object obj, QName qname)

  throws Exception {

  javax.xml.xpath.XPath xpath = javax.xml.xpath.XPathFactory.newInstance().newXPath();

  xpath.setNamespaceContext(getNamespaceContext());

  if (qname.equals(XPathConstants.NODE))

  return (Node) xpath.evaluate(expression, obj, qname);

  return null;

  }

  public static NamespaceContext getNamespaceContext() throws Exception {

  return new NamespaceContext() {

  public String getNamespaceURI(String prefix) {

  /*

  // 一种方式:

  //这里可以采用配置文件的方式,先将所需要使用的xmlNamespace配置好,

  //采用注册的方式供应用使用,这种方式效率应该高一些,不用每次都要从文档中提取namespace

  //不过没有第二种方便

  String uri;

  if (prefix.equalsIgnoreCase("ML"))

  uri = "MREML";

  else if (prefix.equalsIgnoreCase("RE"))

  uri = "http://herry.com.cn";

  else if (prefix.equalsIgnoreCase("RID"))

  uri = "ResourceID";

  else if (prefix.equalsIgnoreCase("RUI"))

  uri = "ResourceUniqueID";

  else if (prefix.equalsIgnoreCase("RMDI"))

  uri = "ResourceMetaDataInfo";

  else

  uri = null;

  System.out.println(ParseXMLUtil.class + "::getNamespaceURI:prefix= " + prefix);

  return uri;

  */

  /*

  // 另外一种方式: 通过PrefixResolver来提取出prefix和Namespace的对应关系

  final PrefixResolver resolver =

  new PrefixResolverDefault(doc.getDocumentElement());

  return resolver.getNamespaceForPrefix(prefix);

  */

  // Dummy implementation - not used!

  public java.util.Iterator getPrefixes(String val) {

  return null;

  }

  // Dummy implemenation - not used!

  public String getPrefix(String uri) {

  return null;

  }

  };

  }

  若要查找ResourceUniqueID的值,则xpath应该书写为://ML:MREML/ML:ResourceEntity/RMDI:ResourceMetaDataInfo/RMDI:ResourceID/RUI:ResourceUniqueID/text()

  这里的ML(prefix)和URI(MERML)等已经通过

  对于含有默认命名空间地文档如果采用NamespaceContext的方式,即将prefix和uri已经映射起来,如对于以下的文档:

  节目GUID

  UserDefID填写节目代码

  1

  911 袭击

  美国遭遇911袭击现场30S画面及同期声素材

  今日世界

  2006-10-25 13:12:10

  00:04:35:12

  2

  00:00:00:05

  00:04:35:10

  若要查找ResourceUniqueID的值,则xpath应该书写为://ML:MREML/ML:ResourceEntity/RMDI:ResourceMetaDataInfo/RMDI:ResourceID/RUI:ResourceUniqueID/text()

  这里的ML(prefix)和URI(MERML)等已经通过

  if (prefix.equalsIgnoreCase("ML"))

  uri = "MREML";

  else if (prefix.equalsIgnoreCase("RE"))

  uri = "http://herry.com.cn";

  else if (prefix.equalsIgnoreCase("RID"))

  uri = "ResourceID";

  else if (prefix.equalsIgnoreCase("RUI"))

  uri= "ResourceUniqueID";

  else if (prefix.equalsIgnoreCase("RMDI"))

  uri = "ResourceMetaDataInfo";

  else

  uri = null;

  映射。

  其中的ML和RMDI都是default namespace,它的作用范围包含它的子元素,直至有新的default namespace出现为止。而namespace只对它自身起作用。如将上述文档中:

  改为:

  …

  …

  查找ResourceUniqueID的值,则xpath应该改为://ML:MREML/ML:ResourceEntity/RMDI:ResourceMetaDataInfo/ResourceID/RUI:ResourceUniqueID/text()

赞(23)
踩(4)
分享到:
华为认证网络工程师 HCIE直播课视频教程