Home Java JavaInterview questions Collection of classic Java interview questions (7)

Collection of classic Java interview questions (7)

Jul 15, 2020 pm 05:15 PM
java Interview questions

Collection of classic Java interview questions (7)

1. Let’s talk about the difference between ArrayList and Vector

(more recommended interview questions: java interview questions)

1. Synchronicity: Vector is thread-safe. Use synchronized to achieve thread safety, while ArrayList is thread-unsafe. If only one thread will access the collection, it is best to use ArrayList because it does not consider Thread-safe, the efficiency will be higher;

If multiple threads will access the collection, it is best to use Vector, because we do not need to think about and write thread-safe code.

2. Data capacity growth: Both have an initial capacity size and use linear continuous storage space. When the number of stored elements exceeds the capacity, the storage space of both needs to be increased, and Vector grows. Double the original value, ArrayList increases by 0.5 times the original value.

2. Why is the ArrayList thread unsafe?

The operation of adding elements to the ArrayList is carried out in two steps, that is, the first step is to Store the elements to be added at the location of object[size]; in the second step, increase the value of size by 1.

Since this process is not guaranteed to be atomic in a multi-threaded environment, ArrayList is thread-unsafe in a multi-threaded environment.

(Related tutorial recommendations: java introductory tutorial)

3. What are the differences between HashMap, LinkedHashMap, and TreeMap?

1. HashMap is the most commonly used Map. It stores data according to the hashCode value of the key. Its value can be obtained directly according to the key, and has a very fast access speed. HashMap only allows the key of one record to be null, and does not allow the value of multiple records to be null.

HashMap does not support thread synchronization, that is, multiple threads can write HashMap at the same time at any time, which may lead to data inconsistency. If synchronization is required, you can use the Collections.synchronizedMap(HashMap map) method to make HashMap synchronized.

2. Hashtable is similar to HashMap, except that it does not allow the recorded keys or values ​​to be empty; it supports thread synchronization, that is, only one thread can write to Hashtable at any time. However, this also leads to Hashtable will be slower when writing.

3. LinkedHashMap saves the insertion order of records. When traversing LinkedHashMap with Iteraor, the record obtained first must be inserted first. It will be slower than HashMap when traversing. Has all the features of HashMap.

4. TreeMap can sort the records it saves according to keys. The default is in ascending order. You can also specify a sorting comparator. When using Iteraor to traverse a TreeMap, the records obtained are sorted. The keys and values ​​of TreeMap cannot be empty.

4. How to remove duplicate elements from a Vector collection?

Use the Vector.contains() method to determine whether the element is included. If it is not included, add it to a new collection. It is suitable for cases where the data is small.

There is also a simple way to traverse the Vector and put in set, SortdSet, HashSet, etc.

(Video tutorial recommendation: java video tutorial)

5. What are the characteristics of the three interfaces List, Map, and Set when accessing elements? ?

1. Duplicate elements are not allowed in Set

Save elements:

The add method has a boolean return value. When there is no such element in the set, For an element, when the add method can successfully add the element, it returns true; when the set contains an element equal to an element equals, the add method cannot add the element at this time, and the return result is false.

Get elements:

There is no way to tell which number to get. You can only get all the elements through the Iterator interface, and then iterate through each element one by one.

2. List represents a sequential collection

Storing elements:

When the add(Object) method is called multiple times, the objects added each time are sorted in the order of first come, first served. , you can also jump in the queue, that is, call the add(int index,Object) method to specify the storage location of the current object in the collection.

Getting elements:

Method 1: The Iterator interface gets all the elements and iterates through each element one by one.

Method 2: Call get(index i) to clearly indicate which index to take. Use this interface to precisely control the insertion position of each element. Users can access elements in the List using the index (the position of the element in the List, similar to an array subscript), which is similar to Java's array.

3. Map is a double-column collection

Storing elements:

Use the put method, put(obj key, obj value). Each time you store, you need to store one For key/value, duplicate keys cannot be stored. The duplication rule is also based on equals comparison.

Get elements:

Use the get(Object key) method to obtain the corresponding value based on the key. You can also get a collection of all keys, a collection of all values, and a collection of Map.Entry objects composed of keys and values.

List holds elements in a specific order and can have duplicate elements; Set cannot have duplicate elements and is sorted internally; Map saves key-value values, and value can be multi-valued.

The above is the detailed content of Collection of classic Java interview questions (7). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Square Root in Java Square Root in Java Aug 30, 2024 pm 04:26 PM

Guide to Square Root in Java. Here we discuss how Square Root works in Java with example and its code implementation respectively.

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Armstrong Number in Java Armstrong Number in Java Aug 30, 2024 pm 04:26 PM

Guide to the Armstrong Number in Java. Here we discuss an introduction to Armstrong's number in java along with some of the code.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

See all articles