I Know its been asked a hundred times, and the answer is always the same, You Can not use multiple repeating values in a hashmap.
But lets get to the problem. I have an import file, the import file has information around the lines of a CustomerID, a ProductID, and Units sold (its a basic Receipt format).
What I want to do is take the import, put it into a map, and be able to reference it.
Map<integer,DoubleSales> hashmap = new HashMap <integer,DoubleSales>
try {
Scanner dataFile = new Scanner 9new File ("./salesData.csv"));
dataFile.nextLine();
while(dataFile.hasNextLine()){
String[] lineValues = line.split (",");
Integer CustomerID = Integer.parseInt(lineValues[0]);
Integer ProductID = Integer.parseInt(lineValues[1]);
integer Units = Integer.parseInt(lineValues[2]);
DoubleSales sales = new DoubleSales(CustomerID,ProductID,Units);
ProductData.put(CustomerID,sales);
}
class DoubleSales{
int CustomerID;
int ProductID;
int Units;
DoubleSales(int custID, int prodID, int Units){
CustomerID = custID;
ProductID = prodID;
Units = units;
}
}
The import file has data in the format of
CustomerID, ProductID, UnitsSold
1,10002,3
1,10004,5
1,10008,2
1,10010,3
1,10010,3
Using the code up there, When I print the customerID value of 1, I get just the last entry which is 10010,3.
How would I do it to print out, all values of CustomerID 1, and the Units sold?
for example:
1,10002,3
10004,5
10008,2
10010,3
10010,3
(will add the two 10010 values later.)
I do not wish to Use array lists.
Try MultiValueMap from Apache Common Collections.
Click here for more reference
In your case, a simple Map won't do your favor, everything you write to the value of a specified customer will be overridden, if you want to retain all entries while keeping them easily referenced, try:
First, create a structured map
Map<Integer,List<DoubleSales>> productData = new HashMap<Integer,List<DoubleSales>>();
Second, add products like this
List<DoubleSales> entries;
if(productData.get(CustomerID) == null) {
entries = new ArrayList<DoubleSales>();
entries.add(sales);
productData.put(CustomerID, entries);
} else {
List<DoubleSales> entries = productData.get(CustomerID);
entries.add(sales);
}
Third, review your products list that you just added
List<DoubleSales> products = productData.get(CustomerID);
if (products != null) {
for(DoubleSales product : products) {
// access your product here.
}
}
You have duplicated CustomerID (all having 1 as id) and you using that as a key in Hashmap. That is the reason it is keep ovverding when you insert a new record with the same id. Looks like your product id is unique. Try that or have unique customer id.
I think in that case it is better to implement the structure with a matrix. It could be done easily with arrays (or lists), where rows could contain a bean formed by the product id and the units sold, being indexed by the customer id
My first idea was Jerry Chin's solution, but I would like to show you a second approach, just to demonstrate that there are multiple solutions to the same problem.
You could store your values in a TreeSet<DoubleSales>. This would not limit the entries, you can enter for example 1,10010,3 as many times as you want.
Then, define an ordering (Comparator) on the DoubleSales, to group the orders by CustomerID.
When you print your list, you can check if the customerID of the current record is different from the prevoius record. If different, then it is the first record of the new customer. If not, it belongs to the same customer.
And the code:
SortedSet<DoubleSales> set = new TreeSet<DoubleSales>(new Comparator<DoubleSales>() {
#Override
public int compare(DoubleSales o1, DoubleSales o2) {
return Long.compare(o1.customerId, o2.customerId);
}
});
// ... import data
set.add(new DoubleSales( /*...*/ ));
// iterate through data
DoubleSales prevDS = null;
for (DoubleSales ds : set) {
if (prevDS == null || ds.customerId != prevDS.customerId) {
// first record of a customer
// print CustomerID, ProductID, UnitsSold
} else {
// second or next record of a customer
// print ProductID, UnitsSold only
}
prevDS = ds;
}
Related
Sorry if the title isn't clear, I wasn't sure how to word it. I have an arraylist of objects and within each of these objects I store an integer value referring to a category and one referring to an ID.
I want to find the number of unique combinations of category and IDs that there are.
So at the moment I have
for(Object object: listofObjects){
//For each unique type of object.getID
//For each unique type of object.getCategory
//Add 1 to counter
}
I can't figure out how to do this. Doing things like for(int cat: object.getCategory()) brings up an error.
I can add the values to a new list within the initial for each loop like so,
ArrayList<Integer> aList= new ArrayList<Integer>();
for (Object object : spriteExplore) {
aList.add(object.getCategory());
}
for (int cat : aList) {
testCounter++;
}
but this obviosuly does not take into account uniqueness and also makes it awkward for factoring in the other variable of ID.
I feel like there is probably some easier work around that I am missing. Any advice?
Thanks in advance.
So you list of UserDefine object in ArrayList and you want to find unique Object.Just create set from list.
For e.g Suppose you have
List<Customer> list=new ArrayList<Custeomer>();
list.add(new Customer("A",12));
list.add(new Customer("B",13));
list.add(new Customer("A",12));
now
create set From this list
Set<Customer> set = new HashSet<Customer>(list);
this will have unique Customer
IMP : dont forget to override equals and hashcode method for Customer
Your best approach would be storing the data correctly.
It's possible that you still need to store non-unique items, if that's so - continue using an ArrayList, but in addition, use the following:
Override the hashcode & equels function as shown in this link:
What issues should be considered when overriding equals and hashCode in Java?
Then, use a Set (HashSet would probably be enough for you) to store all your objects. This data structure will disregard elements which are not unique to elements already inside the set.
Then, all you need to do is query the size of the set, and that gives you the amount of unique elements in the list.
I don't know any library that does this automatically, but you can do it manually using sets. Sets will retain only unique object so if you try to add the same value twice it will only keep one reference.
Set<Integer> categories = new HashSet<Integer>();
Set<Integer> ids= new HashSet<Integer>();
for (Object object : listofObjects) {
categories.add(object.getCategory());
ids.add(object.getID());
}
Then you get the number of unique categories / ids by doing
categories.size()
ids.size()
And all your unique values are stored in the sets if you want to use them.
I would look into using a (Hash)Map<Integer, Integer>. Then just have 1 foreach loop, checking to see if the value of Map<object.getId(), object.getCategory()> is null by checking if map.get(object.getId()) is null - if it is, then this pair does not exist yet, so add this pair into the map by using map.put(object.getId(), object.getCategory()). If not, do nothing. Then at the end, to find the number of unique pairs you can just use map.size()
Hope this helps
Map<Integer,List<Integer>> uniqueCombinations = new HashMap<Integer,List<Integer>>();
for (Object object : listofObjects) {
if(uniqueCombinations.get(object.getCategoryId())==null) {
uniqueCombinations.put(object.getCategoryId(), new LinkedList<Integer>);
}
uniqueCombinations.get(object.getCategoryId()).add(object.getId());
}
return uniqueCombinations.size()
I believe you want unique combinations of both category and id, right?
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class SO {
class MyObject{
private int id;
private int category;
private String name;
private MyObject(int id, int category,String name) {
super();
this.id = id;
this.category = category;
this.name = name;
}
protected int getId() {
return id;
}
protected int getCategory() {
return category;
}
#Override
public String toString() {
return "MyObject [id=" + id + ", category=" + category + ", name=" + name + "]";
}
}
public static void main(String[] args) {
SO so = new SO();
List<Object> listofObjects = new ArrayList<Object>();
listofObjects.add(so.new MyObject(1,1,"One"));
listofObjects.add(so.new MyObject(1,1,"Two"));
listofObjects.add(so.new MyObject(1,2,"Three"));
Map<String,List<MyObject>> combinations = new HashMap<String,List<MyObject>>();
for(Object object: listofObjects ){
//For each unique type of object.getID
//For each unique type of object.getCategory
//Add 1 to counter
if (object instanceof MyObject){
MyObject obj = (MyObject)object;
String unique = obj.id+"-"+obj.category;
if (combinations.get(unique) == null){
combinations.put(unique, new ArrayList<MyObject>());
}
combinations.get(unique).add(obj);
}
}
System.out.println(combinations);
//counts
for(Entry<String,List<MyObject>> entry:combinations.entrySet()){
System.out.println(entry.getKey()+"="+entry.getValue().size());
}
}
}
Use the Hashmap to save occurence. Dont forget to implement hashcode und equals Methods. You can generate them if you work with Eclipse IDE.
public static void main(String[] args) {
List<MyObject> myObjects = Arrays.asList(new MyObject(1, 2), new MyObject(2, 3), new MyObject(3, 4), new MyObject(3, 4));
Map<MyObject, Integer> map = new HashMap<>();
for (MyObject myObject : myObjects) {
Integer counter = map.get(myObject);
if(counter == null){
counter = 1;
} else {
counter = counter + 1;
}
map.put(myObject, counter);
}
long uniqueness = 0;
for(Integer i : map.values()){
if(i == 1){
++uniqueness;
}
}
System.out.println(uniqueness);
}
The last part can be replaced by this one line expression if you are working with Java 8:
long uniqueness = map.values().stream().filter(i -> i == 1).count();
Suppose I want to store phone numbers of persons. Which kind of collection should I use for key value pairs? And it should be helpful for searching. The name may get repeated, so there may be the same name having different phone numbers.
In case you want to use key value pair. Good choice is to use Map instead of collection.
So what should that map store ?
As far it goes for key. First thing you want to assure is that your key is unique to avoid collisions.
class Person {
long uniqueID;
String name;
String lastname;
}
So we will use the uniqueID of Person for key.
What about value ?
In this case is harder. As the single Person can have many phone numbers. But for simple task lest assume that a person can have only one phone number. Then what you look is
class PhoneNumberRegistry {
Map<Long,String> phoneRegistry = new HashMap<>();
}
Where the long is taken from person. When you deal with Maps, you should implement the hashCode and equals methods.
Then your registry could look like
class PhoneNumberRegistry {
Map<Person,String> phoneRegistry = new HashMap<>();
}
In case when you want to store more then one number for person, you will need to change the type of value in the map.
You can use Set<String> to store multiple numbers that will not duplicate. But to have full control you should introduce new type that not only store the number but also what king of that number is.
class PhoneNumberRegistry {
Map<Person,HashSet<String>> phoneRegistry = new HashMap<>();
}
But then you will have to solve various problems like, what phone number should i return ?
Your problem has different solutions. For example, I'll go with a LIST: List<Person>, where Person is a class like this:
public class Person{
private String name;
private List<String> phoneNumbers;
// ...
}
For collections searching/filtering I suggest Guava Collections2.filter method.
You should use this:
Hashtable<String, ArrayList<String>> addressbook = new Hashtable<>();
ArrayList<String> persons = new ArrayList<String>()
persons.add("Tom Butterfly");
persons.add("Maria Wanderlust");
addressbook.put("+0490301234567", persons);
addressbook.put("+0490301234560", persons);
Hashtable are save to not have empty elements, the ArrayList is fast in collect small elements. Know that multiple persons with different names may have same numbers.
Know that 2 persons can have the same number and the same Name!
String name = "Tom Butterfly";
String[] array = addressbook.keySet().toArray(new String[] {});
int firstElement = Collections.binarySearch(Arrays.asList(array),
name, new Comparator<String>() {
#Override
public int compare(String top, String bottom) {
if (addressbook.get(top).contains(bottom)) {
return 0;
}
return -1;
}
});
System.out.println("Number is " + array[firstElement]);
Maybe
List<Pair<String, String> (for one number per person)
or
List<Pair<String, String[]> (for multiple numbers per person)
will fit your needs.
Initially i will be selecting a list of student names in a page and submit for getting the address details.StudentNames will be stored in the studentDetailMapList.While looping through the list , i will compare the student names with the AddressDetailsMap to retrieve the addressDetails.But when there are students with same names , the first iteration returns the exact address but when the second iteration happens , it again returns the 1st student address instead of the second student address.It is getting the duplicate values
for (i=studentDetailMapList.values().iterator;i.hasNext();)
{
detailMap = (Map)i.Next();
sDetails = (StudentDetails)detailMap.get("Student");
student = sDetails.getRollNo();
StudentAddressDetails studentAddressDetails = getDetailswithAddress(AddressDetailsMap,sDetails);
}
private StudentAddressDetails getDetailswithAddress(Map AddressDetailsMap,sDetails student)
{
StudentAddressDetails addDetails = null;
try{
for(Iterator itr = AddressDetailsMap.values().iterator();itr.hasNext();){
addDetails = (StudentAddressDetails )itr.next();
if( (addDetails != null) && (addDetails.getStudentID().equals(student.getId()))){
return addDetails;
}
}
}catch(Throwable t){
return null;
}
return null;
}
Is there a way to avoid the duplicate while comparing with the map?
Thanks a lot.
The problem you are having is that you are using the the map data structure wrong.
A map is an object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.
You can not have multiple addresses for the same name, you should use other property for the map, maybe a Student ID, even the list's index should work in this case.
You have to add a UUID in your student's class and work with it for managing their data
EDIT for response
Java's UUID give you the basic Java's UUID.
you can manage your own sequence - This way may be helpfull for indexing Students in database with UNIQUE_ID.
The best thing to manage your IDs : use a HashMap<Integer,Student>.
Each key of your AddressDetailsMap map must be an identifier (studentId) and not the student name.
EDIT:
In this case, your method should look like this:
private StudentAddressDetails getDetailswithAddress(Map AddressDetailsMap,sDetails student) {
return AddressDetailsMap.get(student.getStudentID());
}
This is a simple issue I am just having trouble finding the right terms to use for google help. I have some java code that loops some data and I end up having two pieces of information: an int id, and an int quantity.
However, sometimes the ids are the same, and I want to combine the quantities if they are, rather than having new entries in an array.
In PHP, I would do this as such (assume $products is an array with lots of id/quant data, of course):
$newArray = array();
for($products as $id > $quant){
if(array_key_exists($id, $newArray)){
$newArray[ $id ] += $quant;
} else {
$newArray[ $id ] = $quant;
}
}
I'm trying to do this in Java but nothing I find seems to help.
Use HashMap:
1. Get the id
2. See if the id is present in the map
3. if not
insert (id, quantity)
else (i.e. if present)
quantity = hashmap.get(id);
quantity = quantity + new_quantity
hashmap.put(id, quantity);
Helps?
There are many approaches, hashmaps take more memory. You can do this with 2 arrays as well, but then you will spend more time searching through the array.
Use a map implementation, like HashMap.
More or less; adjust types as necessary:
Map<Integer> map = new HashMap<Integer>();
for (Product p : products) {
if (map.hasKey(p.getId()) {
map.put(p.getId(), map.get(p.getId()) + p.getQuant());
} else {
map.put(p.getId(), p.getQuant());
}
}
Slightly cleaner to keep the mainline code readable:
// Mainline code
Map<Integer> map = new HashMap<Integer>();
for (Product p : products) {
putOrAddQuant(map, p);
}
// Extracted helper
public void putOrAddQuant(Map<Integer> map, Product p) {
if (map.hasKey(p.getId())) {
map.put(p.getId(), map.get(p.getId()) + p.getQuant());
} else {
map.put(p.getId(), p.getQuant());
}
}
Consider the following question on storing values with duplicate keys:
Suppose there is a class Employee with name, sal and dob as attributes. I want to store the objects of Employee in a Map and the key would be the Employee name. The name can be duplicate.
Also after adding 10 objects in the Map. I want to retrieve the 8th object that was entered.
This is one solution to add objects with duplicate keys but for the 2nd part of the question, this would not work since on displaying the map, all values with the same key will be displayed together.
How would we maintain the order in which the objects were added in this situation? Can we modify equals and hashcode methods to somehow add the elements and then later retrieve them in the order in which they were inserted?
I think a LinkedHashMultimap (from Guava) should work for this. You wouldn't be able to get the 8th entry by index directly, but you could use something like Iterables.get(Iterable iterable, int position) to get it.
Why not just have two containers? One for mapping name to employee (like the one in the stackoverflow question you mentioned), another for mapping number to employee. You can make an "outer" container aggregating multimap and arraylist.
What you intend to do can be easily implemented using an ArrayList. This is the data structure that you should use.
The requirements are somehow contradictory. At one side several values should be possible for one key, at the other side only one value should be returned for a key. Additionaly, retrievals for a sequence should be possible. I see the nearest approximation in designing a dedicated data structure containing a hash map for fast access based on the name, and a list keeping the order of insertions. The access would be based on the overall sequence number or on the name plus index for the name. The implementation would be according the following lines:
public class Employee {
public String name; public int sal;
public Employee() {name = ""; sal = 0;}
public Employee(String name, int sal) {
this.name = name; this.sal = sal;
}
#Override public String toString() {return "(" + name + "," + sal + ")";}
}
public class Team {
private Map<String, ArrayList<Employee>> employees =
new HashMap<String, ArrayList<Employee>>();
private ArrayList<Employee> order = new ArrayList<Employee>();
public void addEmployee(Employee e) {
ArrayList<Employee> list = employees.get(e.name);
if (list == null) {
list = new ArrayList<Employee>();
employees.put(e.name, list);
}
list.add(e);
order.add(e);
}
public int getNumEmployees() {return order.size();}
public Employee getEmployee(int n) {return order.get(n - 1);}
public int getNumEmployees(String name) {
ArrayList<Employee> list = employees.get(name);
return list == null ? 0 : list.size();
}
public Employee getEmployee(String name, int n) {
ArrayList<Employee> list = employees.get(name);
return list == null ? null : list.get(n - 1);
}
}
// Test:
Team team = new Team();
team.addEmployee(new Employee("Bob", 11));
team.addEmployee(new Employee("Bob", 12));
team.addEmployee(new Employee("Eve", 13));
team.addEmployee(new Employee("Eve", 14));
System.out.println("Num all: " + team.getNumEmployees());
System.out.println("3rd: " + team.getEmployee(3));
System.out.println("Num Bobs: " + team.getNumEmployees("Bob"));
System.out.println("2nd Bob: " + team.getEmployee("Bob", 2));