stringbuilder

How to convert byte array to hex format in Java

蹲街弑〆低调 提交于 2019-11-29 03:50:40
I know that you can use printf and also use StringBuilder.append(String.format("%x", byte)) to convert values to HEX values and display them on the console. But I want to be able to actually format the byte array so that each byte is displayed as HEX instead of decimal. Here is a section of my code that I have already that does the first two ways that I stated: if(bytes > 0) { byteArray = new byte[bytes]; // Set up array to receive these values. for(int i=0; i<bytes; i++) { byteString = hexSubString(hexString, offSet, CHARSPERBYTE, false); // Isolate digits for a single byte. Log.d("HEXSTRING"

Default capacity of StringBuilder

↘锁芯ラ 提交于 2019-11-29 02:51:14
What is the default capacity of a StringBuilder ? And when should (or shouldn't) the default be used? endian The Venerable J. Skeet has provided a good analysis of precisely this problem: https://jonskeet.uk/csharp/stringbuilder.html The default capacity of StringBuilder is 16 characters (I used .NET Reflector to find out). baretta Default is 16, which seems to be the default capacity of any type of array or list in the .NET framework. The less number of reallocations you need on your StringBuilder, the better it is. Meanwhile, it is unnecessary to allocate much more than is needed, too. I

Has anyone implemented a Regex and/or Xml parser around StringBuilders or Streams?

余生颓废 提交于 2019-11-29 02:00:57
I'm building a stress-testing client that hammers servers and analyzes responses using as many threads as the client can muster. I'm constantly finding myself throttled by garbage collection (and/or lack thereof), and in most cases, it comes down to strings that I'm instantiating only to pass them off to a Regex or an Xml parsing routine. If you decompile the Regex class, you'll see that internally , it uses StringBuilders to do nearly everything, but you can't pass it a string builder; it helpfully dives down into private methods before starting to use them, so extension methods aren't going

StringBuilder线程为什么不安全

断了今生、忘了曾经 提交于 2019-11-29 01:37:37
我们要知道StringBuilder和StringBuffer的内部实现跟String类一样,都是通过一个char数组存储字符串的,不同的是String类里面的char数组是final修饰的,是不可变的,而StringBuilder和StringBuffer的char数组是可变的。 首先通过一段代码去看一下多线程操作StringBuilder对象会出现什么问题 我们能看到这段代码创建了10个线程,每个线程循环1000次往StringBuilder对象里面append字符。正常情况下代码应该输出10000,但是实际运行会输出什么呢? 我们看到输出了“9326”,小于预期的10000,并且还抛出了一个ArrayIndexOutOfBoundsException异常(异常不是必现)。 1、为什么输出值跟预期值不一样 我们先看一下StringBuilder的两个成员变量(这两个成员变量实际上是定义在AbstractStringBuilder里面的,StringBuilder和StringBuffer都继承了AbstractStringBuilder) 再看StringBuilder的append()方法: StringBuilder的append()方法调用的父类AbstractStringBuilder的append()方法 我们先不管代码的第五行和第六行干了什么,直接看第七行

Is using a StringBuilder a right thing to do in F#?

你。 提交于 2019-11-29 01:03:17
StringBuiler is a mutable object, F# encourages employing immutability as much as possible. So one should use transformation rather than mutation. Does this apply to StringBuilder when it comes to building a string in F#? Is there an F# immutable alternative to it? If so, is this alternative as efficient? A snippet I think that using StringBuilder in F# is perfectly fine - the fact that sb.Append returns the current instance of StringBuilder means that it can be easily used with the fold function. Even though this is still imperative (the object is mutated), it fits reasonably well with the

Java入门系列之StringBuilder、StringBuffer(三)

♀尐吖头ヾ 提交于 2019-11-29 00:52:13
前言 上一节我们讲解了字符串的特性,除了字符串类外,还有两个我们也会经常用到的类,那就是StringBuffer和StringBuilder。因为字符串不可变,所以我们每次对字符串的修改比如通过连接concat、trim等都会创建一个新的字符串对象,那么我们如何在不创建字符串垃圾(大量临时的字符串)的 情况下操作字符串呢?答案则是使用StringBuffer和StringBuilder,StringBuffer是旧类,但是在Java 5中新增了StringBuilder,并且在Enum,Generics等和Java中的Autoboxing方面进行了重大改进。 StringBuffer VS StringBuilder String和StringBuffer之间的主要区别是String是不可变的,而StringBuffer、StringBuilder可变,这也就意味着我们可以在创建StringBuffer对象时修改它而不创建任何新对象,这个可变属性使StringBuffer成为处理Java中的字符串的理想选择,同时,这种可变性更加节省时间并且资源消耗更少。当然我们可以通过toString将StringBuffer转换为String。这两个类几乎相同,它们使用具有相同名称的方法返回相同的结果

java outOfMemoryError with stringbuilder

情到浓时终转凉″ 提交于 2019-11-29 00:10:24
I'm getting a java outOfMemoryError when I call this method - i'm using it in a loop to parse many large files in sequence. my guess is that result.toString() is not getting garbage collected properly during the loop. if so, how should i fix it? private String matchHelper(String buffer, String regex, String method){ Pattern abbrev_p = Pattern.compile(regex);//norms U.S.A., B.S., PH.D, PH.D. Matcher abbrev_matcher = abbrev_p.matcher(buffer); StringBuffer result = new StringBuffer(); while (abbrev_matcher.find()){ abbrev_matcher.appendReplacement(result, abbrevHelper(abbrev_matcher)); } abbrev

Java String,看这篇就够了

别等时光非礼了梦想. 提交于 2019-11-28 23:05:02
String,是Java中最重要的类。这句肯定的推断不是Java之父詹姆斯·高斯林说的,而是沉默王二说的,因此你不必怀疑它的准确性。 关于字符串,有很多的面试题,但我总觉得理论知识绕来绕去没多大意思。你比如说: String cmower = new String("沉默王二"); 定义了几个对象? 我总觉得问我这样的问题,就好像是在拷问我:“既然你家买了冰箱,你难道不应该知道冰箱制冷的原理?” 再说,为什么要用 String cmower = new String("沉默王二"); 而不是 String cmower = "沉默王二"; ? 我劝各位面试官不要再缠住这样的问题不放了,切记“学以致用”。理论知识如果一直是在绕弯弯,那真的毫无价值。如果要我来做面试官,我想要问的问题是:“你平常是怎么判断两个字符串相等的?是用equals()还是==?” 前言就说这么多。接下来,我们来探讨几个实用的知识点。 01、 字符串是不可变的 我们来看一下String类的定义: public final class String implements java.io.Serializable, Comparable<String>, CharSequence { } 可以发现,String类是final类型的,因此不能被继承。 如果类可以被继承,那么就会破坏类的不可变性机制

Java 8中字符串拼接新姿势:StringJoiner

天涯浪子 提交于 2019-11-28 23:00:47
Java 8中字符串拼接新姿势:StringJoiner 在 为什么阿里巴巴不建议在for循环中使用”+”进行字符串拼接 一文中,我们介绍了几种Java中字符串拼接的方式,以及优缺点。其中还有一个重要的拼接方式我没有介绍,那就是Java 8中提供的StringJoiner ,本文就来介绍一下这个字符串拼接的新兵。 如果你想知道一共有多少种方法可以进行字符串拼接,教你一个简单的办法,在Intellij IDEA中,定义一个Java Bean,然后尝试使用快捷键自动生成一个toString方法,IDEA会提示多种toString生成策略可供选择。  目前我使用的IDEA的toString生成策略默认的是使用JDK 1.8提供的StringJoiner。 介绍 StringJoiner是java.util包中的一个类,用于构造一个由分隔符分隔的字符序列(可选),并且可以从提供的前缀开始并以提供的后缀结尾。虽然这也可以在StringBuilder类的帮助下在每个字符串之后附加分隔符,但StringJoiner提供了简单的方法来实现,而无需编写大量代码。 StringJoiner类共有2个构造函数,5个公有方法。其中最常用的方法就是add方法和toString方法,类似于StringBuilder中的append方法和toString方法。 用法 StringJoiner的用法比较简单

35个Java代码优化的细节,你知道几个?

不羁岁月 提交于 2019-11-28 22:54:46
前言 代码 优化 ,一个很重要的课题。可能有些人觉得没用,一些细小的地方有什么好修改的,改与不改对于代码的运行效率有什么影响呢?这个问题我是这么考虑的,就像大海里面的鲸鱼一样,它吃一条小虾米有用吗?没用,但是,吃的小虾米一多之后,鲸鱼就被喂饱了。 代码优化也是一样,如果项目着眼于尽快无BUG上线,那么此时可以抓大放小,代码的细节可以不精打细磨;但是如果有足够的时间开发、维护代码,这时候就必须考虑每个可以优化的细节了,一个一个细小的优化点累积起来,对于代码的运行效率绝对是有提升的。 代码优化的目标是: 减小代码的体积 提高代码运行的效率 代码优化细节 1、尽量指定类、方法的final修饰符 带有final修饰符的类是不可派生的。在Java核心API中,有许多应用final的例子,例如java.lang.String,整个类都是final的。为类指定final修饰符可以让类不可以被继承,为方法指定final修饰符可以让方法不可以被重写。如果指定了一个类为final,则该类所有的方法都是final的。Java编译器会寻找机会内联所有的final方法,内联对于提升Java运行效率作用重大,具体参见Java运行期优化。 此举能够使性能平均提高50% 。 2、尽量重用对象 特别是String对象的使用,出现字符串连接时应该使用StringBuilder/StringBuffer代替