This question does not have any answers yet. In the meantime we have included some related questions and answers below.
Profile photo for Sai Karthik Golagani

1.Need for Serializable interface:An object created in java may sometimes have to travel across the network to another remote network area.So in order to facilitate this, every collection object implements Serializable Interface.

To facilitate the above process, every object should implement Serializable interface.

2.Need for Cloneable Interface: After travelling across the network, the object may undergo unforeseen corruption,so they make a shallow copy of the object that and work on it to check for any corrupted data.

Cloneable Interface implements Clone() which makes a shallow copy of the ob

1.Need for Serializable interface:An object created in java may sometimes have to travel across the network to another remote network area.So in order to facilitate this, every collection object implements Serializable Interface.

To facilitate the above process, every object should implement Serializable interface.

2.Need for Cloneable Interface: After travelling across the network, the object may undergo unforeseen corruption,so they make a shallow copy of the object that and work on it to check for any corrupted data.

Cloneable Interface implements Clone() which makes a shallow copy of the object.This prevents the loss of time and data.So every collection class implements cloneable interface too.

My jargon is not up to the mark,i tried to answer at a low level to make it more comprehensible.

P.S:Correct me if i'm wrong

Profile photo for Shweta Singh

First of all, linked list is a part of data structure. You can implement it in any language like c/c++. Memory is a crucial resource, you know about it, right? simply, because every application ask for it.

So, let's say, there's a guy called 'X' who manage the memory and there's a programmer called 'Y'.

Now, let's hear the conversation carefully between both of them and at the end, you will definitely get what I'm trying to convey.

Y: Hey, what's up? Hope you are doing good. I want to store a list of integers(array) and maximum number of integers in this list will be 4. Can you please assign it i

First of all, linked list is a part of data structure. You can implement it in any language like c/c++. Memory is a crucial resource, you know about it, right? simply, because every application ask for it.

So, let's say, there's a guy called 'X' who manage the memory and there's a programmer called 'Y'.

Now, let's hear the conversation carefully between both of them and at the end, you will definitely get what I'm trying to convey.

Y: Hey, what's up? Hope you are doing good. I want to store a list of integers(array) and maximum number of integers in this list will be 4. Can you please assign it into memory?

X: Ok, I need to give you contiguous block of memory of 16 bytes. Since, one block will take 4 bytes, so 4 blocks will take 16 bytes.

---------- After few months ----------

Y: Now, I want to store 5th element in the same array. So, can you please extend the same block of array?

X: Sorry, I didn't expect any extension. So, I can't give you memory because I've other variable next to your block.

Y: You can't do this to me because I need memory. So, what all options do I have?

X: You can give me new size and I will create new block of memory and copy all the previous elements in new block. This will cost you more.

