Hot questions for Operating Array in Guava
Lists.newArrayList vs new ArrayList
Question: What is the best construction for creating a
List of Strings? Is it
Lists.newArrayList() (from guava) or
is it just a personal preference?
or is it just Type generic type inference?
or is there any theoretical or practical value in using
Answer: The guava builder saves typing the type arguments multiple times. Compare:
In Java it's a bit obsolete though, because you have the diamond operator:
Is there a way to find common elements in multiple lists?
Question: I have a list of integer arrays. I need to find the common elements between those.
There are no duplicates in the arrays as well.
Is there a straight forward way to do this?
Answer: You can transform the lists to sets, and use
intersection method offered by Guava, Here is a little example:
Transform a Generic Array Efficiently Using Guava
Question: I'm searching in the Guava API for a way to apply a function to an array without having to convert it to a Collection first.
So as a summary (in case you don't know exactly what I'm talking about), this is what I've found so far that you can do with Guava in order to apply a function to an array as I said:
But I would like to be able to do something like this instead:
Ideally the functionality I'm talking about would be type-safe.
Answer: Maybe you can try:
Example of how to use it (actually this is a test):
I want to point out that the example code is using vargargs syntax (it's a test, so it's a fun way to use it) but you can just obviously passed an array object instead.
Also, I must add that you need to be careful with this approach when using it on an environment sensitive to reflection performance hits (such as Android), because of the runtime creation of the new array using reflection (java.lang.reflect.newInstance(...)). In the other hand, the same holds true if you are using
toArray(T) method of collections and the passed array's size isn't enough to hold all the collection elements (in which case a new array is allocated at run time, just as with my code), so maybe this code doesn't represent a problem if anyway you are using that method in the way I said (in any case you could easily change this code to fit your needs).
filtering an ArrayList using an object's field
Question: I have an ArrayList which is filled by Objects.
My object class called
Article which has two fields;
I want to filter my List using the
desArt field, and for test I used the String "test".
I used the Guava from google which allows me to filter an ArrayList.
this is the code I tried :
but this code isn't working.
Answer: This is normal:
Predicates.containsPattern() operates on
CharSequence, which your
gestionStock.Article object does not implement.
You need to write your own predicate:
Java - Append quotes to strings in an array and join strings in an array
Question: I would like to append double quotes to strings in an array and then later join them as a single string (retaining the quotes). Is there any String library which does this? I have tried Apache commons StringUtils.join and the Joiner class in Google guava but couldn't find anything that appends double quotes.
My input would be an array as mentioned below:
Required output should be as mentioned below:
I know I can loop through the array and append quotes. But I would like a more cleaner solution if there is one.
1. With Java 8+
Java 8 has
Collectors.joining() and its overloads. It also has
Using a Stream and a Collector
2. Using Guava
3. No libraries
Note: all the solutions assume that
listOfStrings is a
List<String> rather than a
String. You can convert a
String into a
Arrays.asList(arrayOfStrings). You can get a
Stream<String> directly from a