Basics Flashcards
How to determine length/size for objects?
size() is a method specified in java.util.Collection, which is then inherited by every data structure in the standard library. length is a field on any array (arrays are objects, you just don't see the class normally) length() is a method on java.lang.String
diff between sleep and wait
wait() is used for thread synchronization.
It can be called on any object, as it’s defined right on java.lang.Object, but it can only be called from a synchronized block. It releases the lock on the object so that another thread can jump in and acquire a lock.
Wait is used for inter-thread communication while sleep is used to introduce pause on execution.
How can you group objects by a certain value in java
Lambdas
Map> postsPerType = posts.stream()
.collect(groupingBy(BlogPost::getType));
ACID in databases
Atomic
Consistent(it must conform to existing constraints in the database)
Isolated ( it must not affect other transactions) and durable (it must get written to persistent storage).
What is a database transaction
Any logical unit of work. Multiple operations may be needed to perform a transaction
What is REST
REST stands forRepresentationalStateTransfer. (It is sometimes spelled “ReST”.) It relies on a stateless, client-server, cacheable communications protocol – and in virtually all cases, the HTTP protocol is used.
REST isan architecture stylefor designing networked applications. The idea is that, rather than using complex mechanisms such as CORBA, RPC or SOAP to connect between machines, simple HTTP is used to make calls between machines.
- In many ways, the World Wide Web itself, based on HTTP, can be viewed as a REST-based architecture.
RESTful applications use HTTP requests to post data (create and/or update), read data (e.g., make queries), and delete data. Thus, REST uses HTTP for all four CRUD (Create/Read/Update/Delete) operations.
REST is a lightweight alternative to mechanisms like RPC (Remote Procedure Calls) and Web Services (SOAP, WSDL, et al.). Later, we will see how much more simple REST is.
- Despite being simple, REST is fully-featured; there’s basically nothing you can do in Web Services that can’t be done with a RESTful architecture.
REST is not a “standard”.
what does \s represent?
\s - matches single whitespace character. \s+ - matches sequence of one or more whitespace characters
Indexing in DB
Indexing in Databases
Indexing is a way to optimize performance of a database by minimizing the number of disk accesses required when a query is processed.
An index or database index is a data structure which is used to quickly locate and access the data in a database table.
Indexes are created using some database columns.
The first column is the Search key that contains a copy of the primary key or candidate key of the table. These values are stored in sorted order so that the corresponding data can be accessed quickly (Note that the data may or may not be stored in sorted order).
The second column is the Data Reference which contains a set of pointers holding the address of the disk block where that particular key value can be found.
There are two kinds of indices:
Ordered indices: Indices are based on a sorted ordering of the values.
Hash indices: Indices are based on the values being distributed uniformly across a range of buckets. The buckets to which a value is assigned is determined by function called a hash function.
There is no comparison between both the techniques, it depends on the database application on which it is being applied.
Access Types: e.g. value based search, range access, etc.
Access Time: Time to find particular data element or set of elements.
Insertion Time: Time taken to find the appropriate space and insert a new data.
Deletion Time: Time taken to find an item and delete it as well as update the index structure.
Space Overhead: Additional space required by the index
What is Domain Driven Design?
DDD focuses on three core principles:
Focus on the core domain and domain logic.
Base complex designs on models of the domain.
Constantly collaborate with domain experts, in order to improve the application model and resolve any emerging domain-related issues.
What is a memory leak?
A Memory Leak is a situation when there are objects present in the heap that are no longer used, but the garbage collector is unable to remove them from memory and, thus they are unnecessarily maintained.
memory leak analysis — it’s more a debugging practice than something we would like to keep in the production code. Calling System.gc() and seeing heap space still being high might be an indication of a memory leak.
Causes of memory leak
Some causes:
- Usage of a lot of static variables. these end up existing during the entire duration of the application and therefore consume resources
- close resources properly(use finally , or try with resources)
- Improper equals and hashcode implementation. If we use a custom object as a key in hashmap without proper equals and hashcode then the memory utilized may rise
What are thread pools
A thread pool reuses previously created threads to execute current tasks and offers a solution to the problem of thread cycle overhead and resource thrashing. Since the thread is already existing when the request arrives, the delay introduced by thread creation is eliminated, making the application more responsive.
What are futures in Java
Future class represents a future result of an asynchronous computation – a result that will eventually appear in the Future after the processing is complete.
What is throttling
It is a way to restrict the rate at which api’s can process requests per sec/ minute or ….. Spring boot has a throttling library. Java has a bucket4J library
Convert List of integers to int[] in java
result.stream().mapToInt(Integer:: intValue).toArray();