In your collection implementation how you will make sure the unique values without implementing set/hashSet interface? otherway how you implement set interface in your java collection data?

For example


public class Test {
    
    public static void main(String[] args)
    {
       
        HashSet<Object> testSet= new HashSet<Object>();
        testSet.add(100);
        testSet.add("hello");
        System.out.println("Result "+testSet);
    }
}

The result will be : Result [100, hello ] 

Now we will add some duplicate elements in the same collection list


public class Test {
    
    public static void main(String[] args)
    {
       
        HashSet<Object> testSet= new HashSet<Object>();
        testSet.add(100);
        testSet.add("hello");
        testSet.add(100);
        testSet.add("hello");
System.out.println("Result "+testSet); } }

The result will be : Result [100, hello ] 

Eventhough when we add duplicate element in this collection set with add method why the duplicated data not inserted? because add method returs false when we try to add duplicate elements. How the add method returns false?
The HashSet implementation of the add() method in java is internally implemented by HashMap implementation.

Whenever we tries to create an object of HashSet , it internally create an object of HashMap 

public class HashSet
extends AbstractSet
implements Set, Cloneable, java.io.Serializable

{
    private transient HashMap map;
           
    private static final Object PRESENT = new Object();
        
                public HashSet() {
                      map = new HashMap<>();
                    }
    
       
               public boolean add(E e) {
                       return map.put(e, PRESENT)==null;
                   }
    
       

}


The key is unique in hashmap , when we are calling add(Element E) this element E will be added as key in HashMap and the value for key will be added by the java developer api by using the variable PRESENT , it will create ( new Object() ) which is a dummy value referred by Object reference PRESENT

when you are adding a line in HashSet like  hashset.add(100)   what java does internally is that it will put that element E here 100 as a key in the HashMap(created during HashSet object creation) and some dummy value that is Object's object is passed as a value to the key .

The main point to notice in above code is that put (key,value) will return

1.  null , if key is unique and added to the map
2.  Old Value of the key , if key is duplicate

So , in HashSet add() method ,  we check the return value of map.put(key,value) method with null value
i.e.

   public boolean add(E e) {
            return map.put(e, PRESENT)==null;
       }

So , if map.put(key,value) returns null ,then
map.put(e, PRESENT)==null      will return true and element is added to the HashSet.

So , if map.put(key,value) returns old value of the key ,then
map.put(e, PRESENT)==null      will return false and element is  not added to the HashSet .

What is a Collection? 
• An object that groups multiple elements into a single unit.
 • Sometimes called a container






     img courtesy - http://javahungry.blogspot.com/

There are two ways to traverse collections:
1) using Iterators.
2) with the (enhanced) for-each construct

Iterators 

• An Iterator is an object that enables you to traverse through a collection and to remove elements from the collection selectively, if desired.

• You get an Iterator for a collection by calling its iterator method.

• Suppose collection is an instance of a Collection. Then to print out each element on a separate line:

Iterator it = collection.iterator(); 
while (it.hasNext())
 System.out.println(it.next());

 • Note that next() does two things:
                    1. Returns the current element (initially the first element)
                    2. Steps to the next element and makes it the current element. 



    Levels of Abstraction 

• Recall that Java supports three levels of abstraction: –

 - Interface 

             • Java expression of an ADT
             • Includes method declarations with arguments of specified types, but with empty bodies


 – Abstract Class 

             • Implements only a subset of an interface.
             • Cannot be used to instantiate an object. 


– (Concrete) Classes 

            • May extend one or more abstract classes
            • Must fully implement any interface it implements 
            • Can be used to instantiate objects. 


Iterating Over a Map 

• Because Map does not extend Iterable, but Collection (hence Set) does extend Iterable, you can (only) iterate over a Map using one of its three views:
           – Keys: Set keySet() 
           – Values: Collection values() 
           – Pairs: Set> entrySet()


 What is the root interface in collection hierarchy ? 

Root interface in collection hierarchy is Collection interface . Few interviewer may argue that 
Collection interface extends Iterable interface. So iterable should be the root interface. But you should reply iterable interface present in java.lang package not in java.util package .It is clearly mentioned in Oracle Collection  docs , that Collection interface is a member of the Java Collections framework.  For Iterable interface Oracle doc , iterable interface is not mentioned as a part of the Java Collections framework .So if the question includes  collection hierarchy , then you should answer the question as Collection interface (which is found in java.util package).

 What is the difference between Collection and Collections ?

Collection is  an interface while Collections is a java class , both are present in java.util package and  part of java collections framework.

 Which collection classes are synchronized or thread-safe ?

Stack, Properties , Vector and Hashtable can be used in multi threaded environment because they are synchronized classes (or thread-safe). 
Camel Book in one page

  Camel books Camel in action


 It enables easy integration of different applications which use several protocols and technologies.Synchronous remote procedure calls or asynchronous messaging is used to communicate via several technologies such as RMI, SOAP Web Services, REST or JMS. A lot of software silos exists. Nevertheless, all applications and products of these decades have to communicate with each other to work together perfectly.

What is Apache Camel?
Apache Camel is a lightweight integration framework which implements all EIPs. Thus, you can easily integrate different applications using the required patterns. You can use Java, Spring XML, Scala or Groovy. Almost every technology you can imagine is available, for example HTTP, FTP, JMS, EJB, JPA, RMI, JMS, JMX, LDAP, Netty, and many, many more (of course most ESBs also offer support for them). Besides, own custom components can be created very easily.
You can deploy Apache Camel as standalone application, in a web container (e.g. Tomcat or Jetty), in a JEE application Server (e.g. JBoss AS or WebSphere AS), in an OSGi environment or in combination with a Spring container.
If you need more information about Apache Camel, please go to its web site as starting point: http://camel.apache.org. This article is no technical introduction J
When to use Apache Camel?
Apache Camel is awesome if you want to integrate several applications with different protocols and technologies. Why? There is one feature (besides supporting so many technologies and besides supporting different programming languages)  Every integration uses the same concepts! No matter which protocol you use. No matter which technology you use. No matter which domain specific language (DSL) you use – it can be Java, Scala, Groovy or Spring XML. You do it the same way. Always! There is a producer, there is a consumer, there are endpoints, there are EIPs, there are custom processors / beans (e.g. for custom transformation) and there are parameters (e.g. for credentials).

 What is the difference between enrich() and pollEnrich()?

  Content enricher here  ( Link)

 Content enrichment using the enrich DSL element Camel comes with two flavors of content enricher in the DSL enrich pollEnrich enrich uses a Producer to obtain the additional data. It is usually used for Request Reply messaging, for instance to invoke an external web service. pollEnrich on the other hand uses a Polling Consumer to obtain the additional data. It is usually used for Event Message messaging, for instance to read a file or download a FTP file.

  • enrich assumes you want to use an incoming Exchange as a parameter to an another service request. for example, your incoming Exchange could be a userID, but you really need the entire User object, so you could enrich it by passing the userID to a REST service that returns the User object which becomes the Exchange, etc.
  • pollEnrich assumes the incoming Exchange is a simple trigger that tell a PollingConsumer to look for data and create an Exchange (ignoring the contents of the incoming Exchange). For example, you could have a timer or other business process event that requires picking up a file for processing, etc. that said, the incoming Exchange data is NOT used to dynamically configure the PollingConsumer...only the URI is used for this.
What are different message exchange patterns in Camel, which one of these is synchronous?
A: There are two types of message exchange patterns in Camel:
  • In Only: In this exchange pattern, consumer creates a exchange which only contains an In message.
  • In Out: In this exchange pattern, consumer creates a exchange which also contains reply message for the caller.
Out of above two, InOut pattern is synchronous because caller expects for an acknowledgement or reply for each exchange sent.

What is camel exchange? Explain its structure?
A: A Camel Exchange can be called as an holder for holding message during routing. A exchange supports various message exchange patterns, like InOnly and InOut.
Following are the contents of camel exchange:
  • ExchangeId: A unique identifier for each exchange
  • Message Exchange Pattern: This field denotes whether you are using InOnly or InOut exchange pattern.
  • Exception: If any exception occurs during routing, it will be available in this field.
  • Properties: These are the properties that are available for entire duration of exchange.
  • In Message: This is mandatory field which contains the input message.
  • Out Message: This is optional message which exists only if MEP is InOut.