JavaRush /Java Blog /Random EN /Coffee break #167. Things to review before a Java intervi...

Coffee break #167. Things to review before a Java interview. What is the difference between enum and iterator in Java?

Published in the Random EN group

Things to review before a Java interview

Source: Medium In this article, you will find 17 tips from an experienced developer that will help you in a Java interview. Coffee break #167.  Things to review before a Java interview.  What is the difference between enum and iterator in Java?  - 1I've worked in the IT industry for over 20 years and spent most of that time working with Java. In addition, I gained considerable experience interviewing Java developers. Although the Java language is constantly evolving, the core concepts remain largely the same. Here are some tips I want to share with aspiring developers before they go for an interview. If you repeat them, it will save you from serious mistakes.
  1. The Object class is at the root of the Java class hierarchy. All Java classes trace back to Object . Even if a class doesn't explicitly extend any class, it extends Object . However, a class is free to extend Object explicitly.

  2. In Java, you can extend only one class (multiple inheritance is not allowed due to ambiguity). However, a class can implement any number of interfaces at the same time.

  3. An interface extends another interface (as opposed to an implementation).

  4. There are four access modifiers in Java: public (available to everyone), protected (available only to subclasses), private (available only within a single class), default (available within a single package). It should be noted that subclasses of the same class can be in different packages. The parent class and subclasses do not have to be part of the same package.

  5. The class string is immutable. Immutability means that the String class itself does not provide any methods for replacing the value in a String reference . If you want to replace the value of a string reference, you must assign the value explicitly using the = operator . Compare this to the StringBuffer or StringBuilder classes , which have methods like append so you don't have to use the = operator there .

  6. ConcurrentHashMap is more efficient than Hashtable . ConcurrentHashMap operates on segments of the underlying data structure, in which a write operation locks only a specific segment (regardless of which segment the key belongs to). However, in Hashtable the entire data structure will be locked.

  7. ConcurrentHashMap is slower than HashMap because HashMap does not implement thread safety. HashMap may throw a ConcurrentModificationException if a thread iterates over a HashMap and another thread attempts to modify the same HashMap . ConcurrentHashMap will not throw an exception here.

  8. How to implement equality of two objects of the same class that you have defined? Answer: This can be done by overriding the hashcode() method .

  9. What is the default result of the toString() method? Answer: It is the concatenation of the class name, the @ sign and the hashcode() value .

  10. How to implement polymorphism in Java? One way to do this is to overload the method. Another way is to override the method.

  11. How do you call a superclass constructor from a child class? Answer: This can be done using the super() keyword . The super() method without arguments is always called, even if it is not explicitly specified. The super() method with an argument must be specified explicitly. A call to super() (with or without an argument) must always be the first line in the child class's constructor if it is required to be called.

  12. What are checked and unchecked exceptions? Answer: Checked exceptions are those that must be declared or caught in the method where they are expected to be thrown. An unchecked exception does not have this limitation. java.io.IOException is an example of a checked exception. Unchecked exceptions come from the RunTimeException class .

  13. The root class of the exception hierarchy is Throwable (which in turn implicitly extends Object ). Exception and Error come from Throwable .

  14. Since Java 8, methods can have an implementation in an interface. Default methods and static methods can have implementations.

  15. A class that qualifies as abstract cannot be instantiated. Any class that does not provide a body for any of the methods must be declared abstract. A developer can declare a class to be abstract even if all methods have a body - however, this is not very recommended because in this case the class cannot be instantiated.

  16. The final class cannot be extended. A final variable cannot be assigned another value. A final method cannot be overridden.

  17. What keywords are required in a try-catch-finally construct ? This could be try-catch , try-finally , or all three. In this case, catch is not a required keyword.

What is the difference between enum and iterator in Java?

Source: Rrtutors This post brought to you discusses the differences between enumeration and iteration in Java. The Java.util package provides two interfaces for traversing the elements of a Collection object : Enumeration and Iterator . Even though they both pass through a Collection object , there are some differences between them.

Differences between enum and iterator

  • Time added to JDK: They are introduced at different times. Enum was introduced in JDK 1.0, while iterator was introduced in JDK 1.2.

  • Removing elements: This is the main difference between the two. In the Iterator interface , we can remove an element when iterating over a Collection object , whereas we cannot change it when iterating over a Collection object using Enumeration . This is because the Iterator interface has a remove() method, but the Enumeration interface does not.

  • Operation type: Iterator has operation type fail-fast, and enumeration has fail-safe operation type. As a result, Iterator throws a ConcurrentModificationException when the collection is modified during iteration unless its own remove() method is used , while Enumeration does not throw any exception when the collection is modified during iteration.

Enumeration and Iterator Examples in Java

Enumeration example

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Enumeration;

import java.util.List;

import java.util.Vector;

public class Enumeration_Example {

      public static void main(String[] args) {

                  List laptoplist = new ArrayList(Arrays.asList( new String[] {"Samsung", "Lenovo", "Apple", "HP"}));

            Vector vectali = new Vector(laptoplist);

            delete(vectali, "Samsung");

        }

        private static void delete(Vector vectali, String laptop) {

            Enumeration lapi = vectali.elements();

            while (lapi.hasMoreElements()) {

              String s = (String) lapi.nextElement();

              if (s.equals(laptop)) {

                  vectali.remove(laptop);

              }

            }

            System.out.println("The Laptop brands includes:");

            lapi = vectali.elements();

            while (lapi.hasMoreElements()) {

              System.out.println(lapi.nextElement());

            }

      }

}
Conclusion:
The Laptop brands includes: Lenovo Apple HP

Iterator example:

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Iterator;

import java.util.List;

import java.util.Vector;

public class Iterator_example {

      public static void main(String[] args) {

                  List laptoplist = new ArrayList(Arrays.asList( new String[] {"Samsung", "Lenovo", "HP", "Apple"}));

            Vector vectali = new Vector(laptoplist);

            delete(vectali, "HP");

        }

        private static void delete(Vector vectali, String name) {

            Iterator a = vectali.iterator();

            while (a.hasNext()) {

              String s = (String) a.next();

              if (s.equals(name)) {

                  a.remove();

              }

            }

            // Display the names

            System.out.println("The laptop brand includes:");

            a = vectali.iterator();

            while (a.hasNext()) {

              System.out.println(a.next());

            }

      }

}
Conclusion:
The laptop brand includes: Samsung Lenovo Apple
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION