-3

I want to search ExArrayList for a specific gpa? I want an answer of true or false if it exists in ArrayList.

 public class ExArrayList {
        private String Name;
        private double GPA;

        public ExArrayList(String name, double gpa) {
            this.Name = name;
            this.GPA = gpa;

        }
        public void setGPA(double gpa) {
            this.GPA = gpa;
        }

        public double getGPA() {
            return GPA;
        }
        public void setName(String name) {
            this.Name = name;
        }
        public String getName() {
            return Name;
        }
        @Override
        public String toString() {
            return String.format("%s\t%f", this.Name, this.GPA);
        }
    }

    public class Main {
        public static void main(String[] args) {

            ArrayList<ExArrayList> psy101 = new ArrayList<>();
            psy101.add(new ExArrayList("Bob", 2.9 ));
            psy101.add(new ExArrayList("Steve", 3.9 ));
            psy101.add(new ExArrayList("Charles", 4.0 ));

            System.out.println();
            System.out.printf("Student\tGPA\n");
            for(ExArrayList s : psy101) {
                System.out.printf("%s\n", s);

            }

            boolean binFound = psy101.contains(2.9); // This is what I am using to search the ArrayList.  It's not working.
            System.out.println("Does the list contain GPA of 2.9? " +     binFound);`
Luis Lavieri
  • 4,064
  • 6
  • 39
  • 69
datorre
  • 5
  • 3
  • 2
    Sort and then binary search – Luis Lavieri Oct 27 '16 at 17:22
  • You need to overwrite [`boolean equals(Object o)`](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object-) as well as [`int hashCode()`](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode--) if you want to use [`int indexOf(Object o)` from `ArrayList`](http://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#indexOf-java.lang.Object-). – Turing85 Oct 27 '16 at 17:26

4 Answers4

4

You could steam the list and use a lambda to look for matches:

boolean binFound = psy101.stream().anyMatch(g -> g.getGPA() == 2.9);
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • @bradimus Your example does an unnecessary auto-box and your `anyMatch()` argument is not a valid lambda expression. – Andreas Oct 27 '16 at 17:34
0

You have to do a compare in the for each loop.

    if(s.getGPA == 2.9){
        binFound = true;
        break;
    }
Miftis
  • 75
  • 1
  • 5
  • I should add that I am new to programming and any comments/insights are appreciated. – datorre Oct 27 '16 at 17:29
  • 1
    That's not the right way to compare doubles in `Java`. See [this](http://stackoverflow.com/questions/179427/how-to-resolve-a-java-rounding-double-issue) and [this](http://stackoverflow.com/questions/8081827/how-to-compare-two-double-values-in-java) – Luis Lavieri Oct 27 '16 at 17:29
  • 1
    This code does not compile. `s.getGPA` is not a field, it is a method. – Turing85 Oct 27 '16 at 17:31
0

Without Java8 Streams:

boolean binFound = false;
for(ExArrayList exArrayList : psy101) {
      if(exArrayList.getGPA() == 2.9) {
            binFound = true;
            break;
        }
 }
 System.out.println(binFound);

With Java8 Streams:

boolean binFound = psy101.stream().map(ExArrayList::getGPA).
anyMatch(gpa -> gpa == 2.9);
System.out.println(binFound);
Vasu
  • 21,832
  • 11
  • 51
  • 67
-1

I did not like any of these answers. People forgot about the precision errors that occur when trying to compare doubles. I know it should not be too bad for GPA purposes because there isn't a lot of precision involved. However, there is the OK way or the right way of doing things. See this and this for clarification.

The right way of doing what you are planning is with binary search. But, first the list need to be sorted for it to work.

How to sort Objects in Java? You need to know first how to compare their values and there are a couple of ways of doing this. For my example, I will use Comparable. Check this for learning purposes.

Use this import before anything

import java.util.*;

Now, we implement Comparable in your object. It will look like this after the changes:

public class ExArrayList implements Comparable<ExArrayList> {
    private String Name;
    private double GPA;

    public ExArrayList(String name, double gpa) {
        this.Name = name;
        this.GPA = gpa;

    }
    public void setGPA(double gpa) {
        this.GPA = gpa;
    }

    public double getGPA() {
        return GPA;
    }
    public void setName(String name) {
        this.Name = name;
    }
    public String getName() {
        return Name;
    }
    @Override
    public String toString() {
        return String.format("%s\t%f", this.Name, this.GPA);
    }
    //new stuff needed to compare the objects
    public int compareTo(ExArrayList other) {                 
        return Double.compare(this.GPA, other.GPA);        
    } 
 }

Now we can sort your list by doing:

Collections.sort(psy101);

After sorting it, we can search the index of the object by doing:

//here we must pass a fake object with the value that we are trying to find
int index = Collections.binarySearch(psy101, new ExArrayList(null, 2.9));

System.out.println(index >= 0 ? "True" : "False");

index contains the position of 2.9 in the list which is 0, if not found it will contain a negative number.

Community
  • 1
  • 1
Luis Lavieri
  • 4,064
  • 6
  • 39
  • 69