Guava(二)集合类

集合类

1.1 Multiset

允许重复,但是不保证顺序。

Multiset<String> multiset_1 = LinkedHashMultiset.create();
Multiset<String> multiset_2 = TreeMultiset.create();
Multisets multisets;
Multiset<String> multiset = HashMultiset.create();
常用方法
//向其中添加单个元素
int add(E element)  
//向其中添加指定个数的元素 返回0 element是新增加的对象,返回1的时候,说明原来的element已经存在
int add(E element,int occurrences) 

//返回给定参数元素的个数
int count(Object element) 
//判断集合中是否包含指定元素
boolean contains(Object element)
//判断当前集合是不是指定集合的子集
boolean containsAll(Collection<?> elements)

//移除一个元素,其count值 会相应减少
boolean remove(E element) 
//如果element存在,返回数目,如果不存在返回0
int remove(Object element, int occurrences) 
//保留出现在给定集合参数的所有的元素
boolean retainAll(Collection c) 
//去除出现给给定集合参数的所有的元素
boolean removeAll(Collection c) 

//将不同的元素放入一个Set中。保有set的不重性质,只输出元素,不计个数
Set<E> elementSet() 
//返回所有的数据,例如有 qw x 3 那么qw就会输出三次
Iterator<E> iterator() 
//类似与Map.entrySet 返回Set<Multiset.Entry>。包含的Entry支持使用getElement()和getCount()
Set<Multiset.Entry<E>> entrySet()

//设定某一个元素的重复次数.如果这个元素之前不存在,返回0,如果存在,返回之前出现的次数
int setCount(E element ,int count)
//将符合原有重复个数的元素修改为新的重复次数
boolean setCount(E element,int oldCount,int newCount)
代码示例
public class MultisetTest {

  public static void main(String[] args) {

    Set<String> set = new HashSet<>();

    Multiset<String> multiset = HashMultiset.create();
    MultisetTest test = new MultisetTest();
    test.addSetElement(multiset);
    test.countAndJudge(multiset);
    test.removeAndRetain(multiset);
    test.goThroughSet(multiset);
    test.setCountTest(multiset);
  }

  /**
   * 集合中增加元素
   */
  private void addSetElement(Multiset<String> multiset) {
    multiset.add("raw");
    multiset.add("raw");
    multiset.add("raw");
    multiset.add("qw");

    //加入null值
    multiset.add(null);

    //指定添加元素的个数,这个个数最大为Integer.MAX_VALUE
//    System.out.println(multiset.add("count", Integer.MAX_VALUE));
//    System.out.println(multiset.add("count", Integer.MAX_VALUE+1));
    System.out.println(multiset.add("qw", 3));
    System.out.println(multiset.add("e", 0));
    System.out.println("向集合中添加元素:" + multiset);
  }

  /**
   * multiset集合统计与判断
   */
  private void countAndJudge(Multiset<String> multiset) {
    System.out.println("计算集合中指定元素的个数:" + multiset.count("raw"));
    System.out.println("集合中是否包含指定的元素:" + multiset.contains("raw"));
    Multiset<String> set = HashMultiset.create();
    set.add("raw");
    List<String> list = new ArrayList<>(Arrays.asList("raw", "qw"));
    System.out.println("集合中是否包含指定的集合:" + multiset.containsAll(set));
    System.out.println("集合中是否包含指定的集合:" + multiset.containsAll(list));
  }

  /**
   * remove / retain
   */
  private void removeAndRetain(Multiset<String> multiset) {
    System.out.println("remove test:" + multiset.remove("raw"));
    System.out.println("after remove :" + multiset);

    System.out.println("remove specify num:" + multiset.remove("qpoi", 1));
    System.out.println("after remove specify num :" + multiset);

    Multiset<String> set = HashMultiset.create();
    set.add("raw");
    set.add("qqqq");
    set.add("qw");
    System.out.println("retain :" + multiset.retainAll(set));
    System.out.println("after retain element:" + multiset);

    System.out.println("remove all in set:" + multiset.removeAll(set));
    System.out.println("after remove all element in set:" + multiset);

    addSetElement(multiset);
  }

