Sorting ArrayList by specific value - java

Is there a way I can sort my teams arraylist by the number of points that team has, so whichever team has the highest points is first, and so on, please dont use comperator, because I don't understand how to use that, here is my code:
import java.util.*;
import java.io.*;
class team {
public int teamNum;
public int points = 0;
public team(int x) {
this.teamNum = x;
}
public team(int x,int y) {
this.teamNum = x;
this.points = y;
}
}
public class Problem9 {
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(new File("Test9.txt"));
ArrayList<team> teams = new ArrayList<>();
int counter=0;
while(in.hasNextLine()) {
boolean found = false;
String[] split = in.nextLine().split(" ");
int n1 = Integer.parseInt(split[0]);
int n2 = Integer.parseInt(split[1]);
if (!(n1 == 0 && n2 == 0)) {
if (counter<1) teams.add(new team(n1));
for (int i=0; i<teams.size(); i++) {
if (teams.get(i).teamNum == n1) {
teams.get(i).points+=n2;
found = true;
}
}
if (!found) {
teams.add(new team(n1, n2));
}
}
counter++;
}
for (int k=0; k<teams.size(); k++)
System.out.println(teams.get(k).teamNum + " " + teams.get(k).points);
}
}

There are 2 ways to do sorting on a custom data type :
use Comparator
use Comparable
Using Comparable
class team implements Comparable<team> {
public int teamNum;
public int points = 0;
public team(int x) {
this.teamNum = x;
}
public team(int x,int y) {
this.teamNum = x;
this.points = y;
}
public int compareTo(team t1){
return t1.points - this.points;
}
}
Now use Collections.sort() on the arraylist, which would sort it for you.

Agreed with #sschale, you can't run away from certain methods or libraries just because you do not know how to use it and hence do not want to use it. To help you out I will give you a much simplified form of comparator implementation here:
//Place this between class team and public class Problem9
static class rankcomparator implements Comparator<Team> {
#Override //need to override
public int compare(Team lhs, Team rhs) {
return -compare(lhs.points, rhs.points); //if descending order
//return compare(lhs.points, rhs.points); //if ascending order
}
}
public static void main(String[] args) throws IOException {
//Add this code inside public class Problem9 ...
Collections.sort(teams, new rankcomparator());
}

You really need to learn how a Comparator and the Comparable interface work. I recommend this tutorial on Comparator usage and this tutorial on using natural ordering through the Comparable interface.
Concerning the issue at hand: just add a getScore method to your Team class:
int getScore(){ return this.score;}
Then call:
teams.sort(Comparator.comparing(Team::getScore));
You can reverse the order using:
teams.sort(Comparator.comparing(Team::getScore).reversed());
One last thing: it is convention that class names begin with a capital letter. You should refactor your team class to be Team.

You can also just do that without a comparator because you just have Integers to compare to each other:
teams.sort((Team t1, Team t2) -> (Integer.compare(t1.getScore(), t2.getScore())));
Collections.reverse(teams);

Related

How can I use a java interface to calculate the union, intersection, and difference of two sets, all in a default method in the interface?

