Expanding an array to pass into varargs - java

Given this function signature:
public static void test(final String... args) {}
I would like to fit a string and an array of strings into the args:
test("a", new String[]{"b", "c"});
But it is not possible because the second argument is not expanded.
So is it possible to expand an array to fit into varargs?
If that is not possible, what is the shortest way to construct a concatenated string array given one string and a string array? E.g:
String a = "a";
String[] b = new String[]{"b", "c"};
String[] c = // To get: "a", "b", "c"
Thank you.

You can use Guava's ObjectArrays.concat(T, T[]) method:
String a = "a";
String[] b = new String[]{"b", "c"};
String[] c = ObjectArrays.concat(a, b);
Notice the order of arguments. Invoking with (b, a) will also work, but that will append the element to the array rather than prepend (which is what you seem to want). This internally uses System.arraycopy() method.

I don't know any other shorter ways but this is pretty clean to me. With plain java (without ant libraries)
String a = "a";
String[] b = new String[] { "b", "c" };
String[] c = new String[b.length + 1];
c[0]=a;
System.arraycopy(b, 0, c, 1, b.length);
That will work no matter what is the size of b

You could just create a separate array, and put the String "a" first into that array, and then copy the array b[] into the array using System.arraycopy(), and then pass the new array to the method.

You can use StringBuilder:
StringBuilder sb = new StringBuilder();
sb.append(a);
for(String s : b) {
sb.append(s);
}
And then you can convert it back to ArrayList or array of Strings...

Shortest way which comes to my mind (BUT not clean code):
test((a + "," + Arrays.toString(b)).replaceAll("[\\[\\] ]", "").split(","));
Test:
public static void main(String[] args) {
String a = "a";
String[] b = new String[] {"b", "c"};
System.out.println(Arrays.toString(
(a + "," + Arrays.toString(b)).replaceAll("[\\[\\] ]", "").split(",")));
}
Output:
[a, b, c]

Related

How to convert values to string? [duplicate]

I want the Java code for converting an array of strings into an string.
Java 8+
Use String.join():
String str = String.join(",", arr);
Note that arr can also be any Iterable (such as a list), not just an array.
If you have a Stream, you can use the joining collector:
Stream.of("a", "b", "c")
.collect(Collectors.joining(","))
Legacy (Java 7 and earlier)
StringBuilder builder = new StringBuilder();
for(String s : arr) {
builder.append(s);
}
String str = builder.toString();
Alternatively, if you just want a "debug-style" dump of an array:
String str = Arrays.toString(arr);
Note that if you're really legacy (Java 1.4 and earlier) you'll need to replace StringBuilder there with StringBuffer.
Android
Use TextUtils.join():
String str = TextUtils.join(",", arr);
General notes
You can modify all the above examples depending on what characters, if any, you want in between strings.
DON'T use a string and just append to it with += in a loop like some of the answers show here. This sends the GC through the roof because you're creating and throwing away as many string objects as you have items in your array. For small arrays you might not really notice the difference, but for large ones it can be orders of magnitude slower.
Use Apache commons StringUtils.join(). It takes an array, as a parameter (and also has overloads for Iterable and Iterator parameters) and calls toString() on each element (if it is not null) to get each elements string representation. Each elements string representation is then joined into one string with a separator in between if one is specified:
String joinedString = StringUtils.join(new Object[]{"a", "b", 1}, "-");
System.out.println(joinedString);
Produces:
a-b-1
I like using Google's Guava Joiner for this, e.g.:
Joiner.on(", ").skipNulls().join("Harry", null, "Ron", "Hermione");
would produce the same String as:
new String("Harry, Ron, Hermione");
ETA: Java 8 has similar support now:
String.join(", ", "Harry", "Ron", "Hermione");
Can't see support for skipping null values, but that's easily worked around.
From Java 8, the simplest way I think is:
String[] array = { "cat", "mouse" };
String delimiter = "";
String result = String.join(delimiter, array);
This way you can choose an arbitrary delimiter.
You could do this, given an array a of primitive type:
StringBuffer result = new StringBuffer();
for (int i = 0; i < a.length; i++) {
result.append( a[i] );
//result.append( optional separator );
}
String mynewstring = result.toString();
Try the Arrays.deepToString method.
Returns a string representation of the "deep contents" of the specified
array. If the array contains other arrays as elements, the string
representation contains their contents and so on. This method is
designed for converting multidimensional arrays to strings
Try the Arrays.toString overloaded methods.
Or else, try this below generic implementation:
public static void main(String... args) throws Exception {
String[] array = {"ABC", "XYZ", "PQR"};
System.out.println(new Test().join(array, ", "));
}
public <T> String join(T[] array, String cement) {
StringBuilder builder = new StringBuilder();
if(array == null || array.length == 0) {
return null;
}
for (T t : array) {
builder.append(t).append(cement);
}
builder.delete(builder.length() - cement.length(), builder.length());
return builder.toString();
}
public class ArrayToString
{
public static void main(String[] args)
{
String[] strArray = new String[]{"Java", "PHP", ".NET", "PERL", "C", "COBOL"};
String newString = Arrays.toString(strArray);
newString = newString.substring(1, newString.length()-1);
System.out.println("New New String: " + newString);
}
}
You want code which produce string from arrayList,
Iterate through all elements in list and add it to your String result
you can do this in 2 ways: using String as result or StringBuffer/StringBuilder.
Example:
String result = "";
for (String s : list) {
result += s;
}
...but this isn't good practice because of performance reason. Better is using StringBuffer (threads safe) or StringBuilder which are more appropriate to adding Strings
String[] strings = new String[25000];
for (int i = 0; i < 25000; i++) strings[i] = '1234567';
String result;
result = "";
for (String s : strings) result += s;
//linear +: 5s
result = "";
for (String s : strings) result = result.concat(s);
//linear .concat: 2.5s
result = String.join("", strings);
//Java 8 .join: 3ms
Public String join(String delimiter, String[] s)
{
int ls = s.length;
switch (ls)
{
case 0: return "";
case 1: return s[0];
case 2: return s[0].concat(delimiter).concat(s[1]);
default:
int l1 = ls / 2;
String[] s1 = Arrays.copyOfRange(s, 0, l1);
String[] s2 = Arrays.copyOfRange(s, l1, ls);
return join(delimiter, s1).concat(delimiter).concat(join(delimiter, s2));
}
}
result = join("", strings);
// Divide&Conquer join: 7ms
If you don't have the choise but to use Java 6 or 7 then you should use Divide&Conquer join.
String array[]={"one","two"};
String s="";
for(int i=0;i<array.length;i++)
{
s=s+array[i];
}
System.out.print(s);
Use Apache Commons' StringUtils library's join method.
String[] stringArray = {"a","b","c"};
StringUtils.join(stringArray, ",");
When we use stream we do have more flexibility, like
map --> convert any array object to toString
filter --> remove when it is empty
join --> Adding joining character
//Deduplicate the comma character in the input string
String[] splits = input.split("\\s*,\\s*");
return Arrays.stream(splits).filter(StringUtils::isNotBlank).collect(Collectors.joining(", "));
If you know how much elements the array has, a simple way is doing this:
String appendedString = "" + array[0] + "" + array[1] + "" + array[2] + "" + array[3];

Joining two lists together separated by a comma in Java

I have two String lists (a and b) that I wanna join with a comma after each element. I want the elements of list a to be first. I'm also stuck on Java 7
I tried the following but it doesn't work:
StringUtils.join(a, ", ").join(b, ", ");
This works :
ArrayList<String> aAndB = new ArrayList<>();
aAndB.addAll(a);
aAndB.addAll(b);
StringUtils.join(aAndB, ", ");
Is there a shorter way of doing this?
You do not need StringUtils By default List toString() displays elements in comma separated format.
System.out.println (new StringBuilder (aAndB.toString())
.deleteCharAt (aAndB.toString().length ()-1)
.deleteCharAt (0).toString ());
The only thing you need to do is delete square brackets
You can use the guava library like so:
String [] a = {"a", "b", "c"};
String [] b = {"d", "e"};
//using Guava library
String [] joined = ObjectArrays.concat(a, b, String.class);
System.out.println("Joined array : " + Arrays.toString(joined));
// Output: "Joined array : [a, b, c, d, e]"
To get short code you could :
String res = String.join(",", a) + "," + String.join(",", b);
Since you are using Java 7, you could write a static method to perform the task.
List<String> a = Arrays.asList("a", "b", "c");
List<String> b = Arrays.asList("d", "e", "f");
String s = join(",", a, b);
System.out.println(s);
List<Integer> aa = Arrays.asList(101, 102, 103);
List<Integer> bb = Arrays.asList(104, 105, 106);
String ss = join(":", aa, bb);
System.out.println(ss);
}
public static <T> String join(String delimiter, List<T>... lists) {
StringBuilder sb = new StringBuilder();
for (List<T> list : lists) {
for (T item : list) {
sb.append(delimiter);
sb.append(item);
}
}
return sb.substring(delimiter.length()).toString();
}
}
This prints.
a,b,c,d,e,f
101:102:103:104:105:106

Check if String contains part of other string Java

I have some strings defined in my Java application, like so:
m3 = "T, V, E";
m2 = "T, W, E";
as an example.
Now I need to check, which parts of the strings match. So in this case, I would want a string m4, containing T, E, as a result.
In that case for example:
m1 = "A, S";
m3 = "T, V, E";
i would want an empty (but declared) string.
Or is there a better way, to do it with another method then with strings? I'm actually declaring those strings by hand. Would an array be better? If yes, how could I do it with arrays?
In Java 8 you can proceed as below :
String s1 = "T,V,E";
String s2 = "T,W,E";
List<String> l1 = Arrays.asList(s1.split(","));
List<String> l2 = Arrays.asList(s2.split(","));
List<String> result = l1.stream().filter(l2::contains).collect(Collectors.toList());
System.out.println(String.join(",", result));
The result is "T,E" as expected.
You can achieve this in many ways. One of the ways is using Set.
First, split m1 the characters by (comma) and add it to HashSet. Then split the m2 characters and add it to ArrayList. Now by the for loop try to add the ArrayList characters to HashSet. You will get false from the add method (Set.add()) if it is not added (because the character is already there). If you get false print the character or add it to another ArrayList.
String m3 = "T, V, E";
String m2 = "T, W, E";
Set<String> set = new HashSet<>(Arrays.asList(m3.split(",")));
List<String> list = Arrays.asList(m2.split(","));
for (String s : list) {
if (!set.add(s)) {
System.out.println(s);
}
}
Result will be T and E
The appropriate data structure is Set.
Set<String> m3 = new TreeSet<>();
Collections.addAll(m3, "T", "V", "E");
Collections.addAll(m3, "T, V, E".split(",\\s*")); // Alternatively.
Set<String> m2 = new HashSet<>();
Collections.addAll(m2, "T", "W", "E");
Set<String> m5 = new TreeSet<>(m2);
m5.retainAll(m3);
Java 9:
Set<String> m3 = Set.of("T", "V", "E");
Set<String> m2 = Set.of("T", "W", "E");
you can use the split() function as the following
String a="A, B, C, D";
String b="B, C, D";
String[] a_chars =a.split(", "); //returns array with A B C D
String[] b_chars =b.split(", "); //returns array with B C D
this whay you have 2 arrays of strings now you can compare them using 2 (for) loops
String result="";
for(int c=0;c<a_chars.length;c++)
{
for(int d=0;d<b_chars.length;d++)
{
if(a_chars[c].equals(b_chars[d]))
{
result+=a_chars[c]+", ";
}
}
}
now you have the result string like this result="B, C, D, "
check of the length of result is greater than zero
if so erase the lase 2 characters which are ,
if(result.length()>0)result=result.substring(0,result.length()-2);
if the length of the result string is zero that means there is no matching letters so no need to modify it
If all your String follows this pattern : ..., ..., .... , you could split them and filter only each String that is contained in the two arrays.
You can then collect them into a List and join them with , :
List<String> commonStrings = Arrays.stream(m2.split(",\\s"))
.flatMap(s-> Arrays.stream(m3.split(",\\s"))
.filter(s2.equals(s)))
.collect(Collectors.toList());
String joinedString = String.join(",", commonStrings);
Note that this code doesn't return the exact number of equals occurrences in the two Strings.
So if one String contains two A and the other one A, you will get two A in the result.
If it doesn' matter and you want to get only distinct String, you can invoke distinct() before collect().
Otherwise, to return the exact number of equals occurrences, during the processing, as soon as a String part is consumed (A for example) as the two parts are equal in the two Strings, you could create new Strings from the actual Strings but by removing this String part .
String s1 = "T,V,E";
String s2 = "T,W,E";
List<String> l1 = Arrays.asList(s1.split(","));
List<String> l2 = Arrays.asList(s2.split(","));
List<String> result = l1.stream().filter(l2::contains).collect(Collectors.toList());
System.out.println(String.join(",", result));
result = T&E
This is good answer I will tell you too this

