Say I have a simple class like this:
public class BasicObject {
private String name;
public BasicObject (String str) {
name = str;
}
public String getName () {
return name;
}
}
then I have some simple method in the main class like this:
private static int findInList (____ list, String str) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getName().equalsIgnoreCase(str)) {
return i;
}
}
return -1;
}
It would work if I put List<BasicObject> on the blank for the parameter list, but what if I have other objects? Can I generalize the list so this would work with other objects besides BasicObject? If so, how can I get the type of the objects in the list?
Edit: What if I have another class from an imported package that I can still put in a list but I don't want to change what it implements? Is there a way to have a generalized findInList that includes those too? Or do I have to just make another method for that? I'm fine with just making another method, but I'm curious if this way is possible.
You can use an interface structure such as this
Doing so provides a common call structure, so it is predictable what can be called from the implementations
public interface IShape {
String getName();
}
public class Square implements IShape {
private String name = "Square";
#Override
public String getName() {
return name;
}
}
public class Circle implements IShape {
private String name = "Circle";
#Override
public String getName() {
return name;
}
}
public static void main(String[] args){
List<IShape> list = new ArrayList<>();
list.add(new Square());
list.add(new Circle());
list.add(new Circle());
list.add(new Square());
list.add(new IShape() {
#Override
public String getName() {
return "Triangle";
}
});
for(IShape test : list){
System.out.println(test.getName());
}
}
this produces an output like this
Square
Circle
Circle
Square
Triangle
as answer to edit
Yes, but requires reflection, if help is needed on this, please open a new question after proberly trying to solve the issue yourself.
Have you tried checking generics? It is what you're looking for, your method signature should look like below:
public <T> List<T> findInList (Class<T> list, String str) {}
This will work if all objects have common parent (BasicObject or any other class which have getName() method).
private static int findInList (List<? extends BasicObject> list, String str) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getName().equalsIgnoreCase(str)) {
return i;
}
}
return -1;
}
Your code relies on the list having an element type with a getName method that is visible to the compiler. Another way is to use lambdas. For example,
public interface Named {
String getName();
}
public class UseNamed<T extends Named> {
public int findIndex(List<T> nameds, String search) {
for (int len = nameds.size(), ix = 0; ix < len; ++ix) {
final Named named = names.get(ix);
if (search.equalsIgnoreCase(named.getName())) {
return ix;
}
}
return -1;
}
One approach with lambdas is to pass a comparer to findIndex:
public <T, K> int findIndex(List<T> stuff, K search,
BiFunction<T, K, Boolean> compares) {...}
and call it with
List<Named> stuff = ...;
String search = ...;
int index = findIndex(stuff, search,
(named, s) -> s.equalsIgnoreCase(named.getName()));
Related
I have a Task:
public interface Task {
}
Then I have implementations for those:
public interface Task__Init extends Task {
void init(Element e);
}
public interface Task__Hit_Test extends Task {
boolean hit_test(Element e, float x, float y);
}
public interface Task__Draw extends Task {
void draw(Element e);
}
I also have a class that can hold a instance of those implementations:
static public class Task_Holder<T extends Task> {
public int task_id;
public Task_Type type;
public T task;
// ...
}
Then I have a class that holds those, where the last ArrayList holds all of them (all_task_holders)
static public class Implementation_Context {
public HashMap<String, ArrayList<Task_Holder<Task__Init>>> init_solvers = new HashMap<>();
public HashMap<String, ArrayList<Task_Holder<Task__Draw>>> draw_solvers = new HashMap<>();
public HashMap<String, ArrayList<Task_Holder<Task__Hit_Test>>> hit_test_solvers = new HashMap<>();
public ArrayList<Task_Holder<? extends Task>> all_task_holders = new ArrayList<>();
// ...
}
Now one of the problem arises:
static public Task_Holder<?> find_task_holder(int task_id) {
Comparator<Task_Holder<?>> comparator = (a, b)-> {
if (a.task_id < b.task_id) return -1;
if (a.task_id > b.task_id) return 1;
return 0;
};
Collections.sort(ctx.implementation.all_task_holders, comparator);
Task_Holder<?> key = new Task_Holder<>();
key.task_id = task_id;
int index = Collections.binarySearch(ctx.implementation.all_task_holders, key);
for (Task_Holder<?> th : ctx.implementation.all_task_holders) {
if (th.task_id == task_id) {
return th;
}
}
assert false; // should we find things that are not there?
return null;
}
For the binarySearch I get (I make it a codeblock here, else stackoverflow removes words for some reason?):
The method binarySearch(List<? extends Comparable<? super T>>,
T) in the type Collections is not applicable for the arguments
(ArrayList<sfjl_ui.Task_Holder<?>>, sfjl_ui.Task_Holder<capture#6-of
?>)
I have no clue how to fix this. Every attempt breaks other things (for example I break the sort that's 3 lines higher).
It feels like paying off credit card debts with other credit cards, you never win.
How can I fix this?
Pass the comparator as an additional argument:
int index = Collections.binarySearch(ctx.all_task_holders, key, comparator);
I have assignment in java ,I need help please.
I tried to solve it but I have some problem that can't understand them.
Assignment is:
In this exercise, use the Template method pattern to define an abstract class Filter with a public method filter (the template method) that calls the method accept (the hook method) that can be implemented in different ways in the different concrete classes. Write a test program by extending the class Filter and defining accept so that only strings of at most three characters are accepted.
public abstract class Filter<T> {
public abstract T[] filter(T[] list);
public abstract boolean accept(T val);
}
public class FilterTest<T> extends Filter<T> {
private int capacity = 0;
public FilterTest(int cap) {
this.capacity = cap;
}
#Override
public T[] filter(T[] list1) {
#SuppressWarnings("unchecked")
T[] finalList = (T[]) Array.newInstance(list1.getClass().getComponentType(), capacity);
int counter = 0;
for (T t : list1) {
if (accept(t)) {
finalList[counter] = t;
counter++;
}
}
return finalList;
}
public void printArray(T[] list2) {
for (int i = 0; i < list2.length; i++) {
if (list2[i] != null) {
System.out.print(list2[i] + " ");
}
}
System.out.println();
}
#Override
public boolean accept(T val) {
return String.valueOf(val).length() > 0 &&
String.valueOf(val).length() <= 3;
}
public static void main(String[] args) {
FilterTest<String> filterTest = new FilterTest<>(8);
String[] lists = {
"Hi", "here", "is", "the", "AOOP", "course", "at", "University"
};
System.out.print("My original list is: ");
filterTest.printArray(lists);
System.out.print(" The filtered list is: ");
String[] filteredList = filterTest.filter(lists);
filterTest.printArray(filteredList);
}
}
Here is comment from my teacher:
"not correct, only the accept method should be abstract in the Filter class, the filter method should be already implemented in the Filter class and not be abstract all implementation will be the same, only the accept method changes for different filters)".
I don't understand what should I do now, how the code will be correct.
help please,
Thanks
I assume that Filter should look something like this
public abstract class Filter<T> {
public T[] filter(T[] list1) {
#SuppressWarnings("unchecked")
T[] finalList = (T[]) Array.newInstance(list1.getClass().getComponentType(), capacity);
int counter = 0;
for (T t : list1) {
if (accept(t)) {
finalList[counter] = t;
counter++;
}
}
return finalList;
}
public abstract boolean accept(T val);
}
You can even declare Filter<T> as an interface and have a default implementation for filter. Have a look here
I have this Test class and I have to write the others classes code in order to pass the asserts.
That's the Test class:
import java.util.*;
public class Test
{
public static void main(String[] args)
{
Book javabook = new Book("Learn Java",150);
Book cbook = new Book("C",120);
Book[] books = new Book[] {javabook, cbook};
Library lib = new Library(books);
assert(lib.pages() == 270);
List l = lib;
assert(l.size() == 2);
Collections.sort(l);
assert(l.get(0) == cbook);
assert(l.get(1) == javabook);
}
}
I started making the Book class, here is my implementation:
public class Book implements Comparable
{
private String name;
private int pages;
public Book(String name, int pages)
{
this.name = name;
this.pages = pages;
}
public int getPages()
{
return this.pages;
}
public int compareTo(Object obj)
{
if(this == obj) return 0;
else
{
Book x = (Book) obj;
return this.pages - x.pages;
}
}
}
Then I wrote the Library class:
import java.util.*;
public class Library extends ArrayList
{
ArrayList a;
public Library(Book[] b)
{
a = new ArrayList(b.length);
for(int i=0; i<b.length; i++)
{
a.add(b[i]);
}
}
public int pages()
{
int p = 0;
for(int i=0; i<a.size(); i++)
{
p += ( (Book) a.get(i) ).getPages();
}
return p;
}
public int size()
{
return a.size();
}
public Object get(int i)
{
return a.get(i);
}
}
I think the problem is on the Library class, seems like the Collections.sort doesn't work and I can't pass the asserts after the call of the method sort in the Test class! I can't figure out what's the problem on my code, can someone help me please?
Remember: I have to make my code in order to pass the asserts.
I'm not sure about my Library implementation in order to make this line on the Test work:
List l = lib;
Not sure about the ArrayList extension.
P.S. I know it's better to use generic types but I mustn't use them for this exercise. Without using them I get some warnings, just ignore them.
Your implementation of the Library class is indeed wrong - it shouldn't have a list of Books, it should be a list of Books - that's why it extends an ArrayList:
public class Library extends ArrayList<Book> {
// Note: no additional data members.
// The Library is already a List<Book>:
public Library(Book[] b) {
super(Arrays.asList(b));
}
public int pages() {
int p = 0;
for(int i = 0; i < size(); i++) {
p += get(i).getPages();
}
return p;
}
}
I'm trying to implement a Set which is ordered by the count of additions like this:
public class App {
public static void main(String args[]) {
FrequencyOrderedTreeSet<String> set = new FrequencyOrderedTreeSet<String>();
set.add("bar");
set.add("foo");
set.add("foo");
Iterator<String> i = set.iterator();
while (i.hasNext()) {
System.out.print(i.next());
}
// prints "foobar"
}
}
I've created a protected class FrequencyOrderedTreeSet.Element which implements Comparable and has a T entry and an int frequency property and extended TreeSet<FrequencyOrderedTreeSet.Element> with FrequencyOrderedTreeSet<T> and overrode the compareTo and equals methods on the Element.
One problem is that I can't override the add() method because of type erasure problems and also I can't call instanceof Element in the equals method, because in case object given to it is an Element, I have to compare their entries, but if it's not, I have to compare the object itself to this.entry.
In the add method I create a new element, find the element with the same entry in the set, set the frequency on the new element to "old+1", remove the old one and add the new one. I'm not even sure this is the best way to do this or if it would work even because the other problems I described.
The question is: what's the best way to implement such data structure? In case I'm somehow on the right track - how can I circumvent the problems I've mentioned above?
Here's a basic implementation. It's not the most optimal and will take some more work if you want to implement the full Set interface.
public class FrequencySet<T> implements Iterable<T>
{
private TreeSet<T> set;
private HashMap<T, Integer> elements = new HashMap<T, Integer>();
public FrequencySet()
{
set = new TreeSet<T>(new Comparator<T>()
{
public int compare(T o1, T o2)
{
return elements.get(o2)-elements.get(o1);
}
});
}
public void add(T t)
{
Integer i = elements.get(t);
elements.put(t, i == null ? 1 : i+1);
set.remove(t);
set.add(t);
}
public Iterator<T> iterator() {return set.iterator();}
public static void main(String [] args)
{
FrequencySet<String> fset = new FrequencySet<String>();
fset.add("foo");
fset.add("bar");
fset.add("foo");
for (String s : fset)
System.out.print(s);
System.out.println();
fset.add("bar");
fset.add("bar");
for (String s : fset)
System.out.print(s);
}
}
The key is in the add method. We change the counter for the given object (which changes the relation order), remove it from the backing set and put it back in.
This works the other way (count is increased when you use GET)
#SuppressWarnings("rawtypes")
final class Cache implements Comparable {
private String key;
private String value;
private int counter;
public String getValue() {
counter++;
return value;
}
private void setValue(String value) { this.value = value; }
public String getKey() { return key; }
private void setKey(String key) { this.key = key; }
public int getCounter() { return counter; }
public void setCounter(int counter) { this.counter = counter; }
public Cache(String key, String value) {
this.setKey(key);
this.setValue(value);
setCounter(0);
}
#Override
public int compareTo(Object arg0) {
if(!(arg0 instanceof Cache)) {
throw new ClassCastException();
}
return this.getCounter() - ((Cache) arg0).getCounter();
}
}
I tried looking up tutorials and videos and I understand what implementing does, although I'm a bit confused as to how one would implement a class from the Java Library. In my homework, I'm suppose to utilize the class, DataSet and make it so it accepts Comparable objects. The program is suppose to record the Min and Max values depending on the objects, in this case, I'm suppose to use strings. I wasn't sure if I needed any classes to implement the Comparable interface, so I made two classes just in case I was suppose to do so. My real question is how do I actually incorperate a String variable in the tester class to actually read and compare the object to another? thanks in advance.
public class Word implements Comparable
{
private String str;
public Word()
{
str = null;
}
public Word(String s)
{
str = s;
}
public int compareTo(Object other)
{
String n = (String) other;
return str.compareTo(n);
}
}
I wasn't sure which of the two classes would be suitable for implementing Although i think the String class below would not work at all b/c It's already a standard class so I wasn't too sure about using it
public class String implements Comparable
{
public String s;
public String()
{
s = null;
}
public String(String str)
{
s = str;
}
public int compareTo(Object other)
{
String n = (String) other;
return s.compareTo(n);
}
}
public interface Comparable
{
public int compareTo(Object other);
}
public class DataSet
{
private Object maximum;
private Object least;
private Comparable compare;
private int count;
public DataSet(Comparable s)
{
compare = s;
}
public void add(Object x)
{
if(count == 0)
least = x;
if(count == 0 || compare.compareTo(x) >=0)
maximum = x;
else if(compare.compareTo(x) <0)
least = x;
count++;
}
public Object getMaximum()
{
return maximum;
}
public Object getLeast()
{
return least;
}
}
public class DataSetTester
{
public static void main(String[] args)
{
Comparable n = new Word("sand");
DataSet data = new DataSet(n);
data.add(new Word(man));
System.out.println("Maximum Word: " + data.getMaximum());
System.out.println("Least Word: " + data.getLeast());
}
}
An interface is a contract that showes that your class contain all methodes that are implemented in the interface. In this case the CompareTo(object other). The String class already implements the comparable interface so you don't need youre own class. I think your data set class should look something like this :
public class DataSet<T implements Comparable>
{
private T maximum;
private T least;
private T count;
public void add(T x)
{
if(count == 0){
least = x;
maximum = x;
}
else if(least.compareTo(x) > 0)
least = x;
else if(maximum.compareTo(x) < 0)
maximum = x;
count++;
}
public T getMaximum()
{
return maximum;
}
public T getLeast()
{
return least;
}
}
T is a generic type and in your case it should be String, Here is how you create a new Data set:
DataSet<String> ds = new DataSet<String>;