Y: still sad :( because what if I need extension again in future, also I'm not using remaining portion of memory. I need solution.

So, the solution of this problem is LINKED LIST.

Now, this time, Y will ask to memory manager X for 1 unit of data at a time instead of array. For eg, to store 1,5,3,4, he will make separate request to first store 1, then 5, then 3 and so on.

Since, X will make separate request, he may or may not get adjacent memory. In this way, he will get disjoint non-contiguous block of memory. So, we need to link these blocks somehow and for that, we will store extra information with each block.

In one part of block, we will store value and in other part, we will store address of next block. In last block, we will store 0 which indicates end of list.

Reason to use linked list:

  1. Cost of accessing elements: In array, it is O(1), while, in linked list, it is O(n).
  2. Memory Requirement: In array, there may be unused memory but linked list needs extra memory and won't have unused memory.
  3. Insertion and deletion operations are comparatively easy in linked list.
  4. And most importantly, linked list is dynamic.

I hope you get your answer.

Image Source: Shweta Singh (श्वेता सिंह) .

Run payroll as often as you need. Gusto doesn’t charge extra to file taxes or run off-cycle payrolls.
Profile photo for Assistant
Assistant

The LinkedList class in Java implements the Serializable and Cloneable interfaces for several reasons related to its functionality and usability:

1. Serializable Interface

  • Purpose: The Serializable interface enables the serialization of an object, which is the process of converting an object into a byte stream. This is useful for saving the state of an object to a file or sending it over a network.
  • Usage in LinkedList: By implementing Serializable, a LinkedList can be easily saved and restored. This is particularly useful in scenarios such as:
  • Persistence: Saving the state of a program so that it

The LinkedList class in Java implements the Serializable and Cloneable interfaces for several reasons related to its functionality and usability:

1. Serializable Interface

  • Purpose: The Serializable interface enables the serialization of an object, which is the process of converting an object into a byte stream. This is useful for saving the state of an object to a file or sending it over a network.
  • Usage in LinkedList: By implementing Serializable, a LinkedList can be easily saved and restored. This is particularly useful in scenarios such as:
  • Persistence: Saving the state of a program so that it can be restored later.
  • Distributed Systems: Sending a LinkedList over a network in a distributed application.

2. Cloneable Interface

  • Purpose: The Cloneable interface allows objects to be cloned, meaning that a new object can be created that is a copy of the original object.
  • Usage in LinkedList: By implementing Cloneable, a LinkedList can be duplicated using the clone() method. This is beneficial for:
  • Creating Copies: When you need a separate instance of the list with the same elements, without affecting the original list.
  • Performance: Cloning can sometimes be more efficient than creating a new list and copying elements one by one.

Conclusion

In summary, implementing Serializable and Cloneable enhances the flexibility and usability of the LinkedList class, allowing it to be easily saved, transmitted, and copied, which are common requirements in many Java applications.

Profile photo for Quora User

Collection is an interface that specifies a group of objects known as elements. The details of how the group of elements is maintained is left up to the concrete implementations of Collection . ... So mandating cloning and serialization in all implementations is actually less flexible and more restrictive.

Profile photo for Abhilash MR

Serialization in Java

Serialization in Java is a mechanism of writing the state of an object into a byte stream.

It is mainly used in Hibernate, RMI, JPA, EJB and JMS technologies.

The reverse operation of serialization is called deserialization.

Advantages of Java Serialization

It is mainly used to travel object's state on the network (which is known as marshaling).

java.io.Serializable

Serializable is a marker interface (has no data member and method). It is used to "mark" Java classes so that objects of these classes may get the certain capability. The Cloneable and Remote are also marker interfac

Serialization in Java

Serialization in Java is a mechanism of writing the state of an object into a byte stream.

It is mainly used in Hibernate, RMI, JPA, EJB and JMS technologies.

The reverse operation of serialization is called deserialization.

Advantages of Java Serialization

It is mainly used to travel object's state on the network (which is known as marshaling).

java.io.Serializable

Serializable is a marker interface (has no data member and method). It is used to "mark" Java classes so that objects of these classes may get the certain capability. The Cloneable and Remote are also marker interfaces.

It must be implemented by the class whose object you want to persist.

The String class and all the wrapper classes implement the java.io.Serializable interface by default.

Let's see the example given below:

  1. import java.io.Serializable; 
  2. public class Student implements Serializable{ 
  3. int id; 
  4. String name; 
  5. public Student(int id, String name) { 
  6. this.id = id; 
  7. this.name = name; 
  8. } 
  9. } 

In the above example, Student class implements Serializable interface. Now its objects can be converted into stream.


ObjectOutputStream class

The ObjectOutputStream class is used to write primitive data types, and Java objects to an OutputStream. Only objects that support java.io.Serializable interface can be written to streams.

Constructor

  1. public ObjectOutputStream(OutputStream out) throws IOException {} 

creates an ObjectOutputStream that writes to the specified OutputStream.

Important Methods

Description

  1. public final void writeObject(Object obj) throws IOException {} 

writes the specified object to the ObjectOutputStream.

  1. public void flush() throws IOException {} 

flushes the current output stream.

  1. public void close() throws IOException {} 

closes the current output stream.


Example of Java Serialization

In this example, we are going to serialize the object of Student class. The writeObject() method of ObjectOutputStream class provides the functionality to serialize the object. We are saving the state of the object in the file named f.txt.

  1. import java.io.*; 
  2. class Persist{ 
  3. public static void main(String args[])throws Exception{ 
  4. Student s1 =new Student(101,"ram"); 
  5. FileOutputStream fout=new FileOutputStream("f.txt"); 
  6. ObjectOutputStream out=new ObjectOutputStream(fout); 
  7. out.writeObject(s1); 
  8. out.flush(); 
  9. System.out.println("success"); 
  10. } 
  11. } 
  12.  
  13. /* 
  14. success 
  15. */ 

Where do I start?

I’m a huge financial nerd, and have spent an embarrassing amount of time talking to people about their money habits.

Here are the biggest mistakes people are making and how to fix them:

Not having a separate high interest savings account

Having a separate account allows you to see the results of all your hard work and keep your money separate so you're less tempted to spend it.

Plus with rates above 5.00%, the interest you can earn compared to most banks really adds up.

Here is a list of the top savings accounts available today. Deposit $5 before moving on because this is one of th

Where do I start?

I’m a huge financial nerd, and have spent an embarrassing amount of time talking to people about their money habits.

Here are the biggest mistakes people are making and how to fix them:

Not having a separate high interest savings account

Having a separate account allows you to see the results of all your hard work and keep your money separate so you're less tempted to spend it.

Plus with rates above 5.00%, the interest you can earn compared to most banks really adds up.

Here is a list of the top savings accounts available today. Deposit $5 before moving on because this is one of the biggest mistakes and easiest ones to fix.

Overpaying on car insurance

You’ve heard it a million times before, but the average American family still overspends by $417/year on car insurance.

If you’ve been with the same insurer for years, chances are you are one of them.

Pull up Coverage.com, a free site that will compare prices for you, answer the questions on the page, and it will show you how much you could be saving.

That’s it. You’ll likely be saving a bunch of money. Here’s a link to give it a try.

Consistently being in debt

If you’ve got $10K+ in debt (credit cards…medical bills…anything really) you could use a debt relief program and potentially reduce by over 20%.

Here’s how to see if you qualify:

Head over to this Debt Relief comparison website here, then simply answer the questions to see if you qualify.

It’s as simple as that. You’ll likely end up paying less than you owed before and you could be debt free in as little as 2 years.

Missing out on free money to invest

It’s no secret that millionaires love investing, but for the rest of us, it can seem out of reach.

Times have changed. There are a number of investing platforms that will give you a bonus to open an account and get started. All you have to do is open the account and invest at least $25, and you could get up to $1000 in bonus.

Pretty sweet deal right? Here is a link to some of the best options.

Having bad credit

A low credit score can come back to bite you in so many ways in the future.

From that next rental application to getting approved for any type of loan or credit card, if you have a bad history with credit, the good news is you can fix it.

Head over to BankRate.com and answer a few questions to see if you qualify. It only takes a few minutes and could save you from a major upset down the line.

How to get started

Hope this helps! Here are the links to get started:

Have a separate savings account
Stop overpaying for car insurance
Finally get out of debt
Start investing with a free bonus
Fix your credit

Profile photo for Stephan O.

Please read carefully the LinkedList documentation.

Sure, in section “Method Summary” there is no method isEmpty().

But in section “Methods declared in class java.util.AbstractCollection” you can find it:

LinkedList doesn’t implement the isEmpty() function by it self but inherit it from AbstractCollection.

The implementation relies on the abstract method size() and looks like:

  1. public boolean isEmpty() { 
  2. return size() == 0; 
  3. } 

See

http://hg.openjdk.java.net/jdk8u/jdk8u-dev/jdk/file/c5d02f908fb2/src/share/classes/java/util/AbstractCollection.java

This is the Java 8 implementation but I think this doesn’t matter here.

So LinkedList provides a size() method and gets the is

Please read carefully the LinkedList documentation.

Sure, in section “Method Summary” there is no method isEmpty().

But in section “Methods declared in class java.util.AbstractCollection” you can find it:

LinkedList doesn’t implement the isEmpty() function by it self but inherit it from AbstractCollection.

The implementation relies on the abstract method size() and looks like:

  1. public boolean isEmpty() { 
  2. return size() == 0; 
  3. } 

See

http://hg.openjdk.java.net/jdk8u/jdk8u-dev/jdk/file/c5d02f908fb2/src/share/classes/java/util/AbstractCollection.java

This is the Java 8 implementation but I think this doesn’t matter here.

So LinkedList provides a size() method and gets the isEmpty() method for free.

Profile photo for Gaive Gandhi

I presume this question is pertaining to the java programming language.

No, LinkedList is not thread safe or by default it is not synchronized in java.

LinkedList implements the List and Deque interfaces to have a doubly LinkedList implementation.

If multiple threads access a linked list concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more elements; merely setting the value of an element is not a structural modification.) This is typically accomplished by synch

