Hi I am trying to sort a generic object. When the object is created, it is created with three generic parameters and I want the parameters to be sorted when it is created.
Here is my code:

import java.util.*;

public class SortedTrio<T> extends Trio {
   
    T firstST;
    T secondST;
    T thirdST;
    
    public SortedTrio (T firstST, T secondST, T thirdST) {
    	
    	super(firstST, secondST, thirdST);    	
	
    }

}

and this is where the objects are created

import java.util.*;
public class TestHw2{
 
    public static void main(String[] args) {
       
       SortedTrio<Integer> STobj = new SortedTrio<Integer>(3, 1, 5);
       SortedTrio<Integer> STobj1 = new SortedTrio<Integer>(5, 3, 1);
		boolean isEqual = STobj.equals(STobj1);
		if(isEqual)
			System.out.println("Both are equal!");
		else
			System.out.println("Neither are equal!");
			
		System.out.println(STobj1.toString());
		System.out.println(STobj.toString());
		
    }
}

this is my trio class if that's necessary to look at:

import java.util.*;

public class Trio<T> implements Comparable<Trio>
{
	private List<T> arrObj = new ArrayList<T>();	//arraylist of generic type objects
	
	public Trio(T first, T second, T third)
	{
		//add string objects to generic arraylist (which is set as a string arraylist by the main)
		arrObj.add(first);
		arrObj.add(second);
		arrObj.add(third);
	}
	
	public T first() {	//return first element
		return arrObj.get(0);
	}
	
	public T second() {	//return second element
		return arrObj.get(1);
	}
	
	public T third() {	//return third element
		return arrObj.get(2);
	}
	
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	public T contains(T value) { //not done
		return value;
	}
	
	public boolean equals(Trio t_object) {
		boolean tt = false;
		if (this.first() == t_object.first() && this.second() == t_object.second() && this.third() == t_object.third())
			tt = true;
		return tt; 
	}
	
	public boolean matches() {	//not done
		return true;
	}
	
	public String toString() {
		String strValue = "(" + arrObj.get(0) + ", " + arrObj.get(1) + ", " +  arrObj.get(2) + ")";
		return strValue;
	}
	
	
	public int compareTo(Trio that) 
    {
			return 0;
    }
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	public static void main(String[] args) {
		Trio<String> triObj = new Trio<String>("abc", "def", "ghi");		//create trio object of string type and give 3 string parameters
		//retrieve strings in the object by using generic methods first(), second(), third() 
		System.out.println("The elements in generic class Trio are: " + triObj.first() +", " + triObj.second() +", " + triObj.third());			
	}	
}

if there is anything that needs explaining, please let me know.

Thank you so much for any help!

Recommended Answers

All 7 Replies

And your question/problem is?

IMO, you don't need a List when creating a Trio; I'd personally swap the usage of List in Trio and SortedTrio i.e. make Trio have three variables first, second & third whereas use a List only when the sorting functionality is required.

Also, you don't need to manually sort the Trio. As long as the elements of the SortedTrio [T] implement the Comparable interface i.e. implement natural ordering, the Collections#sort method can be used to get the job done. So, the simplest solution here would be to restrict the domain of the generic type T i.e. make sure each T added to the SortedTrio is an instance of the Comparable interface. If this is taken care of, all that is left is to add the `first', `second' and `third' variables to the List when the SortedTrio is created, sort the list and you should be good to go. Make sure that either you don't provide setters for `first', `second' or `third'; if you do, don't forget to sort the list *again* if any of them changes.

IMO, you don't need a List when creating a Trio; I'd personally swap the usage of List in Trio and SortedTrio i.e. make Trio have three variables first, second & third whereas use a List only when the sorting functionality is required.

Also, you don't need to manually sort the Trio. As long as the elements of the SortedTrio [T] implement the Comparable interface i.e. implement natural ordering, the Collections#sort method can be used to get the job done. So, the simplest solution here would be to restrict the domain of the generic type T i.e. make sure each T added to the SortedTrio is an instance of the Comparable interface. If this is taken care of, all that is left is to add the `first', `second' and `third' variables to the List when the SortedTrio is created, sort the list and you should be good to go. Make sure that either you don't provide setters for `first', `second' or `third'; if you do, don't forget to sort the list *again* if any of them changes.

well, actually that's what i tried to do. implement a comparable interface, but i don't know how to implement a comparable interface but i'll do the swap. can u explain how to implement the comparable interface? thanks

Here is my code with the swap:

import java.util.*;

public class Trio<T> implements Comparable<Trio>
{
	//private List<T> arrObj = new ArrayList<T>();	//arraylist of generic type objects
	T firstTrio;
	T secondTrio;
	T thirdTrio;
	
	public Trio(T first, T second, T third)
	{
		//add string objects to generic arraylist (which is set as a string arraylist by the main)
		firstTrio = first;
		secondTrio = second;
		thirdTrio = third;
	}
	
	public T first() {	//return first element
		return firstTrio;
	}
	
	public T second() {	//return second element
		return secondTrio;
	}
	
	public T third() {	//return third element
		return thirdTrio;
	}
	
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	public T contains(T value) { //not done
		return value;
	}
	
	public boolean equals(Trio t_object) {
		boolean tt = false;
		if (this.first() == t_object.first() && this.second() == t_object.second() && this.third() == t_object.third())
			tt = true;
		return tt; 
	}
	
	public boolean matches() {	//not done
		return true;
	}
	
	public String toString() {
		String strValue = "(" + firstTrio + ", " + secondTrio + ", " +  thirdTrio + ")";
		return strValue;
	}
	
	
	public int compareTo(Trio that) 
    {
			return 0;
    }
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	public static void main(String[] args) {
		Trio<String> triObj = new Trio<String>("abc", "def", "ghi");		//create trio object of string type and give 3 string parameters
		//retrieve strings in the object by using generic methods first(), second(), third() 
		System.out.println("The elements in generic class Trio are: " + triObj.first() +", " + triObj.second() +", " + triObj.third());			
	}	
}

I haven't made any changes to the other two classes. But I still need to know how to implement the Comparable interface so I can use the collections sort.

Pretty much all you have to do is implement the interface and define a compareTo method. Here's the interfaces code:

public interface Comparable<T> {
    public int compareTo(T o);
}

what SOS means is restrict the domain of your class to classes that implement this interface, like so:

public class Trio<T implements Comparable> implements Comparable

So to sort it you just have to use Collections.sort(instance of Trio); Now implementing comparable in your class is where having the T's in an array would help. You could use

public int compareTo(Trio o) {
for (int x=0; x<=2; x++) {
if ( myarray[x] > o.myarray[x]) return 1;
}
else if (myarray[0] == o.myarray[0] && /*so on */)
return 0;
else return -1;
}

:D

hey jugglerdrummer,

public class Trio<T implements Comparable> implements Comparable

gives me an error it says: syntax token on implements ",," expected

There is no `implements' keyword when using generics syntax. Irrespective of whether you are restricting your type to a particular class or an interface, you use "extends". So your SortedTrio should look something like:

class SortedTrio<T extends Comparable<? super T>> extends Trio<T>

This roughly reads as "SortedTrio consists of elements of type T where the reference type T is a Comparable. SortedTrio in turn extends the class Trio having elements of type T". The Comparable<? super T> part ensures that if a child class doesn't implement the Comparable interface, the compareTo implementation of its parent classes can be used.

I'd recommend reading Generics tutorial to get a good hang of things.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.