guava Working with Collections

guava Map Table Range

import com.google.common.collect.*;

import java.util.Map;

public class MyMultimaps {

    public static void main(String[] args) {

        testArrayListMultimap();
        testHashMultimap();
        testBiMap();
        testTable();
        testRange();
    }

    private static void testArrayListMultimap() {
        ArrayListMultimap arrayListMultimap = ArrayListMultimap.create();
        arrayListMultimap.put("test","1");
        arrayListMultimap.put("test","2");
        arrayListMultimap.put("test","3");
        arrayListMultimap.put("test","3");
        arrayListMultimap.put("test","3");

        System.out.println(arrayListMultimap.get("test"));
    }

    private static void testHashMultimap() {
        HashMultimap hashMultimap = HashMultimap.create();
        hashMultimap.put("test","1");
        hashMultimap.put("test","2");
        hashMultimap.put("test","3");
        hashMultimap.put("test","3");
        hashMultimap.put("test","3");

        System.out.println(hashMultimap.get("test"));
    }

    private static void testBiMap(){
        BiMap biMap = HashBiMap.create();
        biMap.put("test1","1");
        biMap.put("test2","2");

        System.out.println(biMap.get("test1"));
        System.out.println(biMap.get("test2"));
        BiMap inverseBiMap = biMap.inverse();
        System.out.println(inverseBiMap.get("1"));
        System.out.println(inverseBiMap.get("2"));

    }

    private static void testTable(){
        HashBasedTable hashBasedTable = HashBasedTable.create();
        hashBasedTable.put(1,2,"test1");
        hashBasedTable.put(2,3,"test2");
        hashBasedTable.put(3,4,"test3");

        Map row = hashBasedTable.row(1);
        System.out.println(row);

        Map column = hashBasedTable.column(3);
        System.out.println(column);

        boolean contains1 = hashBasedTable.contains(1, 2);
        System.out.println(contains1);

        System.out.println(hashBasedTable.rowMap());

    }
    private static void testRange() {

        Range range = Range.closed(1, 10);
        System.out.println(range);

        Range range1 = Range.open(1,10);
        System.out.println(range1);

    }
}

运行结果

[1, 2, 3, 3, 3]
[1, 2, 3]
1
2
test1
test2
{2=test1}
{2=test2}
true
{1={2=test1}, 2={3=test2}, 3={4=test3}}
[1..10]
(1..10)

你可能感兴趣的:(guava)