I presume this question is pertaining to the java programming language.

No, LinkedList is not thread safe or by default it is not synchronized in java.

LinkedList implements the List and Deque interfaces to have a doubly LinkedList implementation.

If multiple threads access a linked list concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more elements; merely setting the value of an element is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the list. If no such object exists, the list should be "wrapped" using the Collections.synchronizedList method.

  1. List list = Collections.synchronizedList(new LinkedList(...)); 

The ConcurrentLinkedQueue implementation is a default synchronized queue implementation using LinkedList

Am I the only one who never knew this before?
Profile photo for Deals24/7-365days

In Java, the Serializable interface is a marker interface that indicates that the objects of the class implementing it can be serialized. Serialization is the process of converting an object into a byte stream, which can be saved to a file or sent over a network. Deserialization is the process of reconstructing the object from the byte stream.

Here's the syntax for the Serializable interface:

  1. javaCopy codeimport java.io.Serializable; 
  2.  
  3. public class MyClass implements Serializable { 
  4. // Class members and methods 
  5. // ... 
  6. } 

In this example:

  • MyClass is a class that implements the Serializable interfac

In Java, the Serializable interface is a marker interface that indicates that the objects of the class implementing it can be serialized. Serialization is the process of converting an object into a byte stream, which can be saved to a file or sent over a network. Deserialization is the process of reconstructing the object from the byte stream.

