Java 中的字符串与 []byte 字节数组
一、字符串
1.比较 String、HashSet、List 中的 contains 方法
其中, String、List 都使用了 indexOf 方法,本质是遍历,时间效率为 O(n)。而 HashSet 使用了计算 hash值的方式,时间效率为 O(1) 级别。
2.String 中为什么需要 hashCode 方法?
从String 源码可以看到其底层实现是 char[],即本质是字符数组。包括索引(indexOf)及大部分功能(比如 equals 方法)实现都是使用数组。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public final class String implements xxx { private final char value[]; /** Cache the hash code for the string */ private int hash; // Default to 0 public String() { this .value = "" .value; } public String(String original) { this .value = original.value; this .hash = original.hash; } |
为什么还需要 hash 值呢?原因是 String 常作为 HashSet、HashMap 容器的 key,因此需要获取 key的哈系值,貌似 switch case 方法也是根据 hash 来判别,而不是用 equals。
3.compareTo
实现了 Comparable 接口,此接口强行对实现它的每个类的对象进行整体排序。这种排序被称为类的自然排序,类的 compareTo 方法被称为它的自然比较方法。String 里是通过数组索引实现的字典序。
实现此接口的对象列表(和数组)可以通过 Collections.sort
(和 Arrays.sort
)进行自动排序。实现此接口的对象可以用作有序映射中的键或有序集合中的元素,无需指定比较器。
4. []char 数组的打印语句与普通数组的打印语句
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public void test() { String str = "Good Java" ; char [] chars = str.toCharArray(); System.out.println(chars); System.out.println(Arrays.toString(chars)); byte [] bytes = str.getBytes(); System.out.println(bytes); System.out.println(Arrays.toString(bytes)); int [] intArr = new int []{ 3 , 4 , 2 , 5 }; System.out.println(intArr); System.out.println(Arrays.toString(intArr)); } |
输出结果
1 2 3 4 5 6 | Good Java [G, o, o, d, , J, a, v, a] [B @47089e5f [ 71 , 111 , 111 , 100 , 32 , 74 , 97 , 118 , 97 ] [I @4141d797 [ 3 , 4 , 2 , 5 ] |
说明:字符数组 print 是字符串,而非字符数组 print 一般是数组地址。
5.获取字节数组与获取字符数组
1 2 3 4 5 6 7 8 | //使用指定的字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。 byte [] getBytes(String charsetName) //将字符从此字符串复制到目标字符数组。 void getChars( int srcBegin, int srcEnd, char [] dst, int dstBegin) //将此字符串转换为一个新的字符数组。 public char [] toCharArray() |
6. 其它方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | boolean endsWith(String suffix) boolean startsWith(String prefix, int toffset) boolean matches(String regex) String trim() // 返回字符串的副本,忽略前导空白和尾部空白。 String replaceAll(String regex, String replacement) String[] split(String regex, int limit) static String format(String format, Object... args) byte [] getBytes(String charsetName) char [] toCharArray() /* StringBuilder 的相关方法 */ StringBuilder( int capacity) //默认容量为 16 StringBuilder append( double d) int length() int capacity() StringBuilder reverse() |
7 “重载符号”+、+= 与 concat
str1 += str2 :底层用的 StringBuilder 效率更高,但是不够灵活,在某些情况下效率反而较低。
1 | str1 = new StringBuilder().append(str1).append(str2).toString(); |
concat:返回一个新的字符串,在字符串很少时,效率稍高。
StringBuilder:使用 append 拼接,在多段字符串拼接时效率高。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | @org .junit.Test public void test() { int times = 100000 ; String s1 = "" ; String s2 = "" ; StringBuilder s3 = new StringBuilder( "" ); long a = System.currentTimeMillis(); for ( int i = 0 ;i < times ; i ++){ s1 += "a" ; } long b = System.currentTimeMillis(); for ( int i = 0 ;i < times ; i ++){ s2 = s2.concat( "a" ); } long c = System.currentTimeMillis(); for ( int i = 0 ;i < times ; i ++){ s3.append( "a" ); } long d = System.currentTimeMillis(); System.out.print((b-a) + " | " + (c-b) + " | " + (d-c)); } |
输出结果
1 | 7289 | 1593 | 5 |
8. 字面量与 intern 方法
待更新...
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix