I am trying to learn Guava. However I am not very successful in it as there are very less tutorials.
Can any body tell me how to write this code in Guava.
import java.util.*;
public class list {
public static void main(String[] args) {
List l = new ArrayList();
for (int i = 1; i < 6; i++) {
l.add(i);
}
Iterator it = l.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
Iterating over a list can also be achieved using a while loop. The block of code inside the loop executes until the condition is true. A loop variable can be used as an index to access each element.
A for-each loop uses an iterator under the covers. The difference is just readability (and therefore maintainability).
The upcoming version 10 of Guava will make this kind of initialization much less painful:
List<Integer> l = Lists.newArrayList(
Ranges.closed(1, 5).asSet(DiscreteDomains.integers())
);
for (Integer item : l) {
System.out.println(item);
}
(There is no release 10 yet, but you can download the sources and build it yourself)
Guava (to the best of my knowledge) doesn't provide any functionality to simplify the code that you've posted.
The only improvements that I can suggest are to use generics, and to use the enhanced for loop to iterate over the list rather than obtaining and using the iterator explicitly:
import java.util.*;
public class list {
public static void main(String[] args) {
List<Integer> l = new ArrayList<Integer>();
for (int i = 1; i < 6; i++) {
l.add(i);
}
for(Integer i : l) {
System.out.println(i);
}
}
As @Jared Russell pointed out, this is not exactly the best specimen to show how the improvements Guava can help you make in your code.
That said, if you're just looking for a demonstration of how Guava can find its way into this code, here's an shot:
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableList.Builder;
public class Question6842887 {
public static void main(String[] args) {
Builder<Integer> builder = ImmutableList.builder();
for (int i = 1; i < 6; i++) {
builder.add(i);
}
ImmutableList<Integer> list = builder.build();
System.out.println(Joiner.on(System.getProperty("line.separator")).join(list));
}
The two Guava things I'm using here are immutable lists and joiner.
This is a probably not the type of situation you'd normally need an ImmutableList, but it's good practice to get used to using it regardless. The great advantage of immutable collections (and really any immutable object) is that they are thread-safe. You never need to make defensive copies or otherwise worry about the contents changing after passing it off. You can simply pass it around freely, or cache it, and be happy and secure about its immutability. For more information on the subject of immutability, I'd refer you to Effective Java, item 15.
The other Guava class I'm demonstrating here is Joiner. Joiner is just a nice and easy way to shove together a collection of strings with a separator. It is fairly customizable in that you can specify how it will deal with nulls as well as what separator you'd like to use. In this case, I've specified System.getProperty("line.separator")
as my separator; you could instead choose to use ", "
or similar. I've also chosen not to deal with nulls, since I know there are no null values in our ImmutableList. You could, however, choose an option like skipNulls
or useForNull
if your list could have null values.
The next release of Guava is expected to include Range support. This could be used to simplify your list building to something like ContiguousSet<Integer> list = Ranges.closedOpen(1, 6).asSet(DiscreteDomains.integers());
Though I'd not recommend it, your entire program could be one line.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With