Here's the syntax for the Serializable interface:

  1. javaCopy codeimport java.io.Serializable; 
  2.  
  3. public class MyClass implements Serializable { 
  4. // Class members and methods 
  5. // ... 
  6. } 

In this example:

  • MyClass is a class that implements the Serializable interface.
  • The Serializable interface does not declare any methods; it acts as a marker to indicate that the class is intended to be serializable.

Here's a more detailed example:

  1. javaCopy codeimport java.io.*; 
  2.  
  3. class Person implements Serializable { 
  4. private String name; 
  5. private int age; 
  6.  
  7. public Person(String name, int age) { 
  8. this.name = name; 
  9. this.age = age; 
  10. } 
  11.  
  12. public String getName() { 
  13. return name; 
  14. } 
  15.  
  16. public int getAge() { 
  17. return age; 
  18. } 
  19. } 
  20.  
  21. public class SerializationExample { 
  22. public static void main(String[] args) { 
  23. Person person = new Person("John Doe", 30); 
  24.  
  25. // Serialization 
  26. try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("person.ser"))) { 
  27. out.writeObject(person); 
  28. System.out.println("Object has been serialized"); 
  29. } catch (IOException e) { 
  30. e.printStackTrace(); 
  31. } 
  32.  
  33. // Deserialization 
  34. try (ObjectInputStream in = new ObjectInputStream(new FileInputStream("person.ser"))) { 
  35. Person deserializedPerson = (Person) in.readObject(); 
  36. System.out.println("Object has been deserialized"); 
  37. System.out.println("Name: " + deserializedPerson.getName()); 
  38. System.out.println("Age: " + deserializedPerson.getAge()); 
  39. } catch (IOException | ClassNotFoundException e) { 
  40. e.printStackTrace(); 
  41. } 
  42. } 
  43. } 

In this example:

  • The Person class implements Serializable.
  • An instance of Person is serialized to a file (person.ser) and then deserialized back into a new instance.
  • Note the use of ObjectOutputStream for serialization and ObjectInputStream for deserialization.
Profile photo for Mukesh Thakur

Linked list is not a part of C++, it is a type of data structure like tree and graph. We need link list in many scenarios, like when we dont know how big our list is going to be, in this case we need to add nodes dynamically.

This search engine can reveal so much. Click here to enter any name, wait for it, brace yourself.
Profile photo for Richard Morris

It does.

Here is a simple JUnit test.

  1. @Test 
  2. void testLinkedList() { 
  3. List<String> list = new LinkedList<>(); 
  4. assertTrue(list.isEmpty()); 
  5. list.add("One"); 
  6. assertFalse(list.isEmpty()); 
  7. } 

It passes.

Now if we look at the documentation

LinkedList (Java SE 11 & JDK 11 )
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void add ​(int index, E element) Inserts the specified element at the specified position in this list. boolean add ​( E e) Appends the specified element to the end of this list. boolean addAll ​(int index, Collection <? extends E > c) Inserts all of the elements in the specified collection into this list, starting at the specified position. boolean addAll ​( Collection <? extends E > c) Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator. void addFirst ​( E e) Inserts the specified element at the beginning of this list. void addLast ​( E e) Appends the specified element to the end of this list. void clear () Removes all of the elements from this list. Object clone () Returns a shallow copy of this LinkedList . boolean contains ​( Object o) Returns true if this list contains the specified element. Iterator < E > descendingIterator () Returns an iterator over the elements in this deque in reverse sequential order. E element () Retrieves, but does not remove, the head (first element) of this list. E get ​(int index) Returns the element at the specified position in this list. E getFirst () Returns the first element in this list. E getLast () Returns the last element in this list. int indexOf ​( Object o) Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. int lastIndexOf ​( Object o) Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element. ListIterator < E > listIterator ​(int index) Returns a list-iterator of the elements in this list (in proper sequence), starting at the specified position in the list. boolean offer ​( E e) Adds the specified element as the tail (last element) of this list. boolean offerFirst ​( E e) Inserts the specified element at the front of this list. boolean offerLast ​( E e) Inserts the specified element at the end of this list. E peek () Retrieves, but does not remove, the head (first element) of this list. E peekFirst () Retrieves, but does not remove, the first element of this list, or returns null if this list is empty. E peekLast () Retrieves, but does not remove, the last element of this list, or returns null if this list is empty. E poll () Retrieves and removes the head (first element) of this list. E pollFirst () Retrieves and removes the first element of this list, or returns null if this list is empty. E pollLast () Retrieves and removes the last element of this list, or returns null if this list is empty. E pop () Pops an element from the stack represented by this list. void push ​( E e) Pushes an element onto the stack represented by this list. E remove () Retrieves and removes the head (first element) of this list. E remove ​(int index) Removes the element at the specif

It does not list the method in the Method Summary section. However, if you scroll down you find

Methods declared in interface java.util.List

containsAll, equals, hashCode, isEmpty, iterator, listIterator, removeAll, replaceAll, retainAll, sort, subList

so there is an implementation somewhere. If we further look at its superclasses

  1. java.lang.Ob 

It does.

Here is a simple JUnit test.

  1. @Test 
  2. void testLinkedList() { 
  3. List<String> list = new LinkedList<>(); 
  4. assertTrue(list.isEmpty()); 
  5. list.add("One"); 
  6. assertFalse(list.isEmpty()); 
  7. } 

It passes.

Now if we look at the documentation

LinkedList (Java SE 11 & JDK 11 )
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void add ​(int index, E element) Inserts the specified element at the specified position in this list. boolean add ​( E e) Appends the specified element to the end of this list. boolean addAll ​(int index, Collection <? extends E > c) Inserts all of the elements in the specified collection into this list, starting at the specified position. boolean addAll ​( Collection <? extends E > c) Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator. void addFirst ​( E e) Inserts the specified element at the beginning of this list. void addLast ​( E e) Appends the specified element to the end of this list. void clear () Removes all of the elements from this list. Object clone () Returns a shallow copy of this LinkedList . boolean contains ​( Object o) Returns true if this list contains the specified element. Iterator < E > descendingIterator () Returns an iterator over the elements in this deque in reverse sequential order. E element () Retrieves, but does not remove, the head (first element) of this list. E get ​(int index) Returns the element at the specified position in this list. E getFirst () Returns the first element in this list. E getLast () Returns the last element in this list. int indexOf ​( Object o) Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. int lastIndexOf ​( Object o) Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element. ListIterator < E > listIterator ​(int index) Returns a list-iterator of the elements in this list (in proper sequence), starting at the specified position in the list. boolean offer ​( E e) Adds the specified element as the tail (last element) of this list. boolean offerFirst ​( E e) Inserts the specified element at the front of this list. boolean offerLast ​( E e) Inserts the specified element at the end of this list. E peek () Retrieves, but does not remove, the head (first element) of this list. E peekFirst () Retrieves, but does not remove, the first element of this list, or returns null if this list is empty. E peekLast () Retrieves, but does not remove, the last element of this list, or returns null if this list is empty. E poll () Retrieves and removes the head (first element) of this list. E pollFirst () Retrieves and removes the first element of this list, or returns null if this list is empty. E pollLast () Retrieves and removes the last element of this list, or returns null if this list is empty. E pop () Pops an element from the stack represented by this list. void push ​( E e) Pushes an element onto the stack represented by this list. E remove () Retrieves and removes the head (first element) of this list. E remove ​(int index) Removes the element at the specif

It does not list the method in the Method Summary section. However, if you scroll down you find

Methods declared in interface java.util.List

containsAll, equals, hashCode, isEmpty, iterator, listIterator, removeAll, replaceAll, retainAll, sort, subList

so there is an implementation somewhere. If we further look at its superclasses

  1. java.lang.Object  
  2. java.util.AbstractCollection<E> 
  3. java.util.AbstractList<E> 
  4. java.util.AbstractSequentialList<E> 
  5. java.util.LinkedList<E> 

We find AbstractCollection has a definition for isEmpty(), which basically just checks size() == 0.

The size method is defined in the LinkedList class.

So this is a good example of Object-Oriented programming inheriting methods.

Profile photo for Eugene Ogongo

A list is an ordered list of objects, where the same object may well appear more than once. For example: [1, 7, 1, 3, 1, 1, 1, 5]. It makes sense to talk about the "third element" in a list. You can add an element anywhere in the list, change an element anywhere in the list, or remove an element from any position in the list.

