stringbuilder

StringBufferBuilder

安稳与你 提交于 2019-12-05 23:12:28
String StringBuffer StringBuilder的异同 后两者可变的字符序列 StringBuffer 线程安全的 效率低 new char[16 ] StringBuilder 线程不安全 效率低 JDK5.0新增 如果要添加的数据底层数组盛不下,需要扩容底层的数组 默认情况下,扩容为原来的2倍+2,同时将原有数组的元素复制到新的数组中      开发中建议大家使用StringBuffer(int capacity) 来源: https://www.cnblogs.com/ergePython/p/11949781.html

java中String、StringBuffer和StringBuilder的区别

痴心易碎 提交于 2019-12-05 22:09:43
java中用于处理字符串常用的有三个类: 1、java.lang.String 2、java.lang.StringBuffer 3、java.lang.StrungBuilder 三者共同之处: 都是final类,不允许被继承 ,主要是从性能和安全性上考虑的,因为这几个类都是经常被使用着,且考虑到防止其中的参数被参数修改影响到其他的应用。 StringBuffer是线程安全 ,可以不需要额外的同步用于多线程中; StringBuilder是非同步,运行于多线程中就需要使用着单独同步处理,但是速度就比StringBuffer快多了; StringBuffer与StringBuilder两者共同之处:可以通过append、indert进行字符串的操作。 String实现了三个接口:Serializable、Comparable<String>、CarSequence StringBuilder只实现了两个接口Serializable、CharSequence,相比之下String的实例可以通过compareTo方法进行比较,其他两个不可以。 这三个类之间的区别主要是在两个方面,即运行速度和线程安全这两方面。    1、首先说运行速度 快慢为:StringBuilder > StringBuffer > String    String最慢的原因:String为字符串常量

Maximum number of characters stringbuilder can accommodate

随声附和 提交于 2019-12-05 21:35:19
问题 I need to write 10,000 x 30,000 characters. will a single stringbuilder be able to acomodate all characters or should I think of an array of stringbuilders? I do not have access to the test cases, so I cannot actually verify it myself. Hope I will find the answer here. Thanks in advance. EDIT: I tried to add 10000 x 30000 characters using a loop. I get the following exceptions. Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.util.Arrays.copyOf(Arrays.java:2367)

How can a StringBuilder best be converted to a String[]?

