MAPS. Java. Return of values - java

I am struggling with following:
I need to create a method which returns a collection of all values that specify some particular selection criterion specified by one or more arguments.
My MAP consists of PPS numbers(keys) and values( town, name, surname, place of work ) Both are strings .
However, I am not sure what I need to do to get the values after placin in the map.
/**
*This method returns a collection of all people who work for CO-OP
*/
public Set<String> selectKeys(String factory)
{
for (Set<String>eachTaxPayers : taxPayersList.values())
{
if(taxPayersList.values().contains(factory))
{
Set<String>eachClients = taxPayersList.keySet();
System.out.println(taxPayersList.keySet());
}
}
return null ;
}
Could someone help me please?
This is a code how Map is populated.
public class Tax
{
Map<String, Set<String>>taxPayersList;
public Tax()
{
taxPayersList = new HashMap<>();
Set<String>taxPayersDetails = new HashSet<>();
taxPayersDetails.add(" Eddie Donegan");
taxPayersDetails.add("Prodieco");
taxPayersDetails.add("Limerick");
taxPayersList.put("4481908A", taxPayersDetails);
taxPayersDetails = new HashSet<>();
taxPayersDetails.add(" Paddy Power");
taxPayersDetails.add("Covenant");
taxPayersDetails.add("Limerick");
taxPayersList.put("6088989B", taxPayersDetails);
taxPayersDetails = new HashSet<>();
taxPayersDetails.add(" Mikey Obama");
taxPayersDetails.add("Prodieco");
taxPayersDetails.add("Limerick");
taxPayersList.put("6788910B", taxPayersDetails);
}
}
I want only to return the key's( PPS numbers) for people who works for the same company

public Set<String> selectKeys(String factory) {
// our return Set
Set<String> factoryWorkers = new HashSet<>();
// Let's iterate over the map entries
for (Map.Entry entry : taxPayersList.entrySet()) {
// Let's grab the value of the current map entruy
Set<String> eachTaxPayers = entry.getValue()
// Risky move
if(eachTaxPayers.contains(factory)) {
// add the key (PPS) to the return set
factoryWorkers.add(entry.getKey());
}
}
return factoryWorkers;
}
FYI, the line marked as "Risky Move" is not the best approach.
Though unlikely, it's possible a city has the same name as factory.
You'd be better using an Iterator on the Set and comparing against the 2nd value.
Even better, instead of having a Map>
you could have a Map
where Employee has fields such as name, city and factoryName.

Related

How to create a HashMap that would have String as key and the value would be another HashMap from one list of custom object?

I have a list of custom object,
public class Assignmentsdata {
String assignmentId;
String teacherId;
String groupName;
String sectionId;
String levelId;
String startTime;
}
ArrayList<Assignmentsdata> list = new ArrayList<>();
lets say there are 20 elements in that list.
Now I want to get the output which is a hashmap of startTime as a key and the Value would be a new HashMap of GroupID and a list of Assignments of those that had the same groupName.
OutPut Example
HashMap<startTime,HasMap<groupName,List> hashMap = new HashMap();
a little more insight about the problem: First I want to categorise based on startTime(Month) then i want to categorise based on groupName, Thanks in advance.
I have successfully categorised based on group name and created a map through below code:
for( int i = 0; i<assignmentsdataArrayList.size();i++ ){
if (hashMap.size()>0){
hashMap.get(assignmentsdataArrayList.get(i).getGroupName()).add(assignmentsdataArrayList.get(i));
}else {
hashMap.put(assignmentsdataArrayList.get(i).getGroupName(),new ArrayList<Assignmentsdata>());
hashMap.get(assignmentsdataArrayList.get(i).getGroupName()).add(assignmentsdataArrayList.get(i));
}
}
After that I am lost on how to categorise this hashmap based on the startDate and create a hashmap that would look like the above hashmap in the output heading.
your code may throw a NullPointerException at the first if branch
if (hashMap.size()>0)
{hashMap.get(assignmentsdataArrayList.get(i).getGroupName()).add(assignmentsdataArrayList.get(i));
}
the map.size()>0 doesnt means the Value of GroupName has put a new ArrayList already.
the anwser of using loop should like this
Map<String, Map<String, List<Assignmentsdata>>> map = new HashMap<>();
for (Assignmentsdata assignmentsdata : list) {
if (!map.containsKey(assignmentsdata.getStartTime())) {
map.put(assignmentsdata.getStartTime(), new HashMap<>());
}
Map<String, List<Assignmentsdata>> startTimeMap = map.get(assignmentsdata.startTime);
if (!startTimeMap.containsKey(assignmentsdata.getGroupName())) {
startTimeMap.put(assignmentsdata.getGroupName(), new ArrayList<>());
}
startTimeMap.get(assignmentsdata.groupName).add(assignmentsdata);
}
or you could use the java stream().collect(Collectors.groupingBy()) api to get the result easily
Map<String, Map<String, List<Assignmentsdata>>> result = list.stream()
.collect(Collectors.groupingBy(Assignmentsdata::getStartTime,Collectors.groupingBy(Assignmentsdata::getGroupName)));
I am answering my own question as I solved it if anyone has a better answer please passed your answer aswell, ill accept another answer suitable and efficient answer.
for( int i = 0; i<assignmentsdataArrayList.size();i++ ){
if (hashMap.size()>0){
if (hashMap.get(assignmentsdataArrayList.get(i).getGroupName())==null){
hashMap.put(assignmentsdataArrayList.get(i).getGroupName(),new ArrayList<Assignmentsdata>());
hashMap.get(assignmentsdataArrayList.get(i).getGroupName()).add(assignmentsdataArrayList.get(i));
}else{
hashMap.get(assignmentsdataArrayList.get(i).getGroupName()).add(assignmentsdataArrayList.get(i));
}
}else {
hashMap.put(assignmentsdataArrayList.get(i).getGroupName(),new ArrayList<Assignmentsdata>());
hashMap.get(assignmentsdataArrayList.get(i).getGroupName()).add(assignmentsdataArrayList.get(i));
}
}
// above part is already in the question. the second part i looped through the hashMap then the list once again, and checking if list and map entry have same group name, then made the startdate key that indexed element from the list.
HashMap<String, Map.Entry<String, ArrayList<Assignmentsdata>>> hashMapHashMap = new HashMap<>();
for (var entry : hashMap.entrySet()){
for( int j = 0; j<assignmentsdataArrayList.size();j++ ){
if (assignmentsdataArrayList.get(j).getGroupName()==entry.getKey()){
hashMapHashMap.put(assignmentsdataArrayList.get(j).getStartTime(),entry);
}
}
hashMapHashMap.put(entry.getValue().get())
}

Extract a list containing duplicates from a list and also get the non-duplicate list java 8 stream