A queue is also ordered, but you'll only ever touch elements at one end. All elements get inserted at the "end" and removed from the "beginning" (or head) of the queue. A queue is First in First out. (FIFO)

Profile photo for David Sharp

It won’t change any behavior of the class. It simply marks the class as serializable which allows it to be serialized and written out to a file, sent over a network, or stored in a database blob.

You will want to add a static final serialVersionUID field so that you can mark when you’ve made changes that cause the class to be incompatible with serialized instances of earlier versions of the class.

What is the serialVersionUID? | Baeldung

Profile photo for Alan Mellor

I’ll use it when I want to convey that things are in a strict sequence.

An ArrayList suggest that you can access elements in a random order, because it’s O(1) access to any random element.

If it’s really important that items are processed in strict sequence, I’ll use LinkedList to suggest that.

I’m not particularly precious on this, but when I use it, that’s why I use it.

Most of the time I use ArrayList and don’t think about it, like everyone else.

Profile photo for Michael Lloyd Lee

Because doing so would require every class to be serializable.

Being serializable is the exception not the norm. It only makes sense for a small subset of classes — data objects that need to go over the wire.

Profile photo for Luka Jovicic

Reading docs usually can answer this type of questions.

List: List (Java Platform SE 8 )
Queue:
Queue (Java Platform SE 8 )

So, in short:

When you create an object by using interface, you only have access to that interface's methods. If you create LinkedList as List<Something> list = new List<>(); you can only access methods List interface provides, but not the ones in Queue. List has a richer API, which makes sense because, unlike queue, it provides access to elements by their insertion order, while Queue only provides the head (depending on the type, it's either first or last element; they bo

Reading docs usually can answer this type of questions.

List: List (Java Platform SE 8 )
Queue:
Queue (Java Platform SE 8 )

So, in short:

When you create an object by using interface, you only have access to that interface's methods. If you create LinkedList as List<Something> list = new List<>(); you can only access methods List interface provides, but not the ones in Queue. List has a richer API, which makes sense because, unlike queue, it provides access to elements by their insertion order, while Queue only provides the head (depending on the type, it's either first or last element; they both have their respective iterators). That property allows you to insert and remove elements from specific places, get index of the wanted element or check whether List contains the given object.

Queue introduces only six methods - add (adds element to the queue), element (returns head, throws), offer (adds element if possible), peek (returns head, doesn't throw), poll (returns head and removes it from the queue, doesn't throw), remove (same as poll, throws). It's not the prettiest API, but it works. List has ~30 methods which you can look up in the docs.

What really matters is communicating the purpose. If you need a FIFO queue, declare it as a Queue, if you need a List then, well, declare it as a list.

Profile photo for Gerry Rzeppa

I don’t program in Java, but our Plain English compiler uses general-purpose hashed “indexes” that are implemented with a combination of arrays and linked lists to keep track of type names, global variable names, and routine names (so we can find them quickly and easily when we need them later in the process of compiling). Maybe looking at that will give you some ideas.

Each index record points to an array of bucket records, where each bucket is the anchor for a doubly-linked list of refer (reference) records, like this (click to enlarge):

Storing the both the first and any overflow refers for e

I don’t program in Java, but our Plain English compiler uses general-purpose hashed “indexes” that are implemented with a combination of arrays and linked lists to keep track of type names, global variable names, and routine names (so we can find them quickly and easily when we need them later in the process of compiling). Maybe looking at that will give you some ideas.

Each index record points to an array of bucket records, where each bucket is the anchor for a doubly-linked list of refer (reference) records, like this (click to enlarge):

Storing the both the first and any overflow refers for each bucket in the same list simplifies and standardizes the code for finding any entry in the index.

Only three buckets are shown in the above diagram, but many more are typically used in real-life situations. These, for example, are the create statements we use for our compiler indexes:

Create the type index with 4027 [buckets].
Create the global index with 4027 [buckets].
Create the routine index with 7919 [buckets].

And this is the routine we use to hash the various names:

To get a bucket# given a string and an index:
Put the string's length into the bucket#.
If the bucket# is 0, exit.
Add 5381 to the bucket#.
Slap a substring on the string.
Loop.
Put the substring's first's target into a byte.
Lowercase the byte.
Put the bucket# into a number.
Shift the bucket# left 5 bits.
Add the number to the bucket#.
Add the byte to the bucket#.
Add 3 to the substring's first.
If the substring is blank, break.
Repeat.
Bitwise and the bucket# with the largest number.
Divide the bucket# by the index's bucket count giving a quotient and the bucket#.

