在构建容器时,为什么使用 Java 泛型比使用 Object 类更好?(Java Generics & DataStructures)



所以我一直在审查我的数据结构,并遇到了一个关于Java泛型和Object类的有趣想法。我以两种不同的方式实现和运行"通用包">(注意如下:IObjectBag.java,ObjectBag.java,IGenericBag.java和GenericBag.java(,并同时使用了它们(注意:在main.java和Output下面(。我已经根据堆栈溢出规则删除了一些不必要的代码,但如果您想要完整的实现,请告诉我。

此外,除了查看ArrayList课程的源代码外,我还在许多网站、书籍和课程中研究了这个主题,我知道我的GenericBag比我的ObjectBag更好,但不足以在面试中以实际的方式解释它。而且我很困惑,我的GenericBag在其实现中使用了比我的ObjectBag更多的转换操作(请参阅RemovePrintBag(。

  1. 那么,除了句法糖,为什么我的GenericBag更好?请以我的类为例。

  2. 我没有注意到运行时/开销/空间/时间有什么重要差异吗?

  3. 你会如何回答这个问题或期望在面试中得到回答?

奖励问题:如果您愿意,请回答Main中的奖励问题GenericBag评论(我想我可以自己回答,只是想听听您的意见(。

IObjectBag接口:

public interface IObjectBag {
void add(Object item);
Object remove(Object item) throws NoSuchElementException;
boolean isEmpty();
int find(Object item);
Object get(int index);
int numItems();
}

ObjectBag类:

public class ObjectBag implements IObjectBag {
private Object [] items; // the java class attribute that will hold out "ints"
private int numItems;

public static void printBag(IObjectBag bag) {
for(int i = 0; i < bag.numItems(); i++) {
System.out.println(bag.get(i));
}
}

public ObjectBag(int size) {
this.items = new Object[size]; // fills array with null values
this.numItems = 0;
}

public void add(Object item){
// adds item to end of bag
}

public Object remove(Object item) {
int index = this.find(item);
if(index == -1) throw new NoSuchElementException("oops nothing found");

Object out = this.items[index];

this.items[index] = null;
this.numItems -= 1;

if(index + 1 != this.items.length && this.items[index + 1] != null) {  
for(int i = index; i < this.items.length; i++) {
if(i + 1 != this.items.length) this.items[i] = this.items[i + 1];
}

this.items[this.items.length - 1] = null;
}

return out;
}

public int find(Object item) {
// return index given item or -1 
}

public Object get(int index) {
// returns item given index
}

}

IGenericBag类:

public interface IGenericBag <T> {
void add(T item);
T remove(T item) throws NoSuchElementException;
boolean isEmpty();
int find(T item);
T get(int index);
}

GenericBag类:

public class GenericBag<T> implements IGenericBag<T> {
// private T[] items; can't use this b/c see comment in constructor
private Object[] items;
private int numItems;

public static void printBag(GenericBag bag) {
for(int i = 0; i < bag.numItems(); i++) {
System.out.println(bag.get(i));
}
}

public GenericBag(int size) {
// this.items = new T[size]; Bonus: throws generic array creation error (why?)
this.items = new Object[size];
this.numItems = 0;
}

public void add(T item){
this.items[this.numItems] = item;
this.numItems += 1;
}

public T remove(T item) {
int index = this.find(item);
if(index == -1) throw new NoSuchElementException("oops nothing found");

T out = (T) this.items[index];

this.items[index] = null;
this.numItems -= 1;

if(index + 1 != this.items.length && this.items[index + 1] != null) {  
for(int i = index; i < this.items.length; i++) {
if(i + 1 != this.items.length) this.items[i] = this.items[i + 1];
}

this.items[this.items.length - 1] = null;
}

return out;
}

public int find(Object item) {
// given object return index or throw exception
}

public T get(int index) {
return (T) this.items[index];
}

}

Main类:

public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("Hello StackOverFlow!");
Object int1 = new Integer(1);
Object int2 = new Integer(2);
Object int3 = new Integer(3);


/* using my object bag ************************************************/
System.out.println("using my object bag");
IObjectBag myObjectBag = new ObjectBag(3);

myObjectBag.add(int1);
myObjectBag.add(int2);
myObjectBag.add(int3);
myObjectBag.remove(int2);

ObjectBag.printBag(myObjectBag);

/* using my generic bag ***********************************************/
System.out.println("using generic bag");


// Bonus Question: using object like above causes error at add method (why?)
Integer int4 = new Integer(4); 
Integer int5 = new Integer(5);
Integer int6 = new Integer(6);

GenericBag<Integer> myGenericBag = new GenericBag<Integer>(3); 
//Bonus Question: using Interface decllaration like above causes error in print bag (why?) 

myGenericBag.add(int4);
myGenericBag.add(int5);
myGenericBag.add(int6);
myGenericBag.remove(int4);

GenericBag.printBag(myGenericBag);
}

}

输出:

Hello StackOverFlow!
using my object bag
1
3
using generic bag
5
6

ObjectBag 的问题由 GenericBag 实现提供的类型安全"自动"解决:

  • 访问条目返回 Object,在此阶段您不知道 Object 是什么类型。
  • 您可以将任何类型的对象(混合(插入到同一个列表中,例如字符串和整数,这是一种反模式并导致代码不可读(尝试使用您的泛型包!
  • 因为编译器在声明 GenericBag 后就知道它的类型,所以在代码的任何阶段,如果你将鼠标悬停在 genericBag 实例上,你就会知道它的类型,这使得你的代码更具可读性,并且还可以为其他人扩展

泛型也提供了更多,想象一下,你希望你的GenericBag只接受数字,那么你可以这样写:

public class GenericBag<T extends Number>

我给你的建议是阅读一些关于Java基础知识的文章,尤其是泛型,有一个基于实践的学习方式是一件好事,但是有很多文章可以给你一些非常好的理论见解。

https://www.baeldung.com/java-generics

比方说,使用GenericBag<String>ObjectBag的原因与在Object上使用String(或任何其他类型(的原因基本相同:

类型安全。

你声明某个方法返回一个字符串集合,而不是其他任何内容,从而阻止你自己把其他对象放在那里,或者试图把你从包里得到的东西当作其他类型的东西。当你有 100 行代码时,这可能听起来很愚蠢,但当你使用体面的代码库时,这可能会为你节省大量的调试时间。

虽然,类型安全不是灵丹妙药,它只是一种工具,有些人觉得有用,有些人觉得没有。我很确定这是任何编程论坛的热门话题。

如果你觉得没有这种范式(Javascript背景,对吧?(工作很舒服,你可以考虑尝试一些动态类型的语言,如Python而不是Java。

最新更新