I am reading data from an excel file using apache poi and transforming it into a list of object. But now I want to extract any duplicates based on certain rules into another list of that object and also get the non-duplicate list.
Condition to check for a duplicate
name
email
phone number
gst number
Any of these properties can result in a duplicate. which mean or not an and
Party Class
public class Party {
private String name;
private Long number;
private String email;
private String address;
private BigDecimal openingBalance;
private LocalDateTime openingDate;
private String gstNumber;
// Getter Setter Skipped
}
Let's say this is the list returned by the logic to excel data so far
var firstParty = new Party();
firstParty.setName("Valid Party");
firstParty.setAddress("Valid");
firstParty.setEmail("Valid");
firstParty.setGstNumber("Valid");
firstParty.setNumber(1234567890L);
firstParty.setOpeningBalance(BigDecimal.ZERO);
firstParty.setOpeningDate(DateUtil.getDDMMDateFromString("01/01/2020"));
var secondParty = new Party();
secondParty.setName("Valid Party");
secondParty.setAddress("Valid Address");
secondParty.setEmail("Valid Email");
secondParty.setGstNumber("Valid GST");
secondParty.setNumber(7593612247L);
secondParty.setOpeningBalance(BigDecimal.ZERO);
secondParty.setOpeningDate(DateUtil.getDDMMDateFromString("01/01/2020"));
var thirdParty = new Party();
thirdParty.setName("Valid Party 1");
thirdParty.setAddress("address");
thirdParty.setEmail("email");
thirdParty.setGstNumber("gst");
thirdParty.setNumber(7593612888L);
thirdParty.setOpeningBalance(BigDecimal.ZERO);
secondParty.setOpeningDate(DateUtil.getDDMMDateFromString("01/01/2020"));
var validParties = List.of(firstParty, secondParty, thirdParty);
What I have attempted so far :-
var partyNameOccurrenceMap = validParties.parallelStream()
.map(Party::getName)
.collect(Collectors.groupingBy(Function.identity(), HashMap::new, Collectors.counting()));
var partyNameOccurrenceMapCopy = SerializationUtils.clone(partyNameOccurrenceMap);
var duplicateParties = validParties.stream()
.filter(party-> {
var occurrence = partyNameOccurrenceMap.get(party.getName());
if (occurrence > 1) {
partyNameOccurrenceMap.put(party.getName(), occurrence - 1);
return true;
}
return false;
})
.toList();
var nonDuplicateParties = validParties.stream()
.filter(party -> {
var occurrence = partyNameOccurrenceMapCopy.get(party.getName());
if (occurrence > 1) {
partyNameOccurrenceMapCopy.put(party.getName(), occurrence - 1);
return false;
}
return true;
})
.toList();
The above code only checks for party name but we also need to check for email, phone number and gst number.
The code written above works just fine but the readability, conciseness and the performance might be an issue as the data set is large enough like 10k rows in excel file
Never ignore Equals/hashCode contract
name, email, number, gstNumber
Any of these properties can result in a duplicate, which mean or
Your definition of a duplicate implies that any of these properties should match, whilst others might not.
It means that it's impossible to provide an implementation equals/hashCode that would match the given definition and doesn't violate the hashCode contract.
If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
I.e. if you implement equals in such a way they any (not all) of these properties: name, email, number, gstNumber could match, and that would enough to consider the two objects equal, then there's no way to implement hashCode correctly.
And as the consequence of this, you can't use the object with a broken equals/hashCode implementation in with a hash-based Collection because equal objects might end up the in the different bucket (since they can produce different hashes). I.e. HashMap would not be able to recognize the duplicated keys, hence groupingBy with groupingBy() with Function.identity() as a classifier function would not work properly.
Therefore, to address this problem, you need to implement equals() based on all 4 properties: name, email, number, gstNumber (i.e. all these values have to be equal), and similarly all these values must contribute to hash-code.
How to determine Duplicates
There's no easy way to determine duplicates by multiple criteria. The solution you've provided is not viable, since we can't rely on the equals/hashCode.
The only way is to generate a HashMap separately for each end every attribute (i.e. in this case we need 4 maps). But can we alternate this, avoiding repeating the same steps for each map and hard coding the logic?
Yes, we can.
We can create a custom generic accumulation type (it would be suitable for any class - no hard-coded logic) that would encapsulate all the logic of determining duplicates and maintain an arbitrary number of maps under the hood. After consuming all the elements from the given collection, this custom object would be aware of all the duplicates in it.
That's how it can be implemented.
A custom accumulation type that would be used as container of a custom Collector. Its constructor expects varargs of functions, each function correspond to the property that should be taken into account while checking whether an object is a duplicate.
public static class DuplicateChecker<T> implements Consumer<T> {
private List<DuplicateHandler<T>> handles;
private Set<T> duplicates;
#SafeVarargs
public DuplicateChecker(Function<T, ?>... keyExtractors) {
this.handles = Arrays.stream(keyExtractors)
.map(DuplicateHandler::new)
.toList();
}
#Override
public void accept(T t) {
handles.forEach(h -> h.accept(t));
}
public DuplicateChecker<T> merge(DuplicateChecker<T> other) {
for (DuplicateHandler<T> handler: handles) {
other.handles.forEach(handler::merge);
}
return this;
}
public DuplicateChecker<T> finish() {
duplicates = handles.stream()
.flatMap(handler -> handler.getDuplicates().stream())
.flatMap(Set::stream)
.collect(Collectors.toSet());
return this;
}
public boolean isDuplicate(T t) {
return duplicates.contains(t);
}
}
A helper class representing a single createrion (like name, email, etc.) which encapsulates a HashMap. keyExtractor is used to obtain a key from an object of type T.
public static class DuplicateHandler<T> implements Consumer<T> {
private Map<Object, Set<T>> itemByKey = new HashMap<>();
private Function<T, ?> keyExtractor;
public DuplicateHandler(Function<T, ?> keyExtractor) {
this.keyExtractor = keyExtractor;
}
#Override
public void accept(T t) {
itemByKey.computeIfAbsent(keyExtractor.apply(t), k -> new HashSet<>()).add(t);
}
public void merge(DuplicateHandler<T> other) {
other.itemByKey.forEach((k, v) ->
itemByKey.merge(k,v,(oldV, newV) -> { oldV.addAll(newV); return oldV; }));
}
public Collection<Set<T>> getDuplicates() {
Collection<Set<T>> duplicates = itemByKey.values();
duplicates.removeIf(set -> set.size() == 1); // the object is proved to be unique by this particular property
return duplicates;
}
}
And that is the method, responsible for generating the map of duplicates, that would be used from the clean code. The given collection would be partitioned into two parts: one mapped to the key true - duplicates, another mapped to the key false - unique objects.
public static <T> Map<Boolean, List<T>> getPartitionByProperties(Collection<T> parties,
Function<T, ?>... keyExtractors) {
DuplicateChecker<T> duplicateChecker = parties.stream()
.collect(Collector.of(
() -> new DuplicateChecker<>(keyExtractors),
DuplicateChecker::accept,
DuplicateChecker::merge,
DuplicateChecker::finish
));
return parties.stream()
.collect(Collectors.partitioningBy(duplicateChecker::isDuplicate));
}
And that how you can apply it for your particular case.
main()
public static void main(String[] args) {
List<Party> parties = // initializing the list of parties
Map<Boolean, List<Party>> isDuplicate = partitionByProperties(parties,
Party::getName, Party::getNumber,
Party::getEmail, Party::getGstNumber);
}
I would use create a map for each property where
key is the property we want to check duplicate
value is a Set containing all the index of element in the list with same key.
Then we can
filter values in the map with more that 1 index (i.e. duplicate indexes).
union all the duplicate index
determine if the element is duplicate/unique by using the duplicate index.
The time complexity is roughly O(n).
public class UniquePerEachProperty {
private static void separate(List<Party> partyList) {
Map<String, Set<Integer>> nameToIndexesMap = new HashMap<>();
Map<String, Set<Integer>> emailToIndexesMap = new HashMap<>();
Map<Long, Set<Integer>> numberToIndexesMap = new HashMap<>();
Map<String, Set<Integer>> gstNumberToIndexesMap = new HashMap<>();
for (int i = 0; i < partyList.size(); i++) {
Party party = partyList.get(i);
nameToIndexesMap.putIfAbsent(party.getName(), new HashSet<>());
nameToIndexesMap.get(party.getName()).add(i);
emailToIndexesMap.putIfAbsent(party.getEmail(), new HashSet<>());
emailToIndexesMap.get(party.getEmail()).add(i);
numberToIndexesMap.putIfAbsent(party.getNumber(), new HashSet<>());
numberToIndexesMap.get(party.getNumber()).add(i);
gstNumberToIndexesMap.putIfAbsent(party.getGstNumber(), new HashSet<>());
gstNumberToIndexesMap.get(party.getGstNumber()).add(i);
}
Set<Integer> duplicatedIndexes = Stream.of(
nameToIndexesMap.values(),
emailToIndexesMap.values(),
numberToIndexesMap.values(),
gstNumberToIndexesMap.values()
).flatMap(Collection::stream).filter(indexes -> indexes.size() > 1)
.flatMap(Set::stream).collect(Collectors.toSet());
List<Party> duplicatedList = new ArrayList<>();
List<Party> uniqueList = new ArrayList<>();
for (int i = 0; i < partyList.size(); i++) {
Party party = partyList.get(i);
if (duplicatedIndexes.contains(i)) {
duplicatedList.add(party);
} else {
uniqueList.add(party);
}
}
System.out.println("duplicated:" + duplicatedList);
System.out.println("unique:" + uniqueList);
}
public static void main(String[] args) {
separate(List.of(
// name duplicate
new Party("name1", 1L, "email1", "gstNumber1"),
new Party("name1", 2L, "email2", "gstNumber2"),
// number duplicate
new Party("name3", 3L, "email3", "gstNumber3"),
new Party("name4", 3L, "email4", "gstNumber4"),
// email duplicate
new Party("name5", 5L, "email5", "gstNumber5"),
new Party("name6", 6L, "email5", "gstNumber6"),
// gstNumber duplicate
new Party("name7", 7L, "email7", "gstNumber7"),
new Party("name8", 8L, "email8", "gstNumber7"),
// unique
new Party("name9", 9L, "email9", "gstNumber9")
));
}
}
Assume Party has below constructor and toString()(for testing)
public class Party {
public Party(String name, Long number, String email, String gstNumber) {
this.name = name;
this.number = number;
this.email = email;
this.address = "";
this.openingBalance = BigDecimal.ZERO;
this.openingDate = LocalDateTime.MIN;
this.gstNumber = gstNumber;
}
#Override
public String toString() {
return "Party{" +
"name='" + name + '\'' +
", number=" + number +
", email='" + email + '\'' +
", gstNumber='" + gstNumber + '\'' +
'}';
}
...
}

