简体   繁体   English

创建自己的数据结构 class 还是使用现有的实现?

[英]Create your own data structure class or use existing implementation?

So I've been going through data structure, I can create my own linkedlist class with its functions ( insert, remove, length etc ) but why should I do that when I can simply do the following所以我一直在研究数据结构,我可以创建自己的链表 class 及其功能(插入、删除、长度等),但是当我可以简单地执行以下操作时,为什么要这样做

LinkedList<>list = new LinkedList<>();

One good justification to the question is: Spring models or Spring POJOs.该问题的一个很好的理由是:Spring 模型或 Spring POJO。

It can be troublesome to have bean of type Map in old Spring for example and autowire it, but if you create a class that extends Map or that failing type then it will work: It can be troublesome to have bean of type Map in old Spring for example and autowire it, but if you create a class that extends Map or that failing type then it will work:

public class MyDataContainer extends Map<int, String> {
...
}

Another use is when you want to perform custom action on get or set, then extending Data Structure type and overriding one of its getters or setter can be useful, example: strip value from extra spaces before setting or putting it in the List , Map , or whatever data structure you've modified.另一种用途是当您想对 get 或 set 执行自定义操作时,扩展 Data Structure 类型并覆盖其中一个 getter 或 setter 可能很有用,例如:在设置或将其放入List之前从多余的空格中Map ,或您修改过的任何数据结构。 I've actually had to do that couple times over the course of my career.在我的职业生涯中,我实际上不得不这样做几次。

Generally, this question asked in an interview, design the functionality for LinkedList. 
If you have deep knowledge of LinkedList, then you can design the LinkedList for different languages.
Custom LinkedList implementation will check your deep understanding, how it works internally.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM