在pom中引入
com.google.guava
guava
19.0
调用
public static void main(String[] args) throws IOException {
Map <String, Integer> items = ImmutableMap.of(“coin”, 3, “glass”, 4, “pencil”, 1);
items.entrySet()
.stream()
.forEach(System.out::println);
List<String> fruits = Lists.newArrayList("orange", "banana", "kiwi",
"mandarin", "date", "quince");
for (String fruit: fruits) {
System.out.println(fruit);
}
car中方法
@Override
public String toString() {
return MoreObjects.toStringHelper(Car.class)
.add(“id”, Id)
.add(“name”, Name)
.add(“price”, Price)
.toString();
}
Car car1 = new Car(1, “Audi”, 52642);
Car car2 = new Car(2, “Mercedes”, 57127);
Car car3 = new Car(3, “Skoda”, 9000);
System.out.println(car1);
System.out.println(car2);
System.out.println(car3);
List cars = Lists.newArrayList(new Car(1, “Audi”, 52642),
new Car(2, “Mercedes”, 57127), new Car(3, “Skoda”, 9000),
new Car(4, “Volvo”, 29000));
Predicate byPrice = car -> car.getPrice() <= 30000;
List results = FluentIterable.from(cars)
.filter(byPrice)
.transform(Functions.toStringFunction())
.toList();
System.out.println(results);
List<Integer> values = Lists.newArrayList(3, null, 4, 7,
8, null, 7);
Iterable<Integer> filtered = Iterables.filter(values,
Predicates.notNull());
for (Integer i: filtered) {
System.out.println(i);
}
List<String> item= Lists.newArrayList("coin", "book",
"cup", "purse", "bottle");
Collection<String> result = Collections2.filter(item,
Predicates.containsPattern("o"));
for (String it: result) {
System.out.println(it);
}
String fileName = "src/main/resources/application.yml";
List<String> lines = Files.readLines(new File(fileName),
Charsets.UTF_8);
for (String line: lines) {
System.out.println(line);
}
String fileNames = "src/main/resources/fruits.txt";
File file = new File(fileNames);
String content = "banana, orange, lemon, apple, plum";
Files.write(content.getBytes(), file);
List<String> myList = Lists.newArrayList("8", "2", "7", "10");
String resul = Joiner.on(",").join(myList);
System.out.println(resul);
String input = "There is a dog in the garden.";
List<String> words = Splitter.on(" ").splitToList(input);
for (String word: words) {
System.out.println(word);
}
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter items: ");
String inputs = br.readLine();
List<String> itemss = Splitter.on(" ").splitToList(inputs);
OutputItems(itemss);
}
public static void OutputItems(List<String> items) {
checkArgument(items != null, "The list must not be null");
checkArgument(!items.isEmpty(), "The list must not be empty");
for (String item: items) {
System.out.println(item);
}
}