Categories:
Audio (13)
Biotech (29)
Bytecode (36)
Database (77)
Framework (7)
Game (7)
General (507)
Graphics (53)
I/O (35)
IDE (2)
JAR Tools (101)
JavaBeans (21)
JDBC (121)
JDK (426)
JSP (20)
Logging (108)
Mail (58)
Messaging (8)
Network (84)
PDF (97)
Report (7)
Scripting (84)
Security (32)
Server (121)
Servlet (26)
SOAP (24)
Testing (54)
Web (15)
XML (309)
Collections:
Other Resources:
org.apache.commons.collections4.bag.HashBag Example
What is org.apache.commons.collections4.bag.HashBag class? How to use org.apache.commons.collections4.bag.HashBag class?
✍: FYIcenter.com
org.apache.commons.collections4.bag.HashBag class is a Java class
offered in commons-collections4.jar that
implements the org.apache.commons.collections4.Bag interface
using a HashMap to provide the data storage.
A bag is a special collection which allows the same object to be added multiple times. You can also query the number of copies of a given object in the bag. and reduce the number of copies of a given object in the bag.
Here is a simple example of using org.apache.commons.collections4.bag.HashBag class:
// Copyright (c) 2016-2018 FYIcenter.com // Supports commons-collections4-4.2 // Supports commons-collections4-4.1 import org.apache.commons.collections4.Bag; import org.apache.commons.collections4.bag.HashBag; // Example of using the HashBagExample class public class HashBagExample { public static void main(String[] args) throws Exception { // Create some objects String apple = "Apple"; String orange = "Orange"; String banana = "Banana"; // Create a HashBag bag Bag<String> bag = new HashBag<String>(); // Put some objects into the HashBag bag bag.add(apple); bag.add(apple); bag.add(apple); bag.add(orange); bag.add(orange); bag.add(banana); // Count apples, oranges and bananas System.out.println("# of apple: "+bag.getCount(apple)); System.out.println("# of orange: "+bag.getCount(orange)); System.out.println("# of banana: "+bag.getCount(banana)); // loop through the bag for (String obj : bag) { System.out.println("Element: " + obj); } } }
You can compile and run the above example in a command window as shown below:
C:\fyicenter>\fyicenter\jdk-1.8.0\bin\javac -cp C:\fyicenter\commons-collections4-4.2\commons-collections4-4.2.jar HashBagExample.java C:\fyicenter>\fyicenter\jdk-1.8.0\bin\java -cp .;C:\fyicenter\commons-collections4-4.2\commons-collections4-4.2.jar HashBagExample # of apple: 3 # of orange: 2 # of banana: 1 Element: Apple Element: Apple Element: Apple Element: Banana Element: Orange Element: Orange
⇒ org.apache.commons.collections4.set.ListOrderedSet Example
⇐ org.apache.commons.collections4.queue.CircularFifoQueue Example
2017-05-20, 2250🔥, 0💬
Popular Posts:
Apache Log4j 1.2 Bridge allows applications coded to use Log4j 1.2 API to use Log4j 2 instead. Bytec...
Jackson is "the Java JSON library" or "the best JSON parser for Java". Or simply as "JSON for Java"....
What Is javamail1_1_3.zip? javamail1_1_3.zip is the binary package of JavaMail API 1.1.3 in ZIP form...
What Is javaws.jar in JRE (Java Runtime Environment) 8? javaws.jar in JRE (Java Runtime Environment)...
itextpdf.jar is a component in iText 5 Java library to provide core functionalities. iText Java libr...