我们两清 提交于 2019-12-05 21:35:00
问题 The following code sort of works, but fixes the number of elements in String[]. Is there a way to make a String[] add the number of elements needed dynamically? private static StringBuilder names = new StringBuilder(); ... public String[] getNames() { int start = 0; int end = 0; int i = 0; String[] nameArray = {"","","",""}; while (-1 != end) { end = names.indexOf(TAB, start); nameArray[i++] = names.substring(start, end); start = ++end; // The next name is after the TAB } return nameArray; }

String Vs Stringbuffer as HashMap key

自作多情 提交于 2019-12-05 21:22:23
I am trying to understand why String and Stringbuilder/StringBuffer are treated differently when used as Hashmap keys. Let me make my confusion clearer with the following illustrations: Example #1, using String: String s1 = new String("abc"); String s2 = new String("abc"); HashMap hm = new HashMap(); hm.put(s1, 1); hm.put(s2, 2); System.out.println(hm.size()); Above code snippet prints '1'. Example #2, using StringBuilder(or StringBuffer): StringBuilder sb1 = new StringBuilder("abc"); StringBuilder sb2 = new StringBuilder("abc"); HashMap hm = new HashMap(); hm.put(sb1, 1); hm.put(sb2, 2);

Java中测试StringBuilder、StringBuffer、String在字符串拼接上的性能

末鹿安然 提交于 2019-12-05 19:31:30
String:   1,是字符串常量,一旦创建就不能修改。对于已经存在了的String对象的修改都是重新创建一个新的对象,然后把新的值保存进去。   2,String也是final类,不能被继承。   3,而且String是对象而不是基本类型。   4,string覆盖了equals方法和hashCode()方法。 StingBuffer:   1,是字符串可变对象,可以对字符串进行操作,修改字符串原有值时不会新建一个对象。   2,执行效率较慢,但是线程安全   3,StringBuffer没有覆盖equals方法和hashCode()方法。   4,可以动态的构造字符数据,append()方法。 StringBuilder:   1,也是字符串可变对象,同StringBuffer一样,可以对字符串进行操作,也不会新建对象。   2,执行效率高效,但是线程不安全。 Java中测试StringBuilder、StringBuffer、String在字符串拼接上的性能 应一个大量字符串拼接的任务 测试一下StringBuilder、StringBuffer、String在操作字符串拼接时候的性能 性能上理论是StringBuilder > StringBuffer > String 测试方法: 创建一个StirngTest类含有静态常量字符串是26个字母,以及一个循环轮数

Is it a Good Practice to Write HTML Using a StringBuilder in my ASP.NET Codebehind?

纵然是瞬间 提交于 2019-12-05 19:25:03
I'm interested to hear from other developers their opinion on an approach that I typically take. I have a web application, asp.net 2.0, c#. What I usually do to write out drop downs, tables, input controls, etc. is in the code behind use StringBuilder and write out something like sb.Append(" I don't find myself using to many .net controls as I typically write out the html in the code behind. When I want to use jQuery or call JavaScript I just put that function call in my sb.Append tag like sb.Append("td...onblur='fnCallJS()'. I've gotten pretty comfortable with this approach. For data access I

When should we change a String to a Stringbuilder?

元气小坏坏 提交于 2019-12-05 19:16:02
In an application a String is a often used data type. What we know, is that the mutation of a String uses lots of memory. So what we can do is to use a StringBuilder/StringBuffer. But at what point should we change to StringBuilder? And what should we do, when we have to split it or to remplace characters in there? eg: //original: String[] split = string.split("?"); //better? : String[] split = stringBuilder.toString().split("?); or //original: String replacedString = string.replace("l","st"); //better? : String replacedString = stringBuilder.toString().replace("l","st"); //or StringBuilder

不要再使用String字符串拼接了,这样优化性能提升十几倍

血红的双手。 提交于 2019-12-05 16:45:25
相信很多Java开发的小伙伴对String字符串拼接都不陌生,或多或少都使用过,因为方便使用。但殊不知频繁使用字符串拼接会严重降低性能。今天我们看一看如何让String拼接提升十几倍的性能。 那么应该从编译讲起,Java 从 JDK5 开始,便在编译期间进行了优化。如果是无变量的字符串拼接,那么在编译期间值都确定了, javac 工具会直接把它编译成一个字符常量。如果有变量的字符串拼接,在编译期间变量的值是无法确定,所以在运行期间会生成一个StringBuilder 对象。从这里可以看出两者的区别在于有没有生成StringBuilder对象,如果频繁使用有变量的字符串拼接,那么也就会生成很多StringBuilder对象,必然对性能就会造成影响。 我们使用循环内的字符串拼接举个例子 // 循环中使用字符串拼接 String a = "0"; Long start = System.currentTimeMillis(); for (int i = 1; i < 10000; i++) { a = a + i; } System.out.println("字符串拼接执行一万次时间:" + (System.currentTimeMillis() - start) + " 毫秒"); // 循环外定义StringBuilder start = System

String,StringBuilder(字符串的缓冲区)

给你一囗甜甜゛ 提交于 2019-12-05 16:44:50
String,StringBuilder(字符串的缓冲区) 1.API应用程序接口 快速使用API步骤:A:打开帮助文档 B:点击显示,找到索引,看到输入框 C:你要学习什么内容,你就在框框里面输入什么内容 举例:Random D:看包 java.lang包下的类在使用的时候是不需要导包的 E:看类的描述 Random类是用于生成随机数的类 F:看构造方法 Random():无参构造方法 G:看成员方法 public int nextInt(int n):产生的是一个[0,n)范围内的随机数 2.Scanner类 作用:用Scanner类的方法可以完成接收键盘录入的数据,接收的数据类型为基本数据类型和字符串类型. public String nextLine():获取键盘录入字符串数据 3.Object类 作用::是类层次结构的根类,所有的类都直接的或者间接的继承自该类 构造方法:Object() 4.String类的构造方法 String(字符串是一个不可变的字符序列) String(String original):把字符串数据封装成字符串对象 String(char[] value):把字符数组的数据封装成字符串对象 String(char[] value, int index, int count):把字符数组的一部分数据封装成字符串对象 String类创建对象的特点: A