patternjavaMinor
Sorting ArrayList
Viewed 0 times
sortingarrayliststackoverflow
Problem
Here is the task:
Create several classes of flowers that extend abstract flower. all fields -- random.
Solution:
Every class of a particular flower looks like this:
Main method:
```
public class Main {
public static List flowers = new ArrayList();
public static List bunch = new ArrayList();
public static Random random = new Random();
public static double bunchPrice;
public static double stalkMin = 30;
public static double stalkMax = 100;
public static void main(String[] args) {
flowers.add(new Rose());
flowers.add(new Orchid());
flowers.add(new Gerbera());
flowers.add(new Daisy());
for (int i = 0; i stalkMin && f.getStalk() < stalkMax){
System.out.println(f.getName() + " " + f.getStalk());
}
}
System.out.println("\naverage level of freshness in the bunch ====================");
double sumFreshness = 0;
for(Flower f: b
Create several classes of flowers that extend abstract flower. all fields -- random.
- make a bunch of 20 different flowers
- calculate the price of the bunch
- sort bunch by freshness of flowers
- find flowers with stalks in a certain range
- find average level of freshness of the bunch
Solution:
public abstract class Flower implements Comparable {
public String name;
public double freshness;
public double stalk;
public double price;
public Flower(String name) {
super();
Random rand = new Random();
this.name = name;
this.freshness = rand.nextDouble() * 100;
this.stalk = rand.nextDouble() * 150 + 30;
this.price = rand.nextDouble() * 1000 + 1;
}
// getters and setters
@Override
public int compareTo(Flower that) {
return (int) (that.getFreshness() - this.getFreshness());
}
@Override
public String toString() {
return this.name + " " + this.freshness + "\n";
}Every class of a particular flower looks like this:
public class Rose extends Flower {
public Rose() {
super("rose");
}
}Main method:
```
public class Main {
public static List flowers = new ArrayList();
public static List bunch = new ArrayList();
public static Random random = new Random();
public static double bunchPrice;
public static double stalkMin = 30;
public static double stalkMax = 100;
public static void main(String[] args) {
flowers.add(new Rose());
flowers.add(new Orchid());
flowers.add(new Gerbera());
flowers.add(new Daisy());
for (int i = 0; i stalkMin && f.getStalk() < stalkMax){
System.out.println(f.getName() + " " + f.getStalk());
}
}
System.out.println("\naverage level of freshness in the bunch ====================");
double sumFreshness = 0;
for(Flower f: b
Solution
@Override
public int compareTo(Flower that) {
return (int) (that.getFreshness() - this.getFreshness());
}This may not work as what you expect if you are comparing two freshness ratings that are less than 1... Consider:
- Flower A: 1.1
- Flower B: 1.0
When you subtract B's freshess from A's, you get
1.1 - 1.0 = 0.1. Now cast that to an int and you get... 0. What you are looking for here is probably Double.compare(double, double).Now, if you happen to be on Java 8, you can make use of
Streams to make your code simpler... As a guide, I'll illustrate how points (2) and (3) can be achieved (assuming your bunch is created as a List).-
Calculate price of bunch
Assuming you have a getter method on your
Flower, e.g. getPrice():return bunch.stream().mapToDouble(Flower::getPrice).sum();This firstly converts our
Stream into a DoubleStream via mapToDouble(), before calling the sum() to get our answer. No explict looping, you simply perform the required series of operations on the Stream.-
Sort bunch by freshness of flowers
Originally, I thought the
SortedSet approach suggested by @Nicolas Gallegos is pretty OK, but then realized it being a Set will not allow duplicate objects. No worries using the Stream approach still though, as you can easily sort it first. Assuming you have a getter method on your Flower, e.g. getFreshness():return bunch.stream().sorted(Comparator.comparing(Flower::getFreshness))
.collect(Collectors.toList());We construct a
Comparator via comparing(), which is used in the sorted() method. Having sorted the Stream, we then collect the elements into a resulting List using Collectors.toList().I hope the two examples have given you a good enough sneak peek at the features of Java 8
Streams. :)Code Snippets
@Override
public int compareTo(Flower that) {
return (int) (that.getFreshness() - this.getFreshness());
}return bunch.stream().mapToDouble(Flower::getPrice).sum();return bunch.stream().sorted(Comparator.comparing(Flower::getFreshness))
.collect(Collectors.toList());Context
StackExchange Code Review Q#97689, answer score: 3
Revisions (0)
No revisions yet.