guava在java项目中的应用

在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);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值