I have a task to play with Java Collections framework. I need to obtain a users list from a database, and store it in a collection. (This is finished and users are stored in a HashSet). Each user is an instance of Person class described with name, surname, birth date, join date, and some other parameters which are not important now. Next I need to store the list in different collections (there's nowhere stated how many) providing functionality to sort them by:
- name only
- name, surname, birthdate
- join date
Ok, so to start with, my Person stores data as Strings only (should I change dates to Date ?). I've started implementing sorting with "by name, surname, birthdate", cause that's what I get after calling sort on list with Strings. Am I right ?
public List createListByName(Set set){
List ret = new ArrayList<String>();
String data = "";
for(Object p: set){
data = p + "\n";
ret.add(data);
}
Collections.sort(ret);
return ret;
}
But what with the rest ? Here's my Person :
class Person {
private String firstname;
private String lastname;
)..)
Person(String name, String surname, (..)){
firstname = name;
lastname = surname;
(..)
}
#Override
public String toString(){
return firstname + " " + lastname + " " + (..);
}
}
I wouldn't convert everything to strings to start with. I would implement Comparator<Person> and then sort a List<Person>:
public List<Person> createListByName(Set<Person> set){
List<Person> ret = new ArrayList<Person>(set);
Collections.sort(ret, new NameSurnameBirthComparator());
return ret;
}
The NameSurnameBirthComparator would implement Comparator<Person> and compare two people by first comparing their first names, then their surnames (if their first names are equal) then their birth dates (if their surnames are equal).
Something like this:
public int compare(Person p1, Person p2) {
// TODO: Consider null checks, and what to do :)
int firstNameResult = p1.getFirstName().compareTo(p2.getFirstName());
if (firstNameResult != 0) {
return firstNameResult;
}
int surnameResult = p1.getSurname().compareTo(p2.getSurname());
if (surnameResult != 0) {
return surnameResult;
}
return p1.getBirthDate().compareTo(p2.getBirthDate());
}
And yes, I would store the date of birth as a Date - or preferably as a LocalDate from JodaTime, as that's a much nicer library for date and time manipulation :)
so I should write multiple comprators on Person for each task ?
Given this is a homework task, then I would say that is the way you would start to learn about Comparators.
For interest sake only you can do this by creating a couple of resuable Comparators.
You can use the Bean Comparator to sort on individual properties.
Then you can use the Group Comparator to sort on multiple properties.
Related
I have a Sorted Set in Java with an object with 2 strings, Name and Age. Name is unique.
Now I have the Name and I want to get the age based on the name.
I have my object:
SortedSet<Person> people;
That has 3 people inside: "John / 35", "James / 21" and "Maria /21"
Based on this, I want to check James age.
How can I do it? The only idea I have is just doing a for, but I guess it should be something easier.
I see two solutions there:
If there really are just this two properties, you could simply convert that to a map, where the name is the key and the age is the value, ( Map<String, Integer> ageMap). Then you can quickly get the age by using ageMap.get("James");.
Edit: To convert you can do this:
Map<String, Integer> ageMap = new HashMap<>();
for (Person p : people) {
ageMap.put(p.getName(), p.getAge());
}
int jamesAges = ageMap.get("James");
If you stay with the Set and the Person class, I would recommend using the streams:
Optional findFirst = set.stream().filter(e -> e.getName().equals("James")).findFirst();
if (findFirst.isPresent()) {
int age = findFirst.get().getAge();
}
Internally, this will probably still use some kind of for, but the real implementation might be a bit more optimized.
I would not use a set for this since you cannot easily retrieve values from a set. I would go with a map. You can populate the map anyway you like.
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
#Override
public String toString() {
return "[" + name + ", " + age +"]";
}
}
Map<String, Person> people = new HashMap<>(Map.of("john", new Person("John",35),
"james", new Person("James", 21), "maria", new Person("Maria", 21)));
String name = "James";
Person person = people.get(name.toLowerCase());
System.out.println(person != null
? name + "'s age is "+ person.getAge()
: name + " not found");
prints
James's age is 21
I didn't find proper solution for the below scenario. I have employee names and location. In each location many employees can work.
Example: assume that employee names are unique so I consider it as a key and value as location.
TreeMap<String,String> t=new TreeMap<String,String>();
t.put(mike, Houston);
t.put(arian, Houston);
t.put(John, Atlanta);
Well my scenario is i have to write my own comparator where location is sorted first and when there are multiple locations of same name then they need to be sorted by employees. Any kind of help is appreciated.
you need a structure, and compareTo:
public class EmpLoc implements Comparable<EmpLoc> {
String employee;
String location;
public EmpLoc (String _employee, String _location)
{
employee=_employee;
location=_location;
}
#Override
public int compareTo(EmpLoc other)
{
int last = this.location.compareTo(other.location);
return last == 0 ? this.employee.compareTo(other.employee) : last;
}
}
The problem is in your data structure. TreeMap ensure your keys are always sorted in an order, but your key doesn't have full information you need to sort. Instead what you need is probably
TreeSet<Employee> employees = new TreeSet<>(employeeComparator);
where Employee is:
public class Employee {
private String name;
private String location;
/* getters & setters omitted */
}
Now you can create a comparator for Employee
You can use similar structure:
Map<String, List<String>> map = new TreeMap<>(<your_own_comparator_for_locations_or_default_one>);
This is Multimap, and this is implementation by conventional means, but also there are third-party implementation, e.g. Guava. Guava has some sorted, synchronized and immutable implementations of multimaps, you can use them by default or to see how to do some things.
You can put values like below:
public void putEmployees(String location, String employee) {
List<String> employees = map.get(location);
if (employee == null) {
employees = new ArrayList<>();
}
employees.add(employee);
Collections.sort(employees, <your_own_comparator_for_employees_or_default_one>);
map.put(location, employees);
}
Imagine we are pulling data about people and their favourite foods.
The data would come to us in the format: "Name, FavFood1, FavFood2..FavFoodn".
e.g. "James, Beans, Chicken".Notice how we do not know how many foods a person will favour.
From this data we create an instance of a Person object which captures the person's name and favourite foods. After we have pulled data on every person, we want to create a spreadsheet whose columns would be: Name|Potato|Chicken|Beans|Curry etc.
All of the values to the right of the person's name will be simple boolean values representing whether or not that food was one of the person's favourites.
The problem is: we do not know in advance; all the foods that someone could possibly favour, and as such cannot just set up boolean instance variables in the Person class.
I've given this some thought, implementing sets,hash-sets and hash-maps, however every solution I think of ends up being horribly inelegant and so I've turned to the genius of stackoverflow for help on this one.
My question is: What design pattern / approach can I use to cleanly achieve the outcome I desire? Whilst this is a language-agnostic question I am programming this in Java, so if there's anything in the Java API or elsewhere built for this, do let me know.
Thanks in advance!
Try this. It generates data in csv form.
class Person {
final String name;
final Set<String> foods;
Person(String name, Set<String> foods) {
this.name = name;
this.foods = foods;
}
Stream<Boolean> getBooleans(List<String> foods) {
return foods.stream().map(food -> this.foods.contains(food));
}
#Override
public String toString() {
return "Person(" + name + ", " + foods +")";
}
}
class Test {
public static void main(String[] args) throws Exception {
List<String> data = Arrays.asList(
"James, Beans, Chicken",
"Emily, Potato, Curry",
"Clara, Beans, Curry"
);
List<String> foodNames = Arrays.asList(
"Potato", "Chicken", "Beans", "Curry"
);
Stream<Person> persons = data.stream().map(d -> {
String[] split = d.split(",");
for(int i = 0; i < split.length; i++) {
split[i] = split[i].trim();
}
String name = split[0];
Set<String> foods = Stream.of(split).skip(1).collect(Collectors.toSet());
return new Person(name, foods);
});
Stream<String> csvData = persons.map(p ->
p.name + ", " + p.getBooleans(foodNames)
.map(b -> b.toString())
.collect(Collectors.joining(", "))
);
csvData.forEach(System.out::println);
}
}
First of all, I highly recommend that whatever you do it in a separate class with methods like addFavoriteFood(String food) and boolean isFavoriteFood(String food) getFavorites(String food).
Personally I think the implementation of this class should contain both an instance HashSet (to hold the foods this person likes) and a SortedSet that is common to all the foods that can contain a list of ALL foods. (See notes at end)
Add would add it to both sets, getFavorites would return those in the first Hash set.
Hmm, it may also need a static getAllFavorites() method to return the SortedSet
Since your FavoiteFoods class knows the master list AND the person's favorites, you could even have it do most of the work by having a getFormattedRow() and static getFormattedHeaderRow() method. then your implementaiton is just:
System.out.println(FavoriteFoods.getFormattedHeaderRow());
for(Person person:people)
System.out.println(person.favoriteFood.getFormattedRow());
Again, the best thing here is that you can just use the Simplest Thing That Could Possibly Work for your implementation and re-do it later if need be since, being isolated in another class, it doesn't infect all your code with nasty implementation-specific sets, classes, booleans, etc.
Notes about the master list: This master list could naively be implemented as a Static but that's a bad idea--optimally the same masterList SortedSet would be passed into each instance on construction. Also since it is shared among all instances and is mutable it brings in issues if your solution is threaded!
What is so inelegant about this pseudocode?
Set<String> allFoods = new TreeSet<String>();
List<Person> allPersons = new ArrayList<Person>();
while (hasMorePersons()) {
Person person = getNextPerson();
allPersons.add(person);
allFoods.addAll(person.getFoods());
}
spreadSheet.writeHeader("Name", allFoods);
for (Person person : allPersons) {
spreadSheet.writeName(person.getName());
for (String food : allFoods) {
// assume getFoods() return a Set<String>,
// not necessarily ordered (could be a HashSet)
boolean yourBooleanHere = person.getFoods().contains(food);
spreadSheet.writeBoolean(yourBooleanHere);
}
spreadSheet.nextLine();
}
If you need a table of booleans or whatever else, you can easily store them anywhere you want during the second loop.
Note: TreeSet orders foods according to the natural order (that is, alphabetically). To output them in the order they are encountered, use a LinkedHashSet instead.
I am attemping to populate a JComboBox with the names of cities.
My program has a class called 'Country'. The Country object contains a HashMap of objects called 'City' with a method getName, returning a String value.
public class Country {
private final Map<String, City> cities = new HashMap<>();
public Collection<City> getCities() {
return cities.values();
}
}
public class City {
String cityName;
public String getName() {
return cityName;
}
}
Is it possible to return an String array of cityName without using a loop? I was trying the following but it did not work:
Country country 1 = new Country();
String[] cityNames = country1.getCities().toArray();
JComboBox cityChoice = new JComboBox(cityNames);
This returns an Array of City objects, however I am not sure how to use the City getName method in conjunction with this.
You can not avoid looping. Either, you will loop, or Java will loop in the background.
You can avoid writing your own loop if keys in your map are city names. Then, you could only ask .keySet() from the map. But, even in that case, Java would loop in the background and copy the keys.
Other way is that you loop, but hide the loop in some method (lets say getCitiesArray()) in the class. So, you could do country1.getCitiesArray(); in the calling method. Code would look better and be easier to read, but you would still need to have loop inside of the class.
You can store Map key as CityName then do below to get Names.
cities.keySet();
The city object can be used directly in the combobox with some minor alterations.
public class City {
String cityName;
public String getName() {
return cityName;
}
#Override
public String toString() {
return getName();
}
}
Then the population code
Country country1 = new Country();
City[] cities = country1.getCities().toArray();
JComboBox<City> cityChoice = new JComboBox<City>(cities);
You should probably override hashCode and equals also.
If you are using Java 8, you can use the Stream API to map the names of the cities to a String:
String []cityNames = country1.getCities().stream().map(City::getName).toArray(String[]::new);
I have a customer object with three properties: last name, first name, and SIN number
If the user enters the customer's last name and first name, and the object is found in the arraylist, the objects gets removed.
private static void deleteCustomer (String lastName, String firstName, List<Customer> accounts)
{
for (int i = 0; i < accounts.size(); i++)
{
accounts.get(i);
public int compare (Customer c1, Customer c2)
{
Customer customerOne = (Customer) c1;
Customer customerTwo = (Customer) c2;
if (lastName.equals(CustomerOne.getLastName()) && firstName.equals(CustomerOne.getFirstName()))
{
}
}
}
}
If two customers have the same first and last name, the user is asked to enter the SIN number, that's why I have the compare method. I'm not sure what to do after this.
Updated method:
public static void deleteCustomer (String lastName, String firstName, List<Customer> accounts)
{
for (Iterator<Customer> iterator = accounts.iterator(); iterator.hasNext();)
{
Customer customer = iterator.next();
if(lastName.equals(customer.getLastName()) && firstName.equals(customer.getFirstName()))
{
iterator.remove();
}
}
}
This works, but it removes all customers with the same first and last name
If you simply wish to delete one customer as you have described in the question you should break the loop after you have found the correct customer and removed it:
iterator.remove();
break; // breaks the loop
But, since it seems as if there may be multiple customers with the same first+lastname the algorithm is not exactly bullet-proof. Maybe a customer id or similar should be used to distinguish between customers. In the case of a customer id the data structure to use should quite possibly be a Map<CustomerId, Customer>. Customers can then be quickly accessed via the id and you still can retrieve a collection of all customers by invoking the values() method.
In addition to this, if you don't want to modify the original List you can also use a Java 8 construct where you stream, filter and collect your data.
final List<Customer> newAccountList = accounts.stream()
.filter(c -> !(Objects.equals(c.getFirstName(), firstName) &&
Objects.equals(c.getLastName(), lastName)))
.collect(Collectors.toList());
The List above contains everything from the original List except for those entries where first+lastname matches.