I am new to java so sorry if this is super basic. I need to make default methods in an interface ISet to allow for the calculation of the union, intersection, and difference of 2 sets created in a class that implements this interface. I know how to calculate all of these but I have no idea how to syntactically do it in the interface since it uses interface objects rather than the data structures in the implementing classes.
The Code:
public interface ISet {
void add(Integer val);
Integer removePos(int pos);
boolean contains(Integer val);
int getSize();
default void union(ISet unionWith) {
//List<Integer>unionSet = new ArrayList<>();
//unionSet.add(unionWith);
//List set = new ArrayList();
//set.addAll(0, unionWith);
//ISet unionSet = this;
// To call in main: this.union(unionWith)
// For my variables it would be mySet.union(mySet2)
//this.getSize();
int size1 = this.getSize();
int size2 = unionWith.getSize();
if(this == unionWith) {
}
//List<Integer> unionList = Arrays.asList(this);
ISet allVals = this.add(Integer val);
for(int i = 0; i < size1 + size2; i++) {
if(!this.contains(Integer.unionWith));
}
}
default void intersect(ISet intWith) {
}
default void difference(ISet diffWith) {
}
}
Sorry for the graveyard I'm just trying every thing I can. Pretty much none of this works except for the size stuff. I know that I'm supposed to use all of the other methods above but I have no idea how to use them in this scenario.
I usually try and figure out things like this by myself but after 3 hours of not being able to union two sets I've realized that I just need to get more informed on how java and interfaces work.
I'll really appreciate any help you guys have to offer!
Maybe you need a methed : Integer get(int pos);
public interface ISet {
void add(Integer val);
Integer get(int pos);
Integer removePos(int pos);
boolean contains(Integer val);
int getSize();
default void union(ISet unionWith) {
for (int i = 0; i < unionWith.getSize(); i++) {
this.add(unionWith.get(i));
}
}
default void intersect(ISet intWith) {
for (int i = getSize() - 1; i >= 0; i--) {
Integer value = get(i);
if (!intWith.contains(value)) {
removePos(i);
}
}
}
default void difference(ISet diffWith) {
for (int i = getSize() - 1; i >= 0; i--) {
Integer value = get(i);
if (diffWith.contains(value)) {
removePos(i);
}
}
}
}

Passing Multiple Generics Into Java Method

Using Java. The goal is to search for a value, given as a generic, in an ArrayList, also given as a generic.
My Student class (pertinent parts)
public class Student<T> implements Comparable
{
private String studName;
private Integer gradeAverage;
public Student(String nameIn, int gradeIn)
{
studName = nameIn;
gradeAverage = gradeIn;
}
public int compareTo(Object obj)
{
Student s1 = (Student)obj;
return(this.gradeAverage - s1.gradeAverage);
}
}
My Search; thinking there may be a problem with the generic specifications
public class SearchMethods<T,S>
{
public <T extends Comparable, S extends Comparable> void BinarySearch(T[] inputArray, S searchValue)
{
boolean found = false;
for(int i = 0; i < inputArray.length; i++)
{
T search = inputArray[i];
if(searchValue.compareTo(search) == 0)
{
System.out.println(searchValue + " is at index " + i);
found = true;
}
}
if(found == false)
{
System.out.println(searchValue + " was not found");
}
}
}
And my main()
public static void main(String[] args)
{
Student studentOne = new Student("James",92);
Student studentTwo = new Student("Mary",95);
Student studentThree = new Student("Bobbie",82);
Student studentFour = new Student("Emily",100);
Student studentFive = new Student("Joey",88);
ArrayList<Student> studentList = new ArrayList<Student>();
studentList.add(studentOne);
studentList.add(studentTwo);
studentList.add(studentThree);
studentList.add(studentFour);
studentList.add(studentFive);
SearchMethods<ArrayList, Student> searchMethods = new SearchMethods<ArrayList, Student>();
searchMethods.BinarySearch(studentList, studentOne); //Should print that it was found at index 0
The given compiler error states that an argument mismatch, that ArrayList cannot be converted to T#1[]. But that's the whole point of generics, right?? Interestingly, no analogous error is given for the second type, but maybe the compiler just hasn't read ahead that far.
I'm pretty sure my syntax is OK at the class level, so the error is likely with the calling objects in main(). Though, I could be wrong.
Thanks in advance!
You need to convert arraylist to an array. Check the argument for Binary Search.
Try this:
SearchMethods<ArrayList, Student> searchMethods = new SearchMethods<ArrayList, Student>();
searchMethods.BinarySearch(studentList.toArray(new Student[studentList.size()]), studentOne);
You could also change BinarySearch where you work with an arraylist.
While this is not part of the question, it is important not compute the difference for compareTo or you will get overflow error.
Try this:
class Student<T> implements Comparable
{
private String studName;
private Integer gradeAverage;
public Student(String nameIn, int gradeIn)
{
studName = nameIn;
gradeAverage = gradeIn;
}
public int compareTo(Object obj)
{
Student s1 = (Student)obj;
if (this.gradeAverage < s1.gradeAverage){
return -1;
}
if(this.gradeAverage == s1.gradeAverage){
return 0;
}
return 1;
}
#Override
public String toString(){
return "student name="+studName +" grade average= " + gradeAverage;
}
}

Method undefined for type method?

I have a program to find pythagorean triples. in it, i have an object that needs to be used to call methods. Said object is broken. Errors are " The method Triples(int) is undefined for the type Triples" and "The method greatesCommonFactor() is undefined for the type Triples" mind you, not everything in Triples does useful stuff atm. It isn't completely finished yet.
public class TriplesRunner
{
public static void main(String args[])
{
int number;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the natural number :: ");
number=keyboard.nextInt();
Triples test = new Triples();
test.Triples(number);
test.greatestCommonFactor(number);
System.out.println(test.toString());
}
}
public class Triples
{
public int number;
public Triples(int num)
{
setNum(number);
}
public void setNum(int num)
{
int a = 0;
int b = 0;
int c = 0;
}
public int greatestCommonFactor(int a, int b, int c)
{
int max = 0;
for(a=1; a<=number-2; a++)
{
for(b=a+1; b<=number-1; b++)
{
for(c=b+1; c<=number; c++)
{
if(a*a + b*b == c*c);
}
}
}
return 1;
}
public String toString()
{
String output="";
output+="a + b + c";
return output+"\n";
}
}
you are trying to call the constructor as a method,
Change this part:
Triples test = new Triples();
test.Triples(number);
to
Triples.test = new Triples(number);
Triples isn't a method - it's your constructor, meaning it's invoked with the new operator:
Triples test = new Triples(number);
greatestCommonFactor is not defined properly. It currently takes three int arguments, instead of taking none and using Triples' data members:
public int greatestCommonFactor()

Sort a list that contains a custom class

so I'm currently doing an exercise for college that has several optional parts (because we havn't done this in class yet), one of them being to use lists instead of arrays (so it'd be variable size) and another one printing the list sorted by points (I'll get to that now)
So, I have the Player.java class which looks like this.
public class Player {
String name;
String password;
int chips;
int points;
public Player(String n, String pw, int c, int p) {
name = n;
password = pw;
chips = c;
points = p;
}
public String getName() {
return name;
}
public void setName(String n) {
name = n;
}
public void setPW(String pw) {
password = pw;
}
public String getPW() {
return password;
}
public void setChips(int c) {
chips = c;
}
public int getChips() {
return chips;
}
public void setPoints(int p) {
points = p;
}
public int getPoints() {
return points;
}
}
Pretty simple, then I'm creating a List with this (in another class):
List<Player> lplayer = new ArrayList<Player>();
Adding players with this:
lplayer.add(new Player(n,pw,c,p))`
And finally reading their stats with this:
public int search_Player (String n) {
String name;
int i = 0;
boolean found = false;
while ((i <= tp) && (!found)) {
name = lplayer.get(i).getName();
if (name.equals(n)) {
found = true;
}
i++;
}
return (found == true) ? i-1 : -1;
}
public Player show_Player (int i) {
return lplayer.get(i);
}
public void list_Players() {
Collections.sort(lplayer);
int i2;
if (tp > 0) { // variable which contains number of total players
for (int i = 0;i<tp;i++) {
i2 = i+1;
System.out.println ("\n"+i2+". "+lplayer.get(i).getName()+" [CHIPS: "+lplayer.get(i).getChips()+" - POINTS: "+lplayer.get(i).getPoints()+"]");
}
}
else {
System.out.println ("There are no players yet.");
}
}
So that's basically all the code. As you can see the I already have a list_Players function but that just prints it in the order it was added. I need a way to print in sorted by the points each player has (so basically a ranking).
As you can see I'm pretty new to java so please try not to come up with a very complicated way of doing it.
I've already searched for it and found things like Collections.sort(list) but I guess that's not what I need right here.
Thank you!
You can use the public static <T> void sort(List<T> list, Comparator<? super T> c) overload in Collections - provide the comparator you need (can be just an anonymous class) - and you are all set!
EDIT:
This describes how the method works. In brief, you'll implement your call as
Collections.sort(list, new Comparator<Player>() {
int compare(Player left, Player right) {
return left.getPoints() - right.getPoints(); // The order depends on the direction of sorting.
}
});
That's it!
Collections.sort(list) could definitely by a solution for your problem. It's a way to sort your collections provided by Java. If you are writing a "real world" application (not an exercise for collage) this would be the way you doing it.
To let Collections.sort(list) works, you have to implement an interface call Comparaple. By implementing this interface, the sort will know how to order your elements.
But because it's a exercise for collage, this is perhaps a little bit to easy. If you want (or must) implement you own sorting algorithm, try first to sort a common list of numbers (1, 5, 2, 7...). You can extend such an sorting algorithm easily for your own classes.
A new approach using lambdas, that is a lot shorter to write is
myList.sort((obj1, obj2)->(condition)?1:-1);
where you can use the objects for your condition, and anything greater than 0 returned means swap (in this case if condition returns true)

Sorting 2D array of strings in Java

I know that this question might have been asked before, but I was not able to find a fit answer. So say I have this array:
String[][] theArray = {
{"james", "30.0"},
{"joyce", "35.0"},
{"frank", "3.0"},
{"zach", "34.0"}};
Is there a way to descendingly sort this array by the second element of each sub-element. So I would get something like this.
theArray = {
{"joyce", "35.0"},
{"zach", "34.0"},
{"james", "30.0"},
{"frank", "3.0"}};
Use Arrays.sort(arr, comparator) with a custom comparator:
Arrays.sort(theArray, new Comparator<String[]>(){
#Override
public int compare(final String[] first, final String[] second){
// here you should usually check that first and second
// a) are not null and b) have at least two items
// updated after comments: comparing Double, not Strings
// makes more sense, thanks Bart Kiers
return Double.valueOf(second[1]).compareTo(
Double.valueOf(first[1])
);
}
});
System.out.println(Arrays.deepToString(theArray));
Output:
[[joyce, 35.0], [zach, 34.0], [james, 30.0], [frank, 23.0]]
Beware:
you will be sorting the array you passed in, Arrays.sort() will not return a new array (in fact it returns void). If you want a sorted copy, do this:
String[][] theCopy = Arrays.copyOf(theArray, theArray.length);
And perform the sorting on theCopy, not theArray.
You must use the Arrays.sort() method. This method takes a Comparator as argument. The sort method delegates to the comparator to determine if one element of the array must be considered bigger, smaller or equal to another element. Since every element of the outer array is an array, the comparator will have to compare arrays (of Strings).
The arrays must be compared based on the value of their second element. This second element is a String which in fact represents a double number. So you'll have to transorm the strings into numbers, else the order will be lexicographical (20 come before 3) rather than numerical.
The comparator could thus look like this :
public class StrinArrayComparator implements Comparator<String[]> {
#Override
public int compare(String[] array1, String[] array2) {
// get the second element of each array, andtransform it into a Double
Double d1 = Double.valueOf(array1.[1]);
Double d2 = Double.valueOf(array2.[1]);
// since you want a descending order, you need to negate the
// comparison of the double
return -d1.compareTo(d2);
// or : return d2.compareTo(d1);
}
}
If you want to move away from arrays, here's a variation that uses List<Record> and a RecordComparator that implements Comparator<Record>.
Console:
joyce 35.0
zach 34.0
james 30.0
frank 23.0
Code:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/** #see http://stackoverflow.com/questions/5064027 */
public class ComparatorTest {
public static void main(String[] args) {
List<Record> list = new ArrayList<Record>(Arrays.asList(
new Record("james", "30.0"),
new Record("joyce", "35.0"),
new Record("frank", "23.0"),
new Record("zach", "34.0")));
print(list, Sort.DESCENDING, Field.D);
}
private static void print(List<Record> list, Sort s, Field f) {
RecordComparator rc = new RecordComparator(s, f);
Collections.sort(list, rc);
for (Record r : list) {
System.out.println(r);
}
}
}
class Record {
private String s;
private Double d;
public Record(String name, String number) {
this.s = name;
this.d = Double.valueOf(number);
}
#Override
public String toString() {
return s + " " + d;
}
public int compareTo(Field field, Record record) {
switch (field) {
case S: return this.s.compareTo(record.s);
case D: return this.d.compareTo(record.d);
default: throw new IllegalArgumentException(
"Unable to sort Records by " + field.getType());
}
}
}
enum Sort { ASCENDING, DESCENDING; }
enum Field {
S(String.class), D(Double.class);
private Class type;
Field(Class<? extends Comparable> type) {
this.type = type;
}
public Class getType() {
return type;
}
}
class RecordComparator implements Comparator<Record> {
private Field field;
private Sort sort;
public RecordComparator(Sort sort, Field field) {
this.sort = sort;
this.field = field;
}
#Override
public final int compare(Record a, Record b) {
int result = a.compareTo(field, b);
if (sort == Sort.ASCENDING) return result;
else return -result;
}
}
You seem to be living in object denial. Those inner arrays look a lot like information about a Person (with the name and some value, maybe a score).
What you'd want to do is to write a custom class to hold that information:
public class Person {
private final String name;
private final double score;
public Person(final String name, final double score) {
this.name=name;
this.score=score;
}
public String getName() {
return name;
}
public double getScore() {
return score;
}
}
Then, when you want to sort them, you simply implement a Comparator<Person> that specifies how you want them sorted:
public PersonScoreComparator implements Comparator<Person> {
public int compare(Person p1, Person p2) {
return Double.compare(p1.getScore(), p2.getScore());
}
}
Alternatively, you could have the Person class itself implement Comparable<Person> by adding this method:
public int compareTo(Person other) {
return Double.compare(getScore(), other.getScore());
}
-Create list out of this array using Arrays.toList()
-Design comparator using java.lang.comparator and write logic for sorting every even elements
There are several sort methods in java.util.Arrays. Two of them take custom Comparators. Simply provide a comparator comparing the second element of the inner arrays.
public static void main(String[] args)
{
String Name[][]={{"prakash","kumar"},{"raj","kappor"},{"vinod","bhart"}};
String str[]=new String[2];
for(int j=0; j<Name.length;j++)
{
for (int i=0 ; i<2; i++)
{
str[i]=Name[j][i];
}
for(int i=0;i<str.length;i++)
{
for(int k=i+1;k<str.length;k++)
{
if(str[i].compareTo(str[k])>0)
{
String temp= str[i];
str[i]=str[k];
str[k]=temp;
}
}
System.out.print(str[i]+ " ");
}
System.out.println();
}
}
}
/**
*
* #param array - 2D array required to be arranged by certain column
* #param columnIndex - starts from 0; this will be the main comparator
* #param hasHeaders - true/false; true - ignore the first row. False -
* first row it's also compared and arranged
* #return - the new arranged array
*/
private String[][] arrangeArray(String[][] array, int columnIndex, boolean hasHeaders) {
int headersExists = 0;
if (hasHeaders) {
headersExists = 1;
}
for (int i = headersExists; i < array.length; i++) {
for (int j = headersExists; j < array.length; j++) {
if (array[i][columnIndex].compareTo(array[j][columnIndex]) < 0){
String[] temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
return array;
}

Categories

Resources