It is reasonably simple, small and fast enough for our purposes. It is similar to Daniel J. Bernstein’s “djb2″ algorithm, but we hash only the first and every third byte of the string after the that.

Et voila!

Profile photo for Anonymous
Anonymous

Serializable interface. Serializable is a marker interface (has no data member and method). It is used to "mark" Java classes so that the objects of these classes may get a certain capability. The Cloneable and Remote are also marker interfaces. It must be implemented by the class whose object you want to persist.

Profile photo for Nabarun Mondal

It is a great structure where things gets added/removed to front/end of the list. That immediately gets into Stack/Queue.

LinkedList are literally great at Stack and Queue.

Now comes the problematic part — adding a node is HUGE operation.

HUGE. If we use abstraction as ArrayList then adding a node is superfast.

People tend to thing about [math]O(1) [/math], but that is terrible abstraction. There is actually some time that will be spent to add a node.

LinkedList is a the slowest structure to do so, UNLESS, of course the ArrayList is having a problem of space constraint, so expansion - and boom!

Now ArrayList is

It is a great structure where things gets added/removed to front/end of the list. That immediately gets into Stack/Queue.

LinkedList are literally great at Stack and Queue.

Now comes the problematic part — adding a node is HUGE operation.

HUGE. If we use abstraction as ArrayList then adding a node is superfast.

People tend to thing about [math]O(1) [/math], but that is terrible abstraction. There is actually some time that will be spent to add a node.

LinkedList is a the slowest structure to do so, UNLESS, of course the ArrayList is having a problem of space constraint, so expansion - and boom!

Now ArrayList is way more time consuming.

Reality, real life complexity management comes under performance tuning - which is ( I used to do that in D.E.Shaw & Co. ) definitely non O(.) . It is actually if at all [math]\Theta(.) [/math]and actually not even that.

Copybook definition of efficiency is a terrible mess.

Profile photo for Quora User

Interfaces in JavaIn the Java programming language, an interface is a reference type, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types. Method bodies exist only for default methods and static methods.

Profile photo for Nabarun Mondal

That is because :

http://hg.openjdk.java.net/jdk7/jdk7/jdk/file/00cd9dc3c2b5/src/share/classes/java/util/AbstractCollection.java#l85

Already implements it.

Enjoy.

Good that you are looking at source code, look some more.

Think - before you act - it will not even compile if whatever you are saying is true. :-)

Profile photo for Quora User

Simple and Standard answer for this Query can be Something like this :-

Serializable is a marker interface . It is used to "mark" Java classes so that the objects of these classes may get a certain capability.

Small Example for this can be something like this