  /**
   * 集合遍历
   */
  private void goThroughSet(Multiset<String> multiset) {
    Set<String> set = multiset.elementSet();

    System.out.print("use elementSet go through set:");
    for (String item : set) {
      System.out.print(item + " ");
    }

    Iterator<String> iter = multiset.iterator();
    System.out.println("iterator go through set :");
    while (iter.hasNext()) {
      System.out.print(iter.next() + " ");
    }
    System.out.println();
    System.out.println("entrySet go through set:");
    for (Multiset.Entry<String> item : multiset.entrySet()) {
      System.out.println(
          item.getCount() + " " + item.getElement() + " " + (item.getElement() == null ? 0
              : item.getElement().length()));
    }
  }

  /**
   * setCount test
   */
  private void setCountTest(Multiset<String> multiset) {

    System.out.println(multiset.setCount("test", 9));
    System.out.println(multiset);
    System.out.println(multiset.setCount("qw", 6));
    System.out.println(multiset);

    System.out.println(multiset.setCount("qw", 8, 9));
    System.out.println(multiset.setCount("qw", 6, 9));
  }
}
输出结果
1
0
向集合中添加元素:[null, qw x 4, raw x 3]
计算集合中指定元素的个数:3
集合中是否包含指定的元素:true
集合中是否包含指定的集合:true
集合中是否包含指定的集合:true
remove test:true
after remove :[null, qw x 4, raw x 2]
remove specify num:0
after remove specify num :[null, qw x 4, raw x 2]
retain :true
after retain element:[qw x 4, raw x 2]
remove all in set:true
after remove all element in set:[]
1
0
向集合中添加元素:[null, qw x 4, raw x 3]
use elementSet go through set:null qw raw iterator go through set :
null qw qw qw qw raw raw raw 
entrySet go through set:
1 null 0
4 qw 2
3 raw 3
0
[null, test x 9, qw x 4, raw x 3]
4
[null, test x 9, qw x 6, raw x 3]
false
true

1.2 BiMap

继承 java.util.Map

BiMap实现键值对的双向映射,并保持它们间的同步。键和值都不可以有重复

常用方法
//如果key存在,返回之前的value,如果不存在,返回null
V put(K key, V value) 
//如果key存在,返回之前的value,如果不存在,返回null
V forcePut(K key, V value)
//把另一个集合添加到bimap中
void putAll(Map<? extends K,? extends V> map)
//返回一个set
Set<V> values()
//将map的键值反转
BiMap<K, V> inverse()
例子
public class BiMapTest {

  public static void main(String[] args) {
    BiMapTest test = new BiMapTest();
    BiMap<String, Integer> biMap = HashBiMap.create(16);
    test.addElement(biMap);
    test.keyToValue(biMap);
  }

  /**
   * biMap中添加元素 value already present 对于不同的键,不可以有相同的值
   */
  private void addElement(BiMap<String, Integer> biMap) {
    System.out.println(Test + biMap.put("test1", 9));
    System.out.println(Test + biMap.put("test1", 0));
    biMap.put("test2", 3);
    System.out.println(biMap);

    Map<String, Integer> map = new HashMap<>();
    map.put("map", 1);
    map.put("map1", 2);
    map.put("map2", 4);
    biMap.putAll(map);
    System.out.println("put map into biMap: " + biMap);
    System.out.println(biMap.forcePut("test3", 3));
    System.out.println(biMap.forcePut("test3", 3));
    System.out.println("force put into biMap:" + biMap);
  }
  /**
   * search value by key
   */
  private void keyToValue(BiMap<String, Integer> biMap) {
    System.out.println("get value by key: " + biMap.get("map"));
    System.out.println("get key by value: " + biMap.inverse().get(1));
    System.out.println("get values: " + biMap.values());
    System.out.println("get keys: " + biMap.inverse().values());
  }
}
输出结果
test return value: null
test return value: 9
{test1=0, test2=3}
put map into biMap: {test1=0, test2=3, map2=4, map1=2, map=1}
null
3
force put into biMap:{test1=0, map2=4, map1=2, map=1, test3=3}
get value by key: 1
get key by value: map
get values: [0, 4, 2, 1, 3]
get keys: [test1, map2, map1, map, test3]
[1, 2]
first

