mantiqiy olib tashlash (ob'ekt ob'ekti) (Buzilish) Berilgan ob'ektning barcha holatlarini bagdan olib tashlaydi.
7
mantiqiy olib tashlash (ob'ekt ob'ekti, int nCopies) Belgilangan ob'ektning nCopies nusxalarini bagdan olib tashlaydi.
8
mantiqiy olib tashlashAll(Collection> coll) (Buzilish) Ushbu to'plamda ifodalangan barcha elementlarni tubdan hisobga olgan holda olib tashlang.
9
mantiqiy retainAll(To'plam> coll) (Buzilish) Kardinallikni hurmat qilgan holda, ushbu to'plamda bo'lmagan bagning barcha a'zolarini olib tashlang.
10
int size() Barcha turlar bo'yicha bagkadagi narsalarning umumiy sonini qaytaradi.
Set uniqueSet() Bagdagi noyob elementlar to'plamini qaytaradi.
Bag interfeysiga misol
import org.apache.commons.collections4.Bag;
import org.apache.commons.collections4.bag.HashBag;
public class BagTester {
public static void main(String[] args) {
Bag bag = new HashBag<>();
//add "a" two times to the bag.
bag.add("a" , 2);
//add "b" one time to the bag.
bag.add("b");
//add "c" one time to the bag.
bag.add("c");
//add "d" three times to the bag.
bag.add("d",3
//get the count of "d" present in bag.
System.out.println("d is present " + bag.getCount("d") + " times.");
System.out.println("bag: " +bag);
//get the set of unique values from the bag
System.out.println("Unique Set: " +bag.uniqueSet());
//remove 2 occurrences of "d" from the bag
bag.remove("d",2);
System.out.println("2 occurences of d removed from bag: " +bag);
System.out.println("d is present " + bag.getCount("d") + " times.");
System.out.println("bag: " +bag);
System.out.println("Unique Set: " +bag.uniqueSet());
}}
Natija:
d is present 3 times.
bag: [2:a,1:b,1:c,3:d]
Unique Set: [a, b, c, d]
2 occurences of d removed from bag: [2:a,1:b,1:c,1:d]
d is present 1 times.
bag: [2:a,1:b,1:c,1:d]
Unique Set: [a, b, c, d]