How do I add up and remove repeated objects from ArrayList? [duplicate]

This question already has answers here:
Java - Merge objects of list given a condition
(2 answers)
Closed 2 years ago.
User Detail model:
private String userName;
private int userSalary;
I've a ArrayList of user information
List<UserDetail> userDetails = new ArrayList<>();
UserDetail user1 = new UserDetail("Robert", 100);
UserDetail user2 = new UserDetail("John", 100);
UserDetail user3 = new UserDetail("Robert", 55);
userdetails.add(user1);
userdetails.add(user2);
userdetails.add(user3);
I'm trying to iterate through the array and find out if there are any duplicate entries based on userName, from the above list I've two records with same user name "Robert", in this case I want to add up the userSalary and remove one record from the List.
Expected new ArrayList:
userName userSalary
Robert 155
John 100
Is this possible ??
userDetails.stream()
.collect(Collectors.toMap(
UserDetail::getName,
Function.identity(),
(left, right) -> {
left.setSalary(left.getSalary() + right.getSalary());
return left;
}
))
.values();
This will give you a Collection<UserDetail>. You can copy that into an ArrayList if needed, obviously.
Because your goal is to group UserDetail objects that share the same userName, I recommend storing the result in a Map instead of an ArrayList.
This is possible by streaming your List and collecting it to a Map using Collectors#groupingBy in conjunction with Collectors#summingInt:
List<UserDetail> userDetails = new ArrayList<>();
UserDetail user1 = new UserDetail("Robert", 100);
UserDetail user2 = new UserDetail("John", 100);
UserDetail user3 = new UserDetail("Robert", 55);
userDetails.add(user1);
userDetails.add(user2);
userDetails.add(user3);
Map<String, Integer> groupedUserDetails = userDetails.stream()
.collect(Collectors.groupingBy(UserDetail::getUserName,
Collectors.summingInt(UserDetail::getUserSalary)));
System.out.println(groupedUserDetails);
This above snippet may output the following:
{Robert=155, John=100}
If you want to convert this Map<String, Integer> into a List<UserDetail>, then you can use the following:
List<UserDetail> newUserDetails = groupedUserDetails.entrySet()
.stream()
.map(entry -> new UserDetail(entry.getKey(), entry.getValue())
.collect(Collectors.toList());
Here is a solution without streams(may be easier to understand):
Iterator<UserDetail> it=userDetails.iterator();
Map<String,UserDetail> found=new HashMap<>();
while(it.hasNext()){
UserDetail next=it.next();
if(found.containsKey(next.getUserName())){
found.get(next.getUserName()).setUserSalery(found.get(next.getUserName()).getUserSalery()+next.getUserSalery();
it.remove();
}
else{
found.put(next.getUserName(),next);
}
}
This iterates through all elements.
If it has already found a matching element, it adds its own salery to it and removes itself out of the list.
If not, it marks itself to be found if other elemts are found with the same name later.
This assumes that UserDetail has standard getter/setter methods for userName and userSalery.
Note that a for-each loop cannot be used because you cannot modify the content of the List in there (it would throw a ConcurrentModificationException).
From the comments(from #Holger:
You can use a single UserDetail previous = found.putIfAbsent(next.getName());, followed by if(previous != null) { previous.setSalery(previous.getSalery()+next.getSalery()); it.remove(); } instead of looking up the map three times in a row.
That code would be:
Iterator<UserDetail> it=userDetails.iterator();
Map<String,UserDetail> found=new HashMap<>();
while(it.hasNext()){
UserDetail next=it.next();
UserDetail previous = found.putIfAbsent(next.getUserName());
if(previous != null) {
previous.setUserSalery(previous.getUserSalery()+next.getUserSalery());
it.remove();
}
}
This essentially does the same thing.
It adds the current element to the List if it does not exist and if not, it just adds up the salery.
I can suggest an alternate approach. HashMap
Use username as key
override equals & hashcode
Before adding an element to the hashMap, check if any object already present
If object found, get salary from it, update the current object, and then add to map
public class UserDetail {
private String userName;
private int userSalary;
#Override
public boolean equals(Object obj) {
return this.userName.equals(((UserDetail)obj).userName);
}
#Override
public int hashCode() {
return userName.length();
}
}
Here Map is used to store UserDetail by userName. If user is in map salary will be add up and update in the map. Other wise user will be put in the map. Finally the map values will be converted to a UserDetail list
Map<String, UserDetail> mergedMap = new HashMap<>(); // username is used as the key
userDetails.forEach(userDetail -> {
String userName = userDetail.getUserName();
UserDetail userInMap = mergedMap.get(userName);
if (userInMap != null) { // if user is in map salary will be added
userInMap.setUserSalary(userInMap.getUserSalary() + userDetail.getUserSalary());
} else { //otherwise user will put in map
mergedMap.put(userDetail.getUserName(), userDetail);
}
});
List<UserDetail> usersWithMergedSalaries = new ArrayList<>(mergedMap.values()); //convert map values to a list

prepare a multi valued country map

I want to prepare a HashMap in such way that
Key : Country Code
Value : List of returned orderEntries
the following process data method process every 5 orderEntry which can be from any country.
let me make it more clear. I have list of orderEntries that come from different countries now I want to put these entries into map based on country key. Like if 20 entries coming from US then US will be the key and 20 Entries would be the values. But problem is that I don't want to create a list for each county inside map.
public void processSegmentData(final List resultSet)
{
for (final Object orderEntry : resultSet)
{
if (orderEntry instanceof OrderEntryModel)
{
String countryCode = null;
final OrderModel order = ((OrderEntryModel) orderEntry).getOrder();
if (order.getDeliveryAddress() != null)
{
countryCode = order.getDeliveryAddress().getCountry().getIsocode();
}
orderEntriesMap.put(Config.getParameter(countryCode+".return.pid"), orderEntries);
}
}
}
so you are after a hashmap which contains a linked list Something along the lines of:
public HashMap<String, LinkedList<OrderEntryModel>> processSegmentData(final List resultSet) {
HashMap<String, LinkedList<OrderEntryModel>> orderEntriesMap = new HashMap<String, LinkedList<OrderEntryModel>>();
for (final Object orderEntry : resultSet) {
if (orderEntry instanceof OrderEntryModel) {
String countryCode = null;
final OrderModel order = ((OrderEntryModel) orderEntry).getOrder();
if (order.getDeliveryAddress() != null) {
countryCode = order.getDeliveryAddress().getCountry().getIsocode();
}
if (!orderEntriesMap.containsKey(countryCode)) {
orderEntriesMap.put(countryCode, new LinkedList<OrderEntryModel>());
}
orderEntriesMap.get(countryCode).add((OrderEntryModel) orderEntry);
}
}
return orderEntriesMap;
}
would be an example based on the source code you provided guessing object names.
But problem is that I don't want to create a list for each county
inside map.
I understand your problem but map store unique key, you can not store same country code.
you have to use Map<String, List<String>>() that will hold your country code as key and then put your values inside List<String>.
after doing this if you have any problem edit your question will help you to resolve that.
Just Create a Map<String,List<String>>. and follow the following approach
Map<String,List<String>> countryMap = new HashMap<String, List<String>>();
for (final String orderEntry : orders){
if(countryMap.containsKey(orderEntry.getCountry())){
countryMap.get(orderEntry.getCountry()).add(orderEntry);
}else{
//create a new list and add orderEntry
countryMap.put(orderEntry.getCountry(),orderEntry);
}
}
You need to modify this according to your stuff
You could use Guava's Multimap to simplify things. A Multimap allows you to store multiple entries against a single key, e.g.:
Multimap<String, OrderEntry> orderEntriesMultimap = HashMultimap.create();
for (final Object orderEntry : resultSet) {
// omitted...
orderEntriesMultimap.put(Config.getParameter(countryCode+".return.pid"), orderEntry);
}
You can then retrieve all the associated values by key:
Collection<OrderEntryModel> entries = orderEntriesMultimap.get(key);

Associate multiple values with the single key and also maintain the order of values

I have a class which contain the following members
private String patientName;
private String patientPhoneNumber;
now I have multiple names attached with the phone No. for example
1234567, AAA
1234567, BBB
8765432, CCC
8765432, GGG
Now I want to store them in a Map but the phone No. should be the key having multiple values, for 1234567 i should have value AAA and BBB, please advise how can I store the multiple values with the single key in map here my key is Phone No. and then please let me know if I want to print in console then ow would I iterate over the Map
Also please not that I want to maintain the order also let say first I get the value AAA and then BBB so i should maintain these order also, since I get this is just a example but in my scenario I will be getting this value from backend so to maintain the order is also necessary please advise.
You may try something like this:
HashMap<String,LinkedList<String>> map
private Map<String,List<String>> patients;
public void setPatientNumber(String number, String patient){
List<String> list = patients.get(number);
if(list == null){
list = new ArrayList<String>();
patients.put(number,list);
}
list.add(patient);
}
new Map<String, TreeSet<String>>()
Will allow you to store the values in a TreeSet (sorted...).
To print them:
for(Map.Entry entry : phoneBook.entries()){
System.out.println(entry.key() + ":");
TreeSet names = entry.value();
for(String name : names){
System.out.println("\t" + name);
}
}
You can add, like this, if you want case insensitive ordering:
TreeSet<String> nameSet = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
phoneBook.add(number, nameSet);
Use a LinkedHashMap and an ArrayList for each values :
LinkedHashMap<String,ArrayList<String>> phoneNumbers = new LinkedHashMap<String,ArrayList<String>>();
// register new phone number
phoneNumbers.put("1234567", new ArrayList<String>());
// add names to the phone number
phoneNumbers.get("1234567").add("AAA");
phoneNumbers.get("1234567").add("BBB");
Both collections preserve the insertion ordering.
** Edit **
Here, this is roughly what you'd need (this was done on the spot without much testing, but you should get the idea). Since your ordering may vary, I thought limiting duplicates and providing a comparator for ordering should be preferable (as suggested by other answers) :
public class LinkedMultiMap<K,V> {
private Comparator<V> comparator;
private LinkedHashMap<K,Set<V>> entries;
public LinkedMultiMap() {
this(null);
}
public LinkedMultiMap(Comparator<V> comparator) {
this.comparator = comparator;
this.entries = new LinkedHashMap<K, Set<V>>();
}
public boolean add(K key, V value) {
if (!entries.containsKey(key)) {
entries.put(key, new TreeSet<V>(comparator));
}
return entries.get(key).add(value);
}
public Collection<V> get(K key) {
return entries.get(key);
}
public boolean remove(K key, V value) {
boolean removed = false;
if (entries.containsKey(key)) {
removed = entries.get(key).remove(value);
if (entries.get(key).isEmpty()) {
entries.remove(key);
}
}
return removed;
}
public Collection<V> removeAll(K key) {
return entries.remove(key);
}
public Iterator<K> keyIterator() {
return entries.keySet().iterator();
}
}
Associate multiple values with the single key
That is a Multimap:
A collection similar to a Map, but which may associate multiple values with a single key.
LinkedHashMultimap from Google Collections seems to fit the bill:
Implementation of Multimap that does not allow duplicate key-value
entries and that returns collections whose iterators follow the
ordering in which the data was added to the multimap.
If you don't want to add the dependency, you can use a collection as value type:
Map<String /*number*/, List<String> /*names*/> numbers;
Note that the former only allows retrieval in order of insertion, if you want to be able to change it you will have to use the latter hand-rolled solution
Use a Map to store your data. The keys should be String objects and the values should be List objects. Using a List as the map entry value allows to associate multiple values with a single key. A List will also maintain the order of adding elements.
public static void main(String[] args) {
Map<Integer,List<String>> map = new HashMap<Integer,List<String>>();
//insert values into list one
List<String> list1 = new ArrayList<String>();
list1.add("AAA");
list1.add("BBB");
//insert values into list one
List<String> list2 = new ArrayList<String>();
list2.add("CCC");
list2.add("GGG");
//insert values to keys
//single key multiple values
map.put(1234567, list1);
map.put(8765432, list2);
//iterating and displaying the values
for(Map.Entry<Integer,List<String>> entry: map.entrySet() ) {
Integer key = entry.getKey();
List<String> values = entry.getValue();
System.out.println("Value of " +key+ " is " +values);
//System.out.println("Value of " +key+ " is " +values.get(0));
//System.out.println("Value of " +key+ " is " +values.get(1));
}
}

Categories

Resources