Java interview questions (IO, collection)

I流
What is IO

Data input and output are based on streams, and all data is serialized

Classification of io flow?

Flow direction:

Input stream: read from file to memory, only read operation

Output stream: read from memory to file, only write operation

Operating unit:

Character stream: in units of characters, 16-bit data is read in or out each time, and only character type data can be read

Byte stream: in bytes, 8 bits of data are read in or out each time, and any type of data can be read

Role:

Node stream: directly connected to the data source, read in or read out

Processing flow: also called packaging flow, wrapping existing flow

InputStream (byte input stream): read data (bytes) from the file into memory

OutputStream (byte output stream): write data (bytes) in memory to a file

Reader (character input stream): read data (characters) from the file into memory

Writer (character output stream): write data (characters) in memory to a file

What is the difference between BIO, NIO, and AIO?

BIO: synchronous blocking IO (traditional IO), poor concurrency in the face of millions of connections

NIO: Synchronous non-blocking IO, based on channels and buffers, realizes IO multiplexing and reduces CPU consumption

AIO: asynchronous non-blocking IO, good concurrency, based on event and callback mechanism

How byte streams are converted to character streams

Byte input stream to character input stream is realized by InputStreamReader

Byte output stream to character output stream is realized by OutputStreamWriter

How to choose byte stream and character stream
  1. In most cases, byte streams are used, because byte streams are the wrappers of character streams, and most IOs directly operate on disk files.

b. If you need to process strings frequently, choose a character stream with a buffer

What is a buffer? what's the effect?

A special memory area. When a resource is frequently operated, the performance will be reduced. In order to improve performance, a part of the data can be temporarily written to the cache area.

Character streams operate on buffers.

What is object serialization? What is deserialization?

Object serialization: save the object on the hard disk in binary form

Deserialization: convert binary files into objects for reading

How to clone objects

Override the clone method of Object

Cloning via serialization and deserialization

gather
What is a collection? Collection features? what is the benefit?

A collection is a container for storing data;

Features:

  1. used to store data

  1. collections of variable length

benefit:

  1. Capacity self-growth

  1. The collection can be easily extended or rewritten

  1. low learning cost

  1. High-performance data structure algorithm to improve response speed and quality

Difference between collection and array?
  1. Arrays are of fixed length, collections are not

  1. Arrays can store basic data types and reference data types, and collections can only store reference data types

  1. Array storage must be of the same data type, and collections can store different data types

Why use collections?

When we need to save a group of data of the same type, we should use a container to save it. This container is an array. However, using an array to store objects has certain disadvantages, because in our actual development, the type of data stored It is diverse, so there is a "collection", which is also used to store multiple data; but the collection improves the flexibility of data storage. Java collections can not only be used to store objects of different types and quantities, You can also save data with a mapping relationship

How to choose a collection?

The first choice is to choose according to the characteristics, you need to get elements according to the key value, choose Map, and only need to store data and choose List or Set;

Map:

Choose TreeMap if you need sorting; choose HashMap if you don’t need sorting; choose ConcurrentHashMap for thread safety;

Collection:

Choose to implement Set when the element is guaranteed to be unique; there is no need to choose List;

What are the main commonly used collection classes?

Map:hashMap、TreeMap、HashTable、LinkedHashMap

List:ArrayList、LinkedList、Vector

Set:HashSet、TreeSet、LinkedHashSet

What is the difference between List, Map, and Set?

List: ordered, elements can be repeated, and multiple null elements can be inserted

Map: a collection of key-value pairs, key: unordered and unique, value: duplicates are allowed; each key can only be mapped to one value

Set: Unordered, elements cannot be repeated, and a null element can be stored

The underlying data structure of the collection framework

List:

ArrayList: array

LinkedList: Doubly linked list

Vector: array

Set:

HashSet:hashMap

TreeSet: red-black tree

LinkedHashSet:hashMap

Map:

hashMap: Before JDK1.8, HashMap was composed of array + linked list; after that, it was composed of array + linked list + red-black tree

TreeMap: red-black tree

HashTable: array + linked list

LinkedHashMap: array + linked list

What is iterator Iterator? What are the characteristics?

The Iterator interface provides an interface for traversing any collection

Features: One-way traversal, safe

Advantages and disadvantages of ArrayList
  1. Low-level array, fast search speed

  1. Adding elements sequentially is fast

  1. Deleting elements consumes performance

  1. Inserting elements consumes performance

Expansion mechanism of ArrayList

When using ArrayList() to create an ArrayList object, the length of the underlying array is not defined. When the add(E e) method is called for the first time, the length of the underlying array is initially defined as 10, and when add(E e) is called later, if necessary To expand capacity, call grow(int minCapacity) to expand the capacity, and the length is 1.5 times the original

The difference between ArrayList and LinkedList
  1. The bottom layer of ArrayList is an array; the bottom layer of LinkedList is a linked list

  1. LinkedList has better performance than ArrayList when adding and removing elements;

  1. ArrayList is better than LinkedList when querying

The difference between List and Set
  1. both inherit from the same interface

  1. List: ordered, elements can be repeated, and multiple null elements can be inserted; Set: unordered, elements cannot be repeated, and one null element can be stored

  1. Set query efficiency is low, adding and deleting efficiency is high; List query efficiency is high, adding and deleting efficiency is low

How does HashSet ensure that data is not repeated

When adding an element, the basis for judging whether the element exists not only needs to compare the hashCode value, but also combines equles comparison; to judge whether the key exists, if it exists, overwrite the old value with the new value, and return the old value.

What is the difference between jdk1.7 and jdk1.8 of HashMap?

Before JDK1.8, HashMap was composed of array + linked list; after that, it was composed of array + linked list + red-black tree;

hashMap expansion mechanism

1.7: Create a new empty array with twice the original length; traverse the elements of the linked list at each position of the original array, get the subscript of the element, and insert it into the new array; 

1.8: Create a new empty array, traverse the linked list or red-black tree at each position of the old array; if it is a linked list, directly calculate the subscript and add it to the new array; if it is a red-black tree, first traverse the red-black tree , insert the subscript position of each element in the red-black tree in the new array into the new array;

The difference between hashMap and hashTable
  1. hashMap: non-thread-safe, hashTable: thread-safe (internal method synchronized modification)

  1. hashMap is more efficient than hashTable

  1. hashMap key-value pairs allow null values, hashTable does not allow

  1. The default size of hashMap is 16, and the growth method is an exponential multiple of 2; the default size of hashTable is 11, and the growth method is twice the old +1

  1. TreeMap is sorted in ascending order by default

The difference between Collection and Collections
  1. Collection is a collection interface; Collections is a wrapper class

  1. Collection provides common interface methods for basic operations on collection objects; Collections contains static polymorphic methods for collections

  1. Collections serve the Collection framework

  1. Collection includes interfaces such as List, Set, and Map; Collections includes methods such as inversion, copy, and replacement

おすすめ

転載: blog.csdn.net/qq_35056891/article/details/129641552