Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate in Guava

Tags:

java

guava

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());
    }
  }
}
like image 837
Enosh Bansode Avatar asked Jul 27 '11 10:07

Enosh Bansode


People also ask

Can we iterate list?

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.

Can we use for loop in iterator?

A for-each loop uses an iterator under the covers. The difference is just readability (and therefore maintainability).


3 Answers

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)

like image 79
Sean Patrick Floyd Avatar answered Oct 14 '22 09:10

Sean Patrick Floyd


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);
  }

}
like image 22
Jared Russell Avatar answered Oct 14 '22 09:10

Jared Russell


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.

like image 25
Ray Avatar answered Oct 14 '22 09:10

Ray