1.3 Multimap

多重映射接口扩展映射,使得其键一次可被映射到多个值。

常用方法
//Multimap 中加入元素,如果key在之前不存在,返回true,如果存在,返回false。 对于同一个key,如果put不同的value,会将value形成一个Collection
boolean put(K, V)
boolean putAll(K key, Iterable<? extends V> values)
//清除map中的所有内容
void clear()
//判断是否包含 key-value 对
boolean containsEntry(Object key, Object value)
boolean containsKey(Object * key)
boolean containsValue(Object value)
// 返回一个存放在Collection中的map
Collection<Map.Entry<K,V>> entries()
// 根据key返回value
Collection<V> get(K key)
//返回所有的key,不重复
Multiset<K> keys()
Set<K> keySet()
//返回一个map,value放到数组中,返回一个Map<key, Collection<value>>
Map<K,Collection<V>> asMap()
// remove掉一个键值对,如果没有这键值对,返回false,否则返回true
boolean remove(Object key, Object value)
//remove删除指定key所对应的value,返回Collection<value>
Collection<V> removeAll(Object key)
// 把指定key原来的value替换为指定的value。返回原value
Collection<V> replaceValues(K key, Iterable<? extends V> values)
代码示例
public class MultimapTest {

    public static void main(String[] args) {
        Multimap<String, Integer> multiMap = HashMultimap.create();
        Multimap<String, List<Integer>> map = HashMultimap.create();
        MultimapTest test = new MultimapTest();

        test.addTest(multiMap, map);
        System.out.println();
        test.judgeContain(multiMap, map);
        System.out.println();
        test.throughTest(multiMap, map);
        System.out.println();
        test.replceTest(multiMap, map);
        System.out.println();
        test.removeTest(multiMap, map);
        System.out.println();

        test.clearTest(multiMap, map);
    }

    /**
     * put key-value to map
     */
    private void addTest(Multimap<String, Integer> multiMap, Multimap<String, List<Integer>> map) {
        multiMap.put("test", 1);
        System.out.println("is the value put success : " + multiMap.put("test", 2));
        System.out.println("after addTest map : " + multiMap);
        System.out.println("map get key : " + multiMap.get("test"));

        System.out.println("is the value put success : " + multiMap.put("test", 2));
        System.out.println("after addTest map : " + multiMap);
        System.out.println("map get key : " + multiMap.get("test"));

        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(1);
        map.put("Test", list);

        List<Integer> list_2 = new ArrayList<>();
        list_2.add(1);
        list_2.add(1);
        System.out.println("is the value list put success : " + map.put("Test", list_2));
        System.out.println("map put list : " + map);
        list_2.add(2);
        System.out.println("is the value list put success : " + map.put("Test", list_2));
        System.out.println("map put list : " + map);
        System.out.println("is the value list put success : " + map.put("Test1", list_2));
        System.out.println("map put list : " + map);
    }

    /**
     * judge
     */
    private void judgeContain(Multimap<String, Integer> multiMap, Multimap<String, List<Integer>> map) {
        System.out.println("is multiMap contain the key-value pair : " +
                multiMap.containsEntry("test", 1));
        System.out.println("is multiMap contain the specify key : " +
                multiMap.containsKey("test"));
        System.out.println("is multiMap contain the specify value : " +
                multiMap.containsValue(1));

        System.out.println("is map contain the key-value pair : " +
                map.containsEntry("test", Arrays.asList(1, 1)));
        System.out.println("is map contain the key-value pair : " +
                map.containsEntry("Test", Arrays.asList(1, 1)));
        System.out.println("is map contain the specify key : " +
                multiMap.containsKey("test"));
        System.out.println("is map contain the specify value : " +
                multiMap.containsValue(Arrays.asList(1, 1)));

        //Multimap如果value是list,那么list中的每一个数都有一个对应的key
        System.out.println(multiMap);
        System.out.println("is map contain the specify value : " +
                multiMap.containsValue(1));
    }

