首先讲解关于java的intern
public String intern()返回字符串对象的规范化表示形式。
一个初始时为空的字符串池,它由类 String 私有地维护。
当调用 intern 方法时,如果池已经包含一个等于此 String 对象的字符串(该对象由 equals(Object) 方法确定),则返回池中的字符串。否则,将此 String 对象添加到池中,并且返回此 String 对象的引用。
它遵循对于任何两个字符串 s 和 t,当且仅当 s.equals(t) 为 true 时,s.intern() == t.intern() 才为 true。
注意,equals()表示2个变量是否引用了同一个对象实例,而==仅表示2个变量的值是否一致。
package org.apache.lucene.util;
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Subclasses of StringInterner are required to
* return the same single String object for all equal strings.
* Depending on the implementation, this may not be
* the same object returned as String.intern().
*
* This StringInterner base class simply delegates to String.intern().
*/
public class StringInterner {
/** Returns a single object instance for each equal string. */
public String intern(String s) {
return s.intern();
}
/** Returns a single object instance for each equal string. */
public String intern(char[] arr, int offset, int len) {
return intern(new String(arr, offset, len));
}
}
上述代码中
1)intern(String s)的机制是调用s.intern(),对于所有相同的字符串只返回一个对象的实例,是一个节约内存的好方法
2) public String intern(char[] arr, int offset, int len)完成一个返回某个
字符串从开始位置截取指定长度生成的新字符串对象的实例
其中用到了string的以下构造函数
public String(byte[] bytes,
int offset,
int length)构造一个新的 String,方法是使用指定的字符集解码字节的指定子数组。新的 String 的长度是一个字符集函数,因此不能等于该子数组的长度。
当给定字节在给定字符集中无效的情况下,该构造方法无指定的行为。当需要进一步控制解码过程时,应使用 CharsetDecoder 类。
参数:
bytes - 要解码为字符的字节
offset - 要解码的首字节的索引
length - 要解码的字节数
抛出:
IndexOutOfBoundsException - 如果 offset 和 length 参数索引字符超出 bytes 数组的范围
分享到:
相关推荐
Lucene3.5全部源码,打包jar文件,可以直接打开查看源码,Lucene开发必备
lucene3.5 IKAnalyzer3.2.5 实例中文分词通过,目前在网上找的lucene 和IKAnalyzer 的最新版本测试通过。内含:示例代码,以及最新jar包。 lucene lucene3.5 IKAnalyzer IKAnalyzer3.2.5 jar 中文 分词
luke3.5 可查看lucene3.5索引
lucene3.5的创建和增删改查的工程
lucene 官网的3.5源代码,标准代码,未做任何处理
基于新发布的Lucene3.5 做的一个实例,仅供参考。
lucene3.5高亮
几个经常用到的包,有中文分词器,核心包,高亮包...等等
chm格式的Lucene帮助文档,Lucene3.5
lucene3.5全文检索案例lucene+demo
用爬虫爬下来的Lucene3.5 api 可以正常使用
lucene3.5的API,虽然是英文版,不过还是很有用的
Lucene 3.5&API,最新版
这个为一个lucene3.5所写的程序例子,有助于刚入门的学习lucene的人,更快的的进入到lucene的学习中去。
lucene3.5中文分词,详细的注释,适用于初学者。有兴趣的可以看看。
基于新发布的Lucene3.5的学习,对部分模块的研究及学习心得。
介绍lucene3.5的相关技术,包括基本用法、分析器、索引建立与查询,扩展的高亮、分页、以及solr3.5的相关用法
solr_lucene3.5_lukeall-3.5.0.jar.zip
Lucene3.5视频教程(内含分享链接) 一共50集, 包含各部分讲解及源码