1.
the difference between Set and List
A List is an ordered collection that allows duplicate elements. We can insert and access elements
based on their position or index.
A Set, on the other hand, stores only unique elements. It generally doesn't maintain the order of
elements, of cause some implementations like LinkedHashSet can maintain the insertion order.
So, we use a List when we need to maintain the order of insertion or duplicate elements are allowed.
and we use a Set when the order of elements is not important or when we need a sorted element
such as treeset.
1.the difference between ArrayList and LinkedList
Arraylist backed by an array, better for random access but slower for insertion and deletion.
Linkedlist has double-linked list structure, better for insertion and deletion but slower for random
access.
2.How does HashMap work? Is it thread - safe? How to make it thread - safe?
A HashMap uses a hash table with chaining. When you put a key - value pair in, it calculates the
key's hash code to figure out which bucket in the table to store it in. If multiple keys end up in the
same bucket, they're linked together in a chain.
HashMap isn't thread - safe. That means if multiple threads try to read and write to it at the same
time, you will get errors or different result.
To handle concurrency, you can use ConcurrentHashMap. Unlike HashTable, which locks the whole
map when accessed, ConcurrentHashMap locks only small segments of the map. So, multiple
threads can access different parts of it at the same time. This makes it much faster and more
efficient, especially when there's a lot of activity going on.
3.How does a synchronized block work in Java? What's the difference between class - level
and instance - level locks?
A synchronized block uses an object's monitor to ensure only one thread can execute it at a time.
Instance-level locks use the instance's monitor (e.g., synchronized(this)), so each object instance is
locked separately. Class-level locks use the Class object's monitor
(e.g., synchronized(MyClass.class)), which affects all instances of the class. Use class-level locks for
static methods or global state that needs protection across all instances.
4.Besides Synchronized, what other concurrency tools would you use in Java?
For concurrency, I usually use:
ReentrantLock for advanced locking features like timed waits or interruptible locks
CountDownLatch to wait for multiple threads to complete (e.g., initializing resources)
ExecutorService for thread pooling and managing task queues
I use CompletableFuture mainly for handling asynchronous tasks and combining results. For
example, when rendering a product detail page, there are multiple independent data sources like
product information, inventory, and price. I use CompletableFuture to get these data concurrently.
5.Do you know about ThreadLocal? What's its purpose and how to use it in a multi-threaded
environment?
ThreadLocal provides thread-local storage, meaning each thread gets its own copy of a variable. Its
main use is to avoid sharing state between threads, which helps prevent concurrency issues.
For example, in a web app, you might use ThreadLocal to store a user's session data. This ensures
that each request-handling thread operates on its own context without interfering with others.
6.Are you familiar with Git? How do you usually handle merge conflicts in your projects?
Git is part of my daily workflow. When I hit a merge conflict, I just follow my usual setps.
First, I run git status to see which files are causing trouble. Then I open them in intelliJ to show both
versions side by side.
For example, if two branches changed the same method, I’ll read through both changes to see what
each team member was trying to do. Sometimes one change is clearly right, so I’ll just keep that.
Other times, I need to mix and match to solve conflict.
I’ll test the code to make sure nothing broke.Then, I add the file and commit with a message
like "Fixed merge conflict in order process flow".
The key is to communicate with the team if I’m not sure, and always test before pushing.
No big deal, just part of the job.
7.What's the difference between unit tests and integration tests? In your actual work, what
tools or frameworks do you usually use in with them?
Unit tests focus on testing individual components or functions in isolation. They're fast and easy to
write. Integration tests, on the other hand, check how different components work together.
In Java, for unit tests, I often use JUnit 5 and Spring Boot test support. It's super popular and easy to
use. Usually, I run tests with JUnit 5 for core logic, Mockito for mocking dependencies, and
Testcontainers for DAO or repository layer tests. For integration testing: I usually validate APIs using
WebTestClient and Postman for API contract testing.