package generics;
import java.util.ArrayList;
import java.util.List;
public class Generics {
private static List <Box> newlist = new ArrayList<>();
public static void main(String[] args) {
newlist.add(new Box("charlie",30));
newlist.add(new Box("max",29));
newlist.add(new Box("john",22));
// Testing method find -- Start
find ("max",29);
//Testing method find2 -- Start
Box <String,Integer> search = new Box("max",29);
find2(search);
}
public static void find (String parameter, Integer parameter1){
for (Box e : newlist){
if(e.getName() != null && e.getMoney() !=null
&& e.getName().equals(parameter)
&& e.getMoney().equals(parameter1)){
System.out.println("found on position " + newlist.indexOf(e));
break;
}
}
}
public static void find2 (Box e){
for (Box a : newlist){
if (a.equals(e)){
System.out.println("Found");
}else {
System.out.println("Not found");
}
}
}
}
public class Box<T , D>{
private T name;
private D money;
public Box(T name, D money) {
this.name = name;
this.money = money;
}
public T getName() {
return name;
}
public D getMoney() {
return money;
}
#Override
public String toString() {
return name + " " + money;
}
}
Can someone show me how to search for an object in ArrayList.
Method find() it works perfect but in my opinion is wrong and
the reason why I am thinking like that, because I am passing as parameter a string and an integer but should be an box object or maybe I wrong?
In my second method find2() I am trying to pass as parameter an object of Box and when I am trying to search for it I got a false result =(
I am noobie I am trying to understand and to learn.
Stop using raw types!
Box is generic, so if you are not targeting older Java versions, always add generic parameters!.
The declaration of find2 should be like this:
public static void find2 (Box<String, Integer> e)
And you should check whether two boxes are equal in exactly the way you did in find. equals will not work because you did not define an equals method in Box. So:
for (Box<String, Integer> a : newlist){
if (a.getName().equals(e.getName()) &&
a.getMoney().equals(e.getMoney())){
System.out.println("Found");
}else {
System.out.println("Not found");
}
}
You should override Object.equals() on the Box class.
Try to handle null correctly too. Because 2 Box with null names and/or null money are in fact equal.
(you DON'T need to override Object.hashCode() for this, but it's a good practice to do so, just in case it is used in a hashmap or hashset or such).
The easiest way to search and find something in an arraylist is to use the .equals method combined with a for loop to iterate through your lists.
for(int i = 0; i < newList; ++i)
{
if(newlist.equals(Stringname))
{
//it matches so do something in here
}
}
what it is doing here is moving through the list 1 by 1 until it finds something that matches what you entered -> stringName
Related
I have a collection of Java objects where I want to run a single function across multiple values I might find in some of the object's member variables. I'm looking for a nice way to pass in which getter should be used so I can have one method do all that work. I was thinking about something like a Supplier, but that would mean I have to have one per instance of the class. Here's an example of what I'm trying to do (only I would like to do this without the if statement or with potentially n getters a switch statement:
import java.util.ArrayList;
import java.util.List;
public class TestSupplier {
private int varA;
private int varB;
public TestSupplier(int varA, int varB) {
this.varA = varA;
this.varB = varB;
}
public int getA() {
return this.varA;
}
public int getB() {
return this.varB;
}
public static void main(String[] args) {
List<TestSupplier> testList = new ArrayList<>();
testList.add(new TestSupplier(1, 11));
testList.add(new TestSupplier(2, 22));
// Can I pass something like a generic supplier instead of a bool?
TestSupplier.someCollectorFunction(testList, true);
TestSupplier.someCollectorFunction(testList, false);
}
public static void someCollectorFunction(List<TestSupplier> list, boolean isA /* what if I want more than one getter*/) {
int sum = 0;
for (TestSupplier obj: list) {
// This is where I wish I could have a generic supplier or something
if (isA) {
sum = sum + obj.getA();
}
else {
sum = sum + obj.getB();
}
}
System.out.println("I have a sum: " + sum);
}
}
Is there something is Java's functional API that would let me do this?
It sounds like what you want is
ToIntFunction<TestSupplier> fn = isA ? TestSupplier::getA : TestSupplier::getB;
for (TestSupplier obj: list) {
sum += fn.applyAsInt(obj);
}
It's up to you whether you consider that an improvement.
You could also pass in the ToIntFunction instead of the boolean, passing in TestSupplier::getA instead of true etc.
I have a method that will return a list of objects of type MyClass. MyClass has many properties, but I care about type and count. I want to write a test that asserts that the returned list contains at least one element that matches a certain condition. For instance, I want at least one element in the list of type "Foo" and count 1.
I'm trying to figure out how to do this without literally looping over the returned list and checking each element individually, breaking if I find one that passes, like:
boolean passes = false;
for (MyClass obj:objects){
if (obj.getName() == "Foo" && obj.getCount() == 1){
passes = true;
}
}
assertTrue(passes);
I really don't like this structure. I'm wondering if there's a better way to do it using assertThat and some Matcher.
with hamcrest imports
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
you can test with
assertThat(foos, hasItem(allOf(
hasProperty("name", is("foo")),
hasProperty("count", is(1))
)));
assertTrue(objects.stream().anyMatch(obj ->
obj.getName() == "Foo" && obj.getCount() == 1
));
Or more likely:
assertTrue(objects.stream().anyMatch(obj ->
obj.getName().equals("Foo") && obj.getCount() == 1
));
I don't know if it's worth using Hamcrest for this but it's good to know it's out there.
public class TestClass {
String name;
int count;
public TestClass(String name, int count) {
this.name = name;
this.count = count;
}
public String getName() {
return name;
}
public int getCount() {
return count;
}
}
#org.junit.Test
public void testApp() {
List<TestClass> moo = new ArrayList<>();
moo.add(new TestClass("test", 1));
moo.add(new TestClass("test2", 2));
MatcherAssert.assertThat(moo,
Matchers.hasItem(Matchers.both(Matchers.<TestClass>hasProperty("name", Matchers.is("test")))
.and(Matchers.<TestClass>hasProperty("count", Matchers.is(1)))));
}
Basically, I have 2 classes. One of them has a private member ArrayList(Objects from the other class) and every object from the list has a private field points. I have a method to iterate through the list and get the sum of all points. So I just want to compare list1 > list2 by their summed points. But I'm failing to achieve that - my compareTo() returns always 0.
Here is a short code example of this.
public class StudentsGroup implements IFile, Comparable {
private List<Student> studentsList = new ArrayList<Student>();
public int compareTo(Object o) {
if(StudentsGroup.getTotalPoints(studentsList) < ((StudentsGroup)o).getTotalPoints(studentsList))
return 1;
else if(StudentsGroup.getTotalPoints(studentsList) > ((StudentsGroup)o).getTotalPoints(studentsList))
return -1;
else
return 0;
}
public static int getTotalPoints(List<Student> studentsList1) {
int totalPoints = 0;
for(Student o : studentsList1) {
totalPoints += o.getStudentPoints();
}
return totalPoints;
}
}
The method
if(
StudentsGroup.getTotalPoints(studentsList) <
((StudentsGroup)o).getTotalPoints(studentsList))
You are passing the same studentsList to both sides of the calculation.
The "other group" o is not used at all.
It may look like o is used, but getTotalPoints is a static method and it does not matter what instance you call it on. The compiler will give you a warning about this, too. Do not ignore compiler warnings.
Immediate fix would be to change the code to
if( getTotalPoints(studentsList) < getTotalPoints((StudentsGroup)o).studentsList)
But you should probably change that getTotalPoints method from public static to public (not-static). Instead of the list being passed as a parameter, it can then just use this.studentsList internally.
if (this.getTotalPoints() < ((StudentsGroup)o).getTotalPoints())
In that case I would check the values are not both the same (or both 0)
public class StudentsGroup implements IFile, Comparable<StudentsGroup> {
private List<Student> studentsList = new ArrayList<Student>();
public int compareTo(StudentsGroup sg) {
return Integer.compare(getTotalPoints(), sg.getTotalPoints());
}
public int getTotalPoints() {
return Math.toIntExact(studentsList.stream()
.mapToInt(Student::getStudentPoints).sum());
}
}
By simplifying the code you are less likely to mix up a static method with an instance method (StudentsGroup)o).getTotalPoints(studentsList) just calls StudentsGroup.getTotalPoints(studentsList) as you don't have an instance method.
EDIT: Added some information.
I got an array of Objects. Each object has a name and a value. I need to sort the objects in descending order of those values, and print the names. I saw this easy solution but can't seem to apply it to my problem: http://www.mkyong.com/java/java-object-sorting-example-comparable-and-comparator/
The code compiles with no error but the array is not sorted at all. I know this because I know what the output should be i.e. the output should be something like var364, var200, var65 etc. and what i get is var1, var2, var3 etc.
I tried to strip the code of the irrelevant parts here:
Main class
print(data.preselection());
private void print (UnitRow preselectedUnitRow) {
out.printf("Variables after preselection: \n");
for (int i=0;i<PRESELECTION_LIMIT;i++) {
out.printf("%s, ",preselectedUnitRow.getUnitName(i));
}
}
Dataset (data)
private UnitRow data;
...
public UnitRow preselection() {
UnitRow standardDeviationUnits = new UnitRow(numberOfVariables);
for (int i=0;i<numberOfVariables;i++){
Unit unit = new Unit(1,variableNames[i],calculateStandardDeviation(i));
standardDeviationUnits.add(unit);
}
standardDeviationUnits.sort();
return standardDeviationUnits;
}
UnitRow
import java.util.Arrays;
public class UnitRow {
private Unit[] units;
private int count;
...
public void sort() {
Arrays.sort(units);
}
}
Unit
public class Unit implements Comparable<Unit>{
private NumberRow elements; //just a class with an array of doubles
private String name;
...
#Override
public int compareTo(Unit compareUnit) { //getValue returns a single type double number
int comparison = (int) (compareUnit.getValue(0) - getValue(0));
return comparison;
}
}
I am assuming my implementation of Comparable is wrong. Can you spot the problem?
I say this because I tested as following:
System.out.println(standardDeviationUnits.getValue(0,0));
standardDeviationUnits.sort();
System.out.println(standardDeviationUnits.getValue(0,0));
And the exact same value is returned.
It looks like reverse order
public int compareTo(Unit compareUnit) {
if (getValue(0) < compareUnit.getValue(0)) return 1;
else if (getValue(0) > compareUnit.getValue(0)) return -1;
return 0;
}
Try this.
Also note that in your compareTo, you unnecessarily wrote return 2; and wrote 3 if instead of 1 if-else.
If you are trying to sort as per names:
return compareUnit.name.compareTo(name);
else I have no idea what attribute your getValue(0) returns to you but still if you are trying to sort as per getValue(0):
return compareUnit.getValue(0)-getValue(0);
I have an array of phonebook elements and I'm trying to sort them according to lexicographical order using comparable sort. But it all messes up. Please help. Thanks in advance
It will also help to describe about sort function in java a little bit!
Here is the code:
package myphonebook;
import javax.swing.*;
import java.util.Arrays;
import java.util.Comparator;
public class MyPhoneBook implements Comparator<MyPhoneBook>{
private String name,email,number;
MyPhoneBook()
{
}
public void input()
{
name = (JOptionPane.showInputDialog("Enter Name:\n")).toString();
number = JOptionPane.showInputDialog("Enter Number:\n").toString();
email = JOptionPane.showInputDialog("Enter email:\n").toString();
}
public void print()
{
JOptionPane.showMessageDialog(null, "Name: " + name+ "\n" + "Phone: " +number +"\n" + "Email: " + email);
}
public static void main(String[] args) {
MyPhoneBook a[] = new MyPhoneBook[300];
MyPhoneBook b = new MyPhoneBook();
//final Integer[] sorted = ArrayUtils.toObject(MyPhoneBook);
int i,n;
n = Integer.parseInt(JOptionPane.showInputDi… total number:\n"));
for(i=0;i<n;i++)
{
a[i] = new MyPhoneBook();
a[i].input();
}
Arrays.sort(a);
for(i=0;i<n;i++)
{
a[i].print();
}
}
#Override
public int compare(MyPhoneBook o1, MyPhoneBook o2) {
return o1.number.compareTo(o2.number);
}
public int compareTo(MyPhoneBook o) {
if(this.name.equals(o.name)) return this.number.compareTo(o.number);
if(this.number.equals(o.number)) return this.email.compareTo(o.email);
if(this.email.equals(o.email)) return this.name.compareTo(o.name);
return 0;
}
}
There are several problems here: the first is that your comparison is not actually lexicographic. For example, when the names are equal, you completely ignore the email address in your comparison logic. The second problem is that you fail to specify the comparator as a parameter to Arrays.sort().
First problem: fix comparison logic
If you can use third-party libraries in your code, then a really neat and simple way to handle this is using the ComparisonChain class from the Guava libraries (Google's core Java libraries that are opensource):
public int compareTo(MyPhoneBook o) {
return ComparisonChain
.start()
.compare(name, o.name)
.compare(email, o.email)
.compare(number, o.number)
.result();
}
Assuming you can't do that, however, here's the right way to do it:
public int compareTo(MyPhoneBook o) {
int nameComparison = name.compareTo(o.name);
if (nameComparison != 0) {
return nameComparison;
}
int emailComparison = email.compareTo(o.email);
if (emailComparison != 0) {
return emailComparison;
}
return number.compareTo(o.number);
}
Second problem: invoke Arrays.sort() with the comparator
Instead of your current call to Arrays.sort(), use:
Arrays.sort(a, new PhoneBookComparator());
And define PhoneBookComparator as a separate Comparator class.
Just use String#compareTo, it compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings.
public class MyPhoneBook implements Comparable<MyPhoneBook>{
#Override
public int compareTo(MyPhoneBook o) {
int returnValue =0;
if(o!=null){
returnvalue = this.name.compareTo(o.name);
if(returnValue==0){
returnValue = this.number.compareTo(o.number);
if(returnValue==0){
returnValue = this.email.compareTo(o.email);
}
}
}
return returnValue;
}
}
When running the code as you posted it it comes to a
Exception in thread "main" java.lang.ClassCastException: ...
MyPhoneBook cannot be cast to java.lang.Comparable
This is because you call
Arrays.sort(a);
Arrays.html#sort expects the Objects contained in the array to be implementing the interface Comparable. Your class is implmenting Comparator though.
You should change your class declaration to one of the following
public class MyPhoneBook implements Comparable<MyPhoneBook> {
and just remove
#Override
public int compare(MyPhoneBook o1, MyPhoneBook o2) {
return o1.number.compareTo(o2.number);
}
Or
public class MyPhoneBook {
and the call of sort to
Arrays.sort(a, new Comparator<MyPhoneBook>() {
#Override
public int compare(MyPhoneBook o1, MyPhoneBook o2) {
// TODO implment here your comapre logic
return o1.number.compareTo(o2.number);
}
});
After you have fixed that you'll get NullPointerException, againt because of the line
Arrays.sort(a);
This is because you pass an array of the length 300 and you do not necessarily have to put all 300 element in it since you ask for total number
you should be using
Arrays.sort(a, 0, n);
This sorts only the range witch have data. See the javadoc.