Сортиране на ArrayList
нов съм и не ми ясно как се създава сортиране . Трябва да създам сортировака в определен диапазон , опитах се да създам нов клас и да му задам параметри но не се получи .Някой може ли да помогне
нов съм и не ми ясно как се създава сортиране . Трябва да създам сортировака в определен диапазон , опитах се да създам нов клас и да му задам параметри но не се получи .Някой може ли да помогне
Дай това което си написал да го погледнем (аз или някой др от форума) и ще ти покажем как става.
import java.util.Random; import java.util.Scanner; import java.util.*; public class RangeControl { public static void main(String[] args) { for (int i = 0; i < 1; i++) { System.out.println(getRandomNumberInRange(1, 5000)); } } private static int getRandomNumberInRange(int min, int max) { if (min >= max) { throw new IllegalArgumentException("max must be greater than min"); } Random r = new Random(); return r.nextInt((max - min) + 1) + min; } public static class CyclicShiftApp extends RangeControl { public static void main(String[] args) { Scanner scan = new Scanner(System.in); ArrayList<Integer> empployee= new ArrayList<>(); while (scan.hasNextInt()) { empployee.add(scan.nextInt()); } } } }import java.io.Serializable; import java.util.stream.Collectors; public class Employee implements Comparable<Employee_Users> { private Object Employee_Users; private int id = 1; public String fname=null; public String lname=null ; public String dataOfResgeter; public int shoping=1; public float total=1; public String stars=null; public Employee(int id, String fname, String lname,String dataOfResgeter, int shopping,float total ) { this.id = id; this.fname = fname; this.lname = lname; this.dataOfResgeter = dataOfResgeter; this.shoping = shopping; this.total = total; if (total<99) this.stars="*"; else if (total<299) this.stars="**"; else if (total<499) this.stars="***"; else if (total<999) this.stars="****"; else this.stars="*****"; } public Employee() { } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getFname() { return fname; } public void setFname(String fname) { this.fname = fname; } public String getLname() { return lname; } public void setLname(String lname) { this.lname = lname; } public String getDataOfResgeter() { return dataOfResgeter; } public void setDataOfResgeter(String dataOfResgeter) { this.dataOfResgeter = dataOfResgeter; } public int getShoping() { return shoping; } public void setShoping(int shoping) { this.shoping = shoping; } public float getTotal() { return total; } public void setTotal(float total) { this.total = total; } /** * Compares this object with the specified object for order. Returns a * negative integer, zero, or a positive integer as this object is less * than, equal to, or greater than the specified object. * * <p>The implementor must ensure * {@code sgn(x.compareTo(y)) == -sgn(y.compareTo(x))} * for all {@code x} and {@code y}. (This * implies that {@code x.compareTo(y)} must throw an exception iff * {@code y.compareTo(x)} throws an exception.) * * <p>The implementor must also ensure that the relation is transitive: * {@code (x.compareTo(y) > 0 && y.compareTo(z) > 0)} implies * {@code x.compareTo(z) > 0}. * * <p>Finally, the implementor must ensure that {@code x.compareTo(y)==0} * implies that {@code sgn(x.compareTo(z)) == sgn(y.compareTo(z))}, for * all {@code z}. * * <p>It is strongly recommended, but <i>not</i> strictly required that * {@code (x.compareTo(y)==0) == (x.equals(y))}. Generally speaking, any * class that implements the {@code Comparable} interface and violates * this condition should clearly indicate this fact. The recommended * language is "Note: this class has a natural ordering that is * inconsistent with equals." * * <p>In the foregoing description, the notation * {@code sgn(}<i>expression</i>{@code )} designates the mathematical * <i>signum</i> function, which is defined to return one of {@code -1}, * {@code 0}, or {@code 1} according to whether the value of * <i>expression</i> is negative, zero, or positive, respectively. * * @return a negative integer, zero, or a positive integer as this object * is less than, equal to, or greater than the specified object. * @throws NullPointerException if the specified object is null * @throws ClassCastException if the specified object's type prevents it * from being compared to this object. */ @Override public String toString() { return fname +" "+lname+", "+shoping+", "+total+", "+dataOfResgeter+" "+stars+"\n"; } /** * Compares this object with the specified object for order. Returns a * negative integer, zero, or a positive integer as this object is less * than, equal to, or greater than the specified object. * * <p>The implementor must ensure * {@code sgn(x.compareTo(y)) == -sgn(y.compareTo(x))} * for all {@code x} and {@code y}. (This * implies that {@code x.compareTo(y)} must throw an exception iff * {@code y.compareTo(x)} throws an exception.) * * <p>The implementor must also ensure that the relation is transitive: * {@code (x.compareTo(y) > 0 && y.compareTo(z) > 0)} implies * {@code x.compareTo(z) > 0}. * * <p>Finally, the implementor must ensure that {@code x.compareTo(y)==0} * implies that {@code sgn(x.compareTo(z)) == sgn(y.compareTo(z))}, for * all {@code z}. * * <p>It is strongly recommended, but <i>not</i> strictly required that * {@code (x.compareTo(y)==0) == (x.equals(y))}. Generally speaking, any * class that implements the {@code Comparable} interface and violates * this condition should clearly indicate this fact. The recommended * language is "Note: this class has a natural ordering that is * inconsistent with equals." * * <p>In the foregoing description, the notation * {@code sgn(}<i>expression</i>{@code )} designates the mathematical * <i>signum</i> function, which is defined to return one of {@code -1}, * {@code 0}, or {@code 1} according to whether the value of * <i>expression</i> is negative, zero, or positive, respectively. * * @param o the object to be compared. * @return a negative integer, zero, or a positive integer as this object * is less than, equal to, or greater than the specified object. * @throws NullPointerException if the specified object is null * @throws ClassCastException if the specified object's type prevents it * from being compared to this object. */ @Override public int compareTo(Employee_Users o) { return this.id-o.getId(); } /** * Compares this object with the specified object for order. Returns a * negative integer, zero, or a positive integer as this object is less * than, equal to, or greater than the specified object. * * <p>The implementor must ensure * {@code sgn(x.compareTo(y)) == -sgn(y.compareTo(x))} * for all {@code x} and {@code y}. (This * implies that {@code x.compareTo(y)} must throw an exception iff * {@code y.compareTo(x)} throws an exception.) * * <p>The implementor must also ensure that the relation is transitive: * {@code (x.compareTo(y) > 0 && y.compareTo(z) > 0)} implies * {@code x.compareTo(z) > 0}. * * <p>Finally, the implementor must ensure that {@code x.compareTo(y)==0} * implies that {@code sgn(x.compareTo(z)) == sgn(y.compareTo(z))}, for * all {@code z}. * * <p>It is strongly recommended, but <i>not</i> strictly required that * {@code (x.compareTo(y)==0) == (x.equals(y))}. Generally speaking, any * class that implements the {@code Comparable} interface and violates * this condition should clearly indicate this fact. The recommended * language is "Note: this class has a natural ordering that is * inconsistent with equals." * * <p>In the foregoing description, the notation * {@code sgn(}<i>expression</i>{@code )} designates the mathematical * <i>signum</i> function, which is defined to return one of {@code -1}, * {@code 0}, or {@code 1} according to whether the value of * <i>expression</i> is negative, zero, or positive, respectively. * * @param o the object to be compared. * @return a negative integer, zero, or a positive integer as this object * is less than, equal to, or greater than the specified object. * @throws NullPointerException if the specified object is null * @throws ClassCastException if the specified object's type prevents it * from being compared to this object. */ }import javax.security.auth.Subject; import javax.swing.*; import java.awt.*; import java.nio.FloatBuffer; import java.util.*; import java.util.List; import java.util.stream.Collector; import java.util.stream.Collectors; import java.util.stream.IntStream; public class Employee_Users extends Employee { public Employee_Users(int id, String fname, String lname, String dataOfResgeter, int shopping, float total) { super(id, fname, lname, dataOfResgeter, shopping, total); } public static <IndexSorter> void main(String[] args) { List<Employee> employees = new ArrayList<Employee>(); Employee e1 = new Employee(101, "Ivan", "Ivanov", "12.02.2019", 4, 150); Employee e2 = new Employee(102, "Georgi", "Stoyanov", "17.07.2018", 4, 256); Employee e3 = new Employee(103, "Nikoleta", "Dimitrova", "05.04.2017", 6, 320); Employee e4 = new Employee(104, "Dayana", "Petrova", "11.05.2019", 2, 40); Employee e5 = new Employee(105, "Petiya ", "Ogniyanova", "20.06.2016", 30, 1600); Employee e6 = new Employee(106, "Adreyana", "Andreeva", "15.10.2015", 50, 3000); Employee e7 = new Employee(107, "Ivaiyo", "Petkov", "14.10.2017 ", 50, 2585); Employee e8 = new Employee(108, "Jivko", "Qnkov", "30.03.2017", 16, 4500); Employee e9 = new Employee(109, "Desilava", "Yordanova", "6.12.2014", 70, 3500); Employee e10 = new Employee(110, "Krasimira", "Evtimova", "18.01.2013", 40, 5990); Employee e11 = new Employee(111, "Gergana", "Ivanova", "12.05.2016", 15, 750); Employee e12 = new Employee(112, "Boris", "Kolev", "05.06.2017", 18, 453); Employee e13 = new Employee(113, "Nikolay", "Draganov", "01.09.2019", 2, 50); Employee e14 = new Employee(114, "Milen", "Filipov", "30.08.2018", 6, 360); Employee e15 = new Employee(115, "Jivka", "Kisiova", "04.12.2017", 60, 3798); Employee e16 = new Employee(116, "Ivan", "Hristov", "19.11.2016", 5, 120); Employee e17 = new Employee(117, "Atansa", "Kirov", "05.07.2018", 7, 456); Employee e18 = new Employee(118, "Marin", "Kolev", "07.09.2014", 10, 260); Employee e19 = new Employee(119, "Kiril", "Lazarov", "20.12.2018", 74, 2453); Employee e20 = new Employee(120, "Liliya", "Kovacheva", "04.03.2019", 9, 80); employees.add(e1); employees.add(e2); employees.add(e3); employees.add(e4); employees.add(e5); employees.add(e6); employees.add(e7); employees.add(e8); employees.add(e9); employees.add(e10); employees.add(e11); employees.add(e12); employees.add(e13); employees.add(e14); employees.add(e15); employees.add(e16); employees.add(e17); employees.add(e18); employees.add(e19); employees.add(e20); Collections.sort(employees, new FirstNameSort(employees, new LastNameSorter())); System.out.println(employees); employees.sort((Comparator.comparing(Employee::getTotal))); System.out.print(employees); } }import java.util.Comparator; import java.util.List; public class FirstNameSort implements Comparator<Employee> { public FirstNameSort(List<Employee> employees, LastNameSorter lastNameSorter) { } /** * Compares its two arguments for order. Returns a negative integer, * zero, or a positive integer as the first argument is less than, equal * to, or greater than the second.<p> * <p> * The implementor must ensure that {@code sgn(compare(x, y)) == * -sgn(compare(y, x))} for all {@code x} and {@code y}. (This * implies that {@code compare(x, y)} must throw an exception if and only * if {@code compare(y, x)} throws an exception.)<p> * <p> * The implementor must also ensure that the relation is transitive: * {@code ((compare(x, y)>0) && (compare(y, z)>0))} implies * {@code compare(x, z)>0}.<p> * <p> * Finally, the implementor must ensure that {@code compare(x, y)==0} * implies that {@code sgn(compare(x, z))==sgn(compare(y, z))} for all * {@code z}.<p> * <p> * It is generally the case, but <i>not</i> strictly required that * {@code (compare(x, y)==0) == (x.equals(y))}. Generally speaking, * any comparator that violates this condition should clearly indicate * this fact. The recommended language is "Note: this comparator * imposes orderings that are inconsistent with equals."<p> * <p> * In the foregoing description, the notation * {@code sgn(}<i>expression</i>{@code )} designates the mathematical * <i>signum</i> function, which is defined to return one of {@code -1}, * {@code 0}, or {@code 1} according to whether the value of * <i>expression</i> is negative, zero, or positive, respectively. * * @param o1 the first object to be compared. * @param o2 the second object to be compared. * @return a negative integer, zero, or a positive integer as the * first argument is less than, equal to, or greater than the * second. * @throws NullPointerException if an argument is null and this * comparator does not permit null arguments * @throws ClassCastException if the arguments' types prevent them from * being compared by this comparator. */ @Override public int compare(Employee o1, Employee o2) { return o1.getFname().compareTo(o2.getFname()); } }import java.util.Comparator; public class LastNameSorter implements Comparator<Employee>{ /** * Compares its two arguments for order. Returns a negative integer, * zero, or a positive integer as the first argument is less than, equal * to, or greater than the second.<p> * <p> * The implementor must ensure that {@code sgn(compare(x, y)) == * -sgn(compare(y, x))} for all {@code x} and {@code y}. (This * implies that {@code compare(x, y)} must throw an exception if and only * if {@code compare(y, x)} throws an exception.)<p> * <p> * The implementor must also ensure that the relation is transitive: * {@code ((compare(x, y)>0) && (compare(y, z)>0))} implies * {@code compare(x, z)>0}.<p> * <p> * Finally, the implementor must ensure that {@code compare(x, y)==0} * implies that {@code sgn(compare(x, z))==sgn(compare(y, z))} for all * {@code z}.<p> * <p> * It is generally the case, but <i>not</i> strictly required that * {@code (compare(x, y)==0) == (x.equals(y))}. Generally speaking, * any comparator that violates this condition should clearly indicate * this fact. The recommended language is "Note: this comparator * imposes orderings that are inconsistent with equals."<p> * <p> * In the foregoing description, the notation * {@code sgn(}<i>expression</i>{@code )} designates the mathematical * <i>signum</i> function, which is defined to return one of {@code -1}, * {@code 0}, or {@code 1} according to whether the value of * <i>expression</i> is negative, zero, or positive, respectively. * * @param o1 the first object to be compared. * @param o2 the second object to be compared. * @return a negative integer, zero, or a positive integer as the * first argument is less than, equal to, or greater than the * second. * @throws NullPointerException if an argument is null and this * comparator does not permit null arguments * @throws ClassCastException if the arguments' types prevent them from * being compared by this comparator. */ @Override public int compare(Employee o1, Employee o2) { return o1.getLname().compareTo(o2.getLname()); } }Резултат от това което съм направил:
"C:\Program Files\Java\jdk-13.0.1\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.3\lib\idea_rt.jar=49988:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Users\Bozhidar Kapudzhiev\AppData\Local\Temp\Proj-8\out\production\Proj-8" Employee_Users
[Adreyana Andreeva, 50, 3000.0, 15.10.2015 *****
, Atansa Kirov, 7, 456.0, 05.07.2018 ***
, Boris Kolev, 18, 453.0, 05.06.2017 ***
, Dayana Petrova, 2, 40.0, 11.05.2019 *
, Desilava Yordanova, 70, 3500.0, 6.12.2014 *****
, Georgi Stoyanov, 4, 256.0, 17.07.2018 **
, Gergana Ivanova, 15, 750.0, 12.05.2016 ****
, Ivaiyo Petkov, 50, 2585.0, 14.10.2017 *****
, Ivan Ivanov, 4, 150.0, 12.02.2019 **
, Ivan Hristov, 5, 120.0, 19.11.2016 **
, Jivka Kisiova, 60, 3798.0, 04.12.2017 *****
, Jivko Qnkov, 16, 4500.0, 30.03.2017 *****
, Kiril Lazarov, 74, 2453.0, 20.12.2018 *****
, Krasimira Evtimova, 40, 5990.0, 18.01.2013 *****
, Liliya Kovacheva, 9, 80.0, 04.03.2019 *
, Marin Kolev, 10, 260.0, 07.09.2014 **
, Milen Filipov, 6, 360.0, 30.08.2018 ***
, Nikolay Draganov, 2, 50.0, 01.09.2019 *
, Nikoleta Dimitrova, 6, 320.0, 05.04.2017 ***
, Petiya Ogniyanova, 30, 1600.0, 20.06.2016 *****
]
[Dayana Petrova, 2, 40.0, 11.05.2019 *
, Nikolay Draganov, 2, 50.0, 01.09.2019 *
, Liliya Kovacheva, 9, 80.0, 04.03.2019 *
, Ivan Hristov, 5, 120.0, 19.11.2016 **
, Ivan Ivanov, 4, 150.0, 12.02.2019 **
, Georgi Stoyanov, 4, 256.0, 17.07.2018 **
, Marin Kolev, 10, 260.0, 07.09.2014 **
, Nikoleta Dimitrova, 6, 320.0, 05.04.2017 ***
, Milen Filipov, 6, 360.0, 30.08.2018 ***
, Boris Kolev, 18, 453.0, 05.06.2017 ***
, Atansa Kirov, 7, 456.0, 05.07.2018 ***
, Gergana Ivanova, 15, 750.0, 12.05.2016 ****
, Petiya Ogniyanova, 30, 1600.0, 20.06.2016 *****
, Kiril Lazarov, 74, 2453.0, 20.12.2018 *****
, Ivaiyo Petkov, 50, 2585.0, 14.10.2017 *****
, Adreyana Andreeva, 50, 3000.0, 15.10.2015 *****
, Desilava Yordanova, 70, 3500.0, 6.12.2014 *****
, Jivka Kisiova, 60, 3798.0, 04.12.2017 *****
, Jivko Qnkov, 16, 4500.0, 30.03.2017 *****
, Krasimira Evtimova, 40, 5990.0, 18.01.2013 *****
]
Process finished with exit code 0
Трябва да изведа списък със рейтиг две (**)