I get a list of city from a database in a table.
My table:
Kolkata
Kolkata
Kolkata
Mumbai
Mumbai
Bengaluru
...
I would like from this table to create another table like this
Kolkata 3
Mumbai 2
Bengaluru 1
How do I do that?
I get a list of city from a database in a table.
My table:
Kolkata
Kolkata
Kolkata
Mumbai
Mumbai
Bengaluru
...
I would like from this table to create another table like this
Kolkata 3
Mumbai 2
Bengaluru 1
How do I do that?
Why not do this in your DBMS with a simple query, its more practical and you have a table of two dimensions.
Code:select Distinct (name) ,count (name) from test group by name;
Yes to consolidate the cities I tried LinkedHashMap putting in key cities, it works, but I can not count with this method the number of Occurrence of each city.
How can I do?
I did not direct my table with my city is already extracted.Code:select Distinct (name) ,count (name) from test group by name;
The interface Bag of commons-collections, for example implemented by HashBag kit, is precisely that.
If you can not use any external library like commons-collections, you have more than one redo HashBag kit. Basically it's like HashMap, except that the value associated with key is the number of times this key has been added to the Map.Syntax
public class HashBag
extends AbstractMapBag
implements Bag, java.io.Serializable
Description
Implements Bag, using a HashMap to provide the data storage. This is the standard implementation of a bag.
A Bag stores each object in the collection together with a count of occurrences. Extra methods on the interface allow multiple copies of an object to be added or removed at once. It is important to read the interface javadoc carefully as several methods violate the Collection interface specification.
Bookmarks