[code]import java.util.Scanner;
public class Employee implements Cloneable {
int age;
String name;
public Student (String name, int ag

Simple and Standard answer for this Query can be Something like this :-

Serializable is a marker interface . It is used to "mark" Java classes so that the objects of these classes may get a certain capability.

Small Example for this can be something like this

[code]import java.util.Scanner;
public class Employee implements Cloneable {
int age;
String name;
public Student (String name, int age){
this.age = age;
this.name = name;
}
public void display() {
System.out.println("Name of the employee is: "+name);
System.out.println("Age of the employee is: "+age);
}
public static void main (String args[]) throws CloneNotSupportedException {
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name: ");
String name = sc.next(...

Profile photo for Saikat Ray

Most modern languages provide containers like list, set, map, etc., which are used everywhere. List semantics are usually like a linked list, and most likely is implemented as a linked list, but the programmers do not need to know that.

Profile photo for Richard Rombouts

Linked lists are handy if you need to insert items in between or remove items.

With an array, you would need to move lots of elements ‘to the right’ to make room for a new element in the middle or ‘to the left’ to fill the hole if you remove an element in the middle. That is: if you insist that the original order is maintained. Else you could add new elements to the end of a std::vector. Removing a value in the middle (if order doesn’t matter) can be done by overwriting it with the last element and then making the vector one smaller. That would still have complexity O(1), constant time. Note: y

Linked lists are handy if you need to insert items in between or remove items.

With an array, you would need to move lots of elements ‘to the right’ to make room for a new element in the middle or ‘to the left’ to fill the hole if you remove an element in the middle. That is: if you insist that the original order is maintained. Else you could add new elements to the end of a std::vector. Removing a value in the middle (if order doesn’t matter) can be done by overwriting it with the last element and then making the vector one smaller. That would still have complexity O(1), constant time. Note: you may have to delete the element that is removed or free it. The latter is uncommon, cause then you would mix C style malloc with C++ STL.

Linked lists are often sorted in some way and you want to maintain the proper order at all times. Although they allow for fast removal and insertion, they have poor cache performance due to poor space locality: all nodes can be ‘anywhere’ in memory, they are not at neat consecutive locations. Also, the pointers to the next node (and previous node in case of double linked list) take extra memory. Thus for small lists it is better to use a std::vector or an array. Perhaps you can first build the vector or array and sort when you are done adding all items.

Profile photo for Prajakta Agarkar

List(I) Interface is child interface of collection interface(root interface of all collection framework). using List interface we get all objects in listed format. Insertion order is preserved by List(I). we get all list of objects in listed format. Duplication is allowed in list(I). List Interface is implemented by many child classes like Array List,Stack,Vector,Linked List.

Profile photo for Bryan White

Hardly ever. I cannot recall when I last used one. The business requirements rarely required them.

Normally I would use either SortedList or SortedDictionary. Maybe not even sorted depending on retrieval requirements.

Profile photo for Maurice Bourdin

You need to use the add(int index,E element) method, so there are some requirments for this to work :

You have to be sure your list is already sorted.

You need to find which index to use, which means you need to interated over the list until you reach the right condition.

Profile photo for Ranveer

I am assuming InsertInOrder here means a method which inserts an element appropriately in an already sorted LinkedList.

  1. public void insertInOrder(int num){ 
  2. //making a copy of the original list 
  3. Node copy=this.head; 
  4. while(copy!=null){ 
  5. if(copy.next==null){ 
  6. copy.next=new Node(num); 
  7. break; 
  8. } 
  9. if(copy.val<num && copy.next.val>num){ 
  10. Node insert=new Node(num); 
  11. insert.next=copy.next; 
  12. copy.next=insert; 
  13. break; 
  14. } 
  15. copy=copy.next; 
  16. } 
  17. } 

Basically, we just iterate to find the right spot [math]preceding Node value<val<succeeding Node value[/math]. If no such located is found, [math]Node[/math] is inserted at the end.

Profile photo for Usman Saleem

Traditionnaly we use ArrayList implementation of List in Java most of the time, which provide O(1) access to n element because they are backed by arrays. There is a downside though, when it is time to expand the ArrayList, a new expanded internal array is created and contents are copied over from previous array.

On the other hand, LinkedList doesn’t have this problem, inserting at tail is always one step, though index based access or insertion will be slower, worst case scenario order of (n/2) depending whether internal implementation start traversing from tail or head. Other advantage is using

Traditionnaly we use ArrayList implementation of List in Java most of the time, which provide O(1) access to n element because they are backed by arrays. There is a downside though, when it is time to expand the ArrayList, a new expanded internal array is created and contents are copied over from previous array.

On the other hand, LinkedList doesn’t have this problem, inserting at tail is always one step, though index based access or insertion will be slower, worst case scenario order of (n/2) depending whether internal implementation start traversing from tail or head. Other advantage is using LinkedList as a Queue which is useful in many scenarios.

At the end it depends on your usecase which implement of List is suitable for your algorithm.

Profile photo for John Collins

I worked primarily in Lisp (with a bit of C now and then) for about ten years starting in the mid-1980s. In Lisp, a linked list is the basic data structure, and there are lots of clever things you can do with them. They become trees if the elements themselves are lists. But in Java or other languages I mostly use now, I hardly ever care about how a list is implemented. In Java, the ArrayList generally has better performance unless you frequently need to insert or remove items in the middle of the list. But in many of those cases you should be asking whether a sorted list or tree would be bette

I worked primarily in Lisp (with a bit of C now and then) for about ten years starting in the mid-1980s. In Lisp, a linked list is the basic data structure, and there are lots of clever things you can do with them. They become trees if the elements themselves are lists. But in Java or other languages I mostly use now, I hardly ever care about how a list is implemented. In Java, the ArrayList generally has better performance unless you frequently need to insert or remove items in the middle of the list. But in many of those cases you should be asking whether a sorted list or tree would be better to start with.

Say if you want to create an array then you need to specify the size of it ,maybe not in some programing language, if say you specified it to 100 but you used only 50 of it so other 50 will be waste so we use linked list for efficient programing but if you have very small dataset then you should avoid it because that would not be efficient .

for more info you can visit to some good sites like geeksforgeeks , javatpoint etc.

Profile photo for Kethu Yerramma

No methods. It is marker interface which doesn’t have any methods but gives the capability to do something.

Example: if you want to send custom object from one activity to another then custom object need to implement Serializable.

About · Careers · Privacy · Terms · Contact · Languages · Your Ad Choices · Press ·
© Quora, Inc. 2025