One-liner to count number of occurrences of String in a String[] in Java?

I have an array of String:
String[] myArray = {"A", "B", "B", "C"};
Is there a quick way to count the number of occurrences of a string in that array? Yes, I know I can iterate and do the count myself:
int count = 0;
String whatToFind = "B";
for (String s : myArray) {
if (s.equals(whatToFind)) {
++count;
}
}
But I was wondering if there was a utility function for this. I couldn't find anything in Arrays or ArrayUtils. Is it possible to do this with a one-liner?
You can use the frequency method:
List<String> list = Arrays.asList(myArray);
int count = Collections.frequency(list, "B");
or in one line:
int count = Collections.frequency(Arrays.asList(myArray), "B");
With Java 8 you can also write:
long count = Arrays.stream(myArray).filter(s -> "B".equals(s)).count();
Or with a method reference:
long count = Arrays.stream(myArray).filter("B"::equals).count();
You can also try using Guava which is full of useful utilities. Using below code, you can count the frequency via Multiset:
public static void main(final String[] args) {
String[] myArray = {"A", "B", "B", "C"};
Multiset<String> wordsMultiset = HashMultiset.create();
wordsMultiset.addAll(new ArrayList<String>(Arrays.asList(myArray)));
int counts=wordsMultiset.count("B");
System.out.println(counts);
}
Although I know that you are looking for a single liner, but Guava is full of many more utils which are not possible with routine java utils.

Convert array of strings into a string in Java

I want the Java code for converting an array of strings into an string.
Java 8+
Use String.join():
String str = String.join(",", arr);
Note that arr can also be any Iterable (such as a list), not just an array.
If you have a Stream, you can use the joining collector:
Stream.of("a", "b", "c")
.collect(Collectors.joining(","))
Legacy (Java 7 and earlier)
StringBuilder builder = new StringBuilder();
for(String s : arr) {
builder.append(s);
}
String str = builder.toString();
Alternatively, if you just want a "debug-style" dump of an array:
String str = Arrays.toString(arr);
Note that if you're really legacy (Java 1.4 and earlier) you'll need to replace StringBuilder there with StringBuffer.
Android
Use TextUtils.join():
String str = TextUtils.join(",", arr);
General notes
You can modify all the above examples depending on what characters, if any, you want in between strings.
DON'T use a string and just append to it with += in a loop like some of the answers show here. This sends the GC through the roof because you're creating and throwing away as many string objects as you have items in your array. For small arrays you might not really notice the difference, but for large ones it can be orders of magnitude slower.
Use Apache commons StringUtils.join(). It takes an array, as a parameter (and also has overloads for Iterable and Iterator parameters) and calls toString() on each element (if it is not null) to get each elements string representation. Each elements string representation is then joined into one string with a separator in between if one is specified:
String joinedString = StringUtils.join(new Object[]{"a", "b", 1}, "-");
System.out.println(joinedString);
Produces:
a-b-1
I like using Google's Guava Joiner for this, e.g.:
Joiner.on(", ").skipNulls().join("Harry", null, "Ron", "Hermione");
would produce the same String as:
new String("Harry, Ron, Hermione");
ETA: Java 8 has similar support now:
String.join(", ", "Harry", "Ron", "Hermione");
Can't see support for skipping null values, but that's easily worked around.
From Java 8, the simplest way I think is:
String[] array = { "cat", "mouse" };
String delimiter = "";
String result = String.join(delimiter, array);
This way you can choose an arbitrary delimiter.
You could do this, given an array a of primitive type:
StringBuffer result = new StringBuffer();
for (int i = 0; i < a.length; i++) {
result.append( a[i] );
//result.append( optional separator );
}
String mynewstring = result.toString();
Try the Arrays.deepToString method.
Returns a string representation of the "deep contents" of the specified
array. If the array contains other arrays as elements, the string
representation contains their contents and so on. This method is
designed for converting multidimensional arrays to strings
Try the Arrays.toString overloaded methods.
Or else, try this below generic implementation:
public static void main(String... args) throws Exception {
String[] array = {"ABC", "XYZ", "PQR"};
System.out.println(new Test().join(array, ", "));
}
public <T> String join(T[] array, String cement) {
StringBuilder builder = new StringBuilder();
if(array == null || array.length == 0) {
return null;
}
for (T t : array) {
builder.append(t).append(cement);
}
builder.delete(builder.length() - cement.length(), builder.length());
return builder.toString();
}
public class ArrayToString
{
public static void main(String[] args)
{
String[] strArray = new String[]{"Java", "PHP", ".NET", "PERL", "C", "COBOL"};
String newString = Arrays.toString(strArray);
newString = newString.substring(1, newString.length()-1);
System.out.println("New New String: " + newString);
}
}
You want code which produce string from arrayList,
Iterate through all elements in list and add it to your String result
you can do this in 2 ways: using String as result or StringBuffer/StringBuilder.
Example:
String result = "";
for (String s : list) {
result += s;
}
...but this isn't good practice because of performance reason. Better is using StringBuffer (threads safe) or StringBuilder which are more appropriate to adding Strings
String[] strings = new String[25000];
for (int i = 0; i < 25000; i++) strings[i] = '1234567';
String result;
result = "";
for (String s : strings) result += s;
//linear +: 5s
result = "";
for (String s : strings) result = result.concat(s);
//linear .concat: 2.5s
result = String.join("", strings);
//Java 8 .join: 3ms
Public String join(String delimiter, String[] s)
{
int ls = s.length;
switch (ls)
{
case 0: return "";
case 1: return s[0];
case 2: return s[0].concat(delimiter).concat(s[1]);
default:
int l1 = ls / 2;
String[] s1 = Arrays.copyOfRange(s, 0, l1);
String[] s2 = Arrays.copyOfRange(s, l1, ls);
return join(delimiter, s1).concat(delimiter).concat(join(delimiter, s2));
}
}
result = join("", strings);
// Divide&Conquer join: 7ms
If you don't have the choise but to use Java 6 or 7 then you should use Divide&Conquer join.
String array[]={"one","two"};
String s="";
for(int i=0;i<array.length;i++)
{
s=s+array[i];
}
System.out.print(s);
Use Apache Commons' StringUtils library's join method.
String[] stringArray = {"a","b","c"};
StringUtils.join(stringArray, ",");
When we use stream we do have more flexibility, like
map --> convert any array object to toString
filter --> remove when it is empty
join --> Adding joining character
//Deduplicate the comma character in the input string
String[] splits = input.split("\\s*,\\s*");
return Arrays.stream(splits).filter(StringUtils::isNotBlank).collect(Collectors.joining(", "));
If you know how much elements the array has, a simple way is doing this:
String appendedString = "" + array[0] + "" + array[1] + "" + array[2] + "" + array[3];

Categories