Java集合框架面试问题集锦(六)

Q:Java集合框架都有哪些最佳实践呢?

A:根据实际的使用情况选择合适的数据结构,例如固定大小的还是需要增加大小的,有重复元素的还是没有的,需要保持有序还是不需要,遍历是正向的还是双向的,插入是在末尾的还是任意位置的,更多的插入还是更多的读取,是否需要并行访问,是否允许修改,元素类型是相同的还是不同的,等等。另外,还需要尽早考虑多线程,原子性,内存使用量以及性能等因素。

不要假设你的集合里元素的数量一直会保持较小,它也有可能随着时间增长。所以,你的集合最好能够给定一个合适的大小。

针对接口编程优于针对实现编程。例如,可能在某些情况下,LinkedList是最佳的选择,但是后来ArrayList可能因为性能的原因变得更加合适

 不好的方式:

ArrayList list = new ArrayList( 100 );

好的方式:

// program to www.bjfp66.com interface so that the implementation canchange
List list = new ArrayList( 100 );
List list2 = new LinkedList( 100 );
 
List emptyList = Collections.emptyList( );
Set emptySet = Collections.emptySet( );

在取得列表的时候,如果返回的结果是空的话,最好返回一个长度为0的集合或者数组,而不要返回null。因为,返回null的话可能能会导致程序错误。调用你的方法的开发人员可能会忘记对返回为null的情况进行处理。

封装好集合:一般来说,集合都是不可变的对象。所以尽量不要把集合的成员变量暴露给调用者。因为他们的操作一般都不会进行必要的校验。

注:这些Java面试题和答案都是从我的书《Core Java Career Essentials》里提取出来的。

英文原文:java-success,编译:ImportNew 朱伟杰

译文地址: http://www.bjfp66.com  

【如需转载,请在正文中标注并保留原文链接、译文链接和译者等信息,谢谢合作!】

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
"Core Java Essentials" are something you must know, and know it well to succeed as a Java professional. Even 40% to 60% of the so called experienced professionals fail to make an impression due to their lack of understanding in one or more of the areas covered in this book. Lack of good understanding in the core essentials can be a speed breaker to your career advancement. If you have a java interview coming up in a few days or you want to impress your peers and superiors with your technical strengths during code review sessions, team meetings, and stand-ups, and concerned about how to make a good impression? You don't need to worry if you are familiar with the fundamentals. This is a Questions & Answers book with 250+ questions and answers relating to core Java with lots of code snippets (100+), examples, and diagrams. Most Java interviews and technical challenges you face at work are structured around the fundamentals and how well you communicate those fundamentals. So regularly brushing up on these fundamentals really pays off. Your analytical, problem solving, and coding skills will also be under scrutiny along with your ability to get the job done with the right tools. If your fundamentals are clear and know what tools to use, you can tackle any questions, and find solutions to any problems and challenges you face. Even if you don't have the exact answer for a problem, you will know how to go about solving them with a little research if you have a solid grounding in the fundamentals covered in this book. A little preparation can make a huge difference to your career success. Preparation can help you communicate your thoughts more clearly with examples and illustrations. Preparation can make a good and lasting impression on those who talk with you during your interviews and team meetings. This impression will be partly influenced by how prepared you are and how knowledgeable you are about your industry and the challenges it faces. It will also be influenced by your appearance, attitude, enthusiasm, and confidence. Good preparation breeds confidence and it shows in the interviews and team meetings. So prepare well in advance if you just begun to see yourself in your dream company or would like to go places in your chosen field.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值