    /**
     * go through map
     */
    private void throughTest(Multimap<String, Integer> multiMap, Multimap<String, List<Integer>> map) {
        // map.entries()
        Collection<Map.Entry<String, List<Integer>>> collectionMap = map.entries();
        Iterator<Map.Entry<String, List<Integer>>> iter = collectionMap.iterator();
        while (iter.hasNext()) {
            Map.Entry<String, List<Integer>> entry = iter.next();
            System.out.println("key-value " + entry);
            System.out.println("key " + entry.getKey());
            System.out.println("value " + entry.getValue());
        }

        //map.get(K key)
        Collection<List<Integer>> list = map.get("Test");
        System.out.println("getKey : " + list);

        //map.keys()
        Multiset<String> set = map.keys();
        System.out.println("keys: " + set);

        //map.keySet()
        Set<String> keySet = map.keySet();
        System.out.println("keySet: " + keySet);

        //map.asMap()
        Map<String, Collection<Integer>> asmap = multiMap.asMap();
        System.out.println("asMap: " + asmap);

        //map.values()
        Collection<Integer> multi = multiMap.values();
        System.out.println("values: " + multi);
    }

    //replace
    private void replceTest(Multimap<String, Integer> multiMap, Multimap<String, List<Integer>> map) {
        System.out.println("replace test: " + multiMap.replaceValues("test", ImmutableList.of(1, 23, 4)));
        System.out.println(multiMap);

        System.out.println("replace test: " + multiMap.replaceValues("error_test", ImmutableList.of(1, 23, 4)));
        System.out.println(multiMap);
    }

    // remove
    private void removeTest(Multimap<String, Integer> multiMap, Multimap<String, List<Integer>> map) {
        System.out.println("multimap remove false: " + multiMap.remove("test", ImmutableList.of(1, 2)));
        System.out.println("multimap remove true: " + multiMap.remove("test", 1));
        System.out.println("multimap after remove : " + multiMap);

        System.out.println("map remove false: " + map.remove("Test", ImmutableList.of(1, 1)));
        System.out.println("map remove true: " + map.remove("Test", 1));
        System.out.println("map after remove : " + map);

        // map.removeAll()
        System.out.println("removeAll test: " + multiMap.removeAll("test"));
        System.out.println("removeAll test: " + multiMap.removeAll("error_test"));
    }


    /**
     * clear map
     */
    private void clearTest(Multimap<String, Integer> multiMap, Multimap<String, List<Integer>> map) {
        multiMap.clear();
        map.clear();
        System.out.println("multiMap after clear : " + multiMap);
        System.out.println("map after clear : " + map);
    }

}
输出结果
is the value put success : true
after addTest map : {test=[1, 2]}
map get key : [1, 2]
is the value put success : false
after addTest map : {test=[1, 2]}
map get key : [1, 2]
is the value list put success : false
map put list : {Test=[[1, 1]]}
is the value list put success : true
map put list : {Test=[[1, 1], [1, 1, 2]]}
is the value list put success : true
map put list : {Test1=[[1, 1, 2]], Test=[[1, 1], [1, 1, 2]]}

is multiMap contain the key-value pair : true
is multiMap contain the specify key : true
is multiMap contain the specify value : true
is map contain the key-value pair : false
is map contain the key-value pair : true
is map contain the specify key : true
is map contain the specify value : false
{test=[1, 2]}
is map contain the specify value : true

key-value Test1=[1, 1, 2]
key Test1
value [1, 1, 2]
key-value Test=[1, 1]
key Test
value [1, 1]
key-value Test=[1, 1, 2]
key Test
value [1, 1, 2]
getKey : [[1, 1], [1, 1, 2]]
keys: [Test1, Test x 2]
keySet: [Test1, Test]
asMap: {test=[1, 2]}
values: [1, 2]

replace test: [1, 2]
{test=[4, 1, 23]}
replace test: []
{test=[4, 1, 23], error_test=[4, 1, 23]}

multimap remove false: false
multimap remove true: true
multimap after remove : {test=[4, 23], error_test=[4, 1, 23]}
map remove false: true
map remove true: false
map after remove : {Test1=[[1, 1, 2]], Test=[[1, 1, 2]]}
removeAll test: [4, 23]
removeAll test: [4, 1, 23]

multiMap after clear : {}
map after clear : {}

1.4 Table

有两个键一个值的数据结构。

常用API
//获取指定列的值,返回行和值形成的键值对
Map<R,V> column(C columnKey)
// 获取指定行的值,返回列和值形成的键值对
Map<C,V> row(R rowKey)
// 获取列元素
Set<C> columnKeySet()
// 获取行元素
Set<R> rowKeySet()
// 返回以row为键,列和值为值得键值对
Map<R,Map<C,V>> rowMap()
Map<C,Map<R,V>> columnMap()
// 返回所有值
Collection<V> values()
//根据行和列确定值
V get(Object rowKey, Object columnKey)
//根据行和列移除元素,如果存在返回值,不存在返回null
V remove(Object rowKey, Object columnKey)
//判断元素是否存在
boolean contains(Object rowKey, Object columnKey)
boolean containsColumn(Object columnKey)
boolean containsRow(Object rowKey)
boolean containsValue(Object value)
代码示例
public class TableTest {

    public static void main(String[] args) {
        TableTest test = new TableTest();
        Table<String, String, Integer> table = HashBasedTable.create();

        test.putTest(table);
        System.out.println();
        test.throughTableTest(table);
        System.out.println();
        test.containTest(table);
    }

    //添加元素
    private void putTest(Table<String, String, Integer> table) {
        table.put("语文", "张三", 110);
        table.put("语文", "李四", 120);
        table.put("语文", "王五", 130);

        table.put("数学", "张三", 110);
        table.put("数学", "李四", 120);
        table.put("数学", "王五", 130);
        System.out.println("table :" + table);
    }

    // 遍历
    private void throughTableTest(Table<String, String, Integer> table) {
        System.out.println(table);
        Map<String, Integer> columnMap = table.column("张三");
        System.out.println("column values: " + columnMap);

        Map<String, Integer> rowMap = table.row("语文");
        System.out.println("row values: " + rowMap);

        Set<String> columnSet = table.columnKeySet();
        System.out.println("column key set: " + columnSet);

        Set<String> rowSet = table.rowKeySet();
        System.out.println("row key set: " + rowSet);

        Map<String, Map<String, Integer>> rowMapMap = table.rowMap();
        System.out.println("rowMap test: " + rowMapMap);

        Map<String, Map<String, Integer>> columnMapMap = table.columnMap();
        System.out.println("columnMap test: " + columnMapMap);

        Collection<Integer> collectionTest = table.values();
        System.out.println("collection : " + collectionTest);

        Integer value = table.get("语文", "张三");
        System.out.println("get value by row and column :" + value);

        System.out.println("根据行和列移除元素1: " + table.remove("张三", "语文"));
        System.out.println("根据行和列移除元素2: " + table.remove("张三", "张三"));
        System.out.println("根据行和列移除元素3: " + table.remove("语文", "张三"));
    }

    private void containTest(Table<String, String, Integer> table) {
        System.out.println("contains key and value1 : " + table.contains("张三", "语文"));
        System.out.println("contains key and value2 : " + table.contains("语文", "李四"));
        System.out.println("contain row: " + table.containsRow("语文"));
        System.out.println("contain column: " + table.containsColumn("张三"));
        System.out.println("contain value: " + table.containsValue(110));
    }
}
结果输出
table :{语文={张三=110, 李四=120, 王五=130}, 数学={张三=110, 李四=120, 王五=130}}

{语文={张三=110, 李四=120, 王五=130}, 数学={张三=110, 李四=120, 王五=130}}
column values: {语文=110, 数学=110}
row values: {张三=110, 李四=120, 王五=130}
column key set: [张三, 李四, 王五]
row key set: [语文, 数学]
rowMap test: {语文={张三=110, 李四=120, 王五=130}, 数学={张三=110, 李四=120, 王五=130}}
columnMap test: {张三={语文=110, 数学=110}, 李四={语文=120, 数学=120}, 王五={语文=130, 数学=130}}
collection : [110, 120, 130, 110, 120, 130]
get value by row and column :110
根据行和列移除元素1: null
根据行和列移除元素2: null
根据行和列移除元素3: 110

contains key and value1 : false
contains key and value2 : true
contain row: true
contain column: true
contain value: true
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值