Computer Science

Java List Interface

Java List Interface is a part of the Java Collections Framework that defines an ordered collection of elements. It extends the Collection interface and provides methods to add, remove, and access elements in a list. The List interface is implemented by various classes such as ArrayList, LinkedList, and Vector.

Written by Perlego with AI-assistance

10 Key excerpts on "Java List Interface"

  • Book cover image for: 100+ Solutions in Java - 2nd Edition
    eBook - ePub

    100+ Solutions in Java - 2nd Edition

    Everything you need to know to develop Java applications (English Edition)

    java.lang.UnsupportedOperationException .
    7.4 List interface
    Lists are collections created by implementing the List interface, which is an extension of the Collection interface. Lists are ordered collections that allow duplicate values and multiple null elements. Elements of a list can be accessed/searched by using an integer index. Similar to Java arrays, the list index begins at zero and the last index is size-1 . Collections can be manipulated by the methods of List interface through position-based operations. The List interface uses an index internally to order the elements that are added to a list. List interface also provides methods to add and search elements as well as perform range operations.
    The List interface provides the ListIterator to sequentially access the elements of the list. It is used to insert and replace elements and for bidirectional access along with the normal operations. Listed below are the additional methods of List interface apart from those inherited from the Collection
  • Book cover image for: 100+ Solutions in Java
    eBook - ePub

    100+ Solutions in Java

    A Hands-On Introduction to Programming in Java: A Hands-On Introduction to Programming in Java (English Edition)

    • Dhruti Shah, DHRUTI SHAH(Authors)
    • 2020(Publication Date)
    • BPB Publications
      (Publisher)
    java.lang.UnsupportedOperationException .

    7.4 List interface

    Lists are collections created by implementing the List interface which is an extension of the Collection interface. Lists are ordered collections that allow duplicate values and multiple null elements. Elements of a list can be accessed/searched by using an integer index. Similar to Java arrays, the list index begins at zero and the last index is size-1. Collections can be manipulated by the methods of the List interface through position-based operations. The List interface uses an index internally to order the elements that are added to a list. The List interface also provides methods to add and search elements as well as perform range operations.
    The List interface provides the ListIterator to sequentially access the elements of the list. It is used to insert and replace elements and for bidirectional access along with the normal operations. Here are the additional methods of the List interface apart from those inherited from the Collection
  • Book cover image for: Brief Java
    eBook - PDF

    Brief Java

    Early Objects

    • Cay S. Horstmann(Author)
    • 2019(Publication Date)
    • Wiley
      (Publisher)
    In this chapter, you will learn about the Java collections framework, a hierarchy of interface types and classes for collecting objects. Each interface type is implemented by one or more classes (see Figure 1). At the root of the hierarchy is the Collection interface. That interface has methods for adding and removing elements, and so on. Table 1 on page 514 shows all the methods. Because all collections implement this interface, its methods are available for all collection classes. For example, the size method reports the number of ele- ments in any collection. The List interface describes an important category of collections. In Java, a list is a collection that remembers the order of its elements (see Figure 2). The ArrayList class implements the List interface. An ArrayList is simply a class containing an array that is expanded as needed. If you are not concerned about efficiency, you can use the Array- List class whenever you need to collect objects. However, several common opera- tions are inefficient with array lists. In particular, if an element is added or removed, the elements at larger positions must be moved. The Java library supplies another class, LinkedList, that also implements the List interface. Unlike an array list, a linked list allows efficient insertion and removal of elements in the middle of the list. We will discuss that class in the next section. A collection groups together elements and allows them to be retrieved later. ‹‹interface›› Map HashMap TreeMap ‹‹interface›› Collection HashSet TreeSet Stack LinkedList ‹‹interface›› List ‹‹interface›› Queue ‹‹interface›› Set ArrayList PriorityQueue Figure 1 Interfaces and Classes in the Java Collections Framework 15.1 An Overview of the Collections Framework 513 © Vladimir Trenin/iStockphoto. Figure 4 A Stack of Books You use a list whenever you want to retain the order that you established. For example, on your bookshelf, you may order books by topic.
  • Book cover image for: Big Java
    eBook - PDF

    Big Java

    Late Objects

    • Cay S. Horstmann(Author)
    • 2016(Publication Date)
    • Wiley
      (Publisher)
    In this chapter, you will learn about the Java collections framework , a hierarchy of interface types and classes for collecting objects. Each interface type is implemented by one or more classes (see Figure 1). At the root of the hierarchy is the Collection interface. That interface has methods for adding and removing elements, and so on. Table 1 on page 694 shows all the methods. Because all collections implement this interface, its methods are available for all collection classes. For example, the size method reports the number of ele-ments in any collection. The List interface describes an important category of collections. In Java, a list is a collection that remembers the order of its elements (see Figure 2). The ArrayList class implements the List interface. An ArrayList is simply a class containing an array that is expanded as needed. If you are not concerned about efficiency, you can use the Array-List class whenever you need to collect objects. However, several common opera-tions are inefficient with array lists. In particular, if an element is added or removed, the elements at larger positions must be moved. The Java library supplies another class, LinkedList , that also implements the List interface. Unlike an array list, a linked list allows efficient insertion and removal of elements in the middle of the list. We will discuss that class in the next section. A collection groups together elements and allows them to be retrieved later. Figure 1 Interfaces and Classes in the Java Collections Framework ‹‹interface›› Map HashMap TreeMap ‹‹interface›› Collection HashSet TreeSet Stack LinkedList ‹‹interface›› List ‹‹interface›› Queue ‹‹interface›› Set ArrayList PriorityQueue © nicholas belton/iStockphoto. 15.1 An Overview of the Collections Framework 693 You use a list whenever you want to retain the order that you established. For example, on your bookshelf, you may order books by topic.
  • Book cover image for: Java Concepts
    eBook - PDF

    Java Concepts

    Late Objects

    • Cay S. Horstmann(Author)
    • 2016(Publication Date)
    • Wiley
      (Publisher)
    In this chapter, you will learn about the Java collections framework, a hierarchy of interface types and classes for collecting objects. Each interface type is implemented by one or more classes (see Figure 1). At the root of the hierarchy is the Collection interface. That interface has methods for adding and removing elements, and so on. Table 1 on page 694 shows all the methods. Because all collections implement this interface, its methods are available for all collection classes. For example, the size method reports the number of ele- ments in any collection. The List interface describes an important category of collections. In Java, a list is a collection that remembers the order of its elements (see Figure 2). The ArrayList class implements the List interface. An ArrayList is simply a class containing an array that is expanded as needed. If you are not concerned about efficiency, you can use the Array- List class whenever you need to collect objects. However, several common opera- tions are inefficient with array lists. In particular, if an element is added or removed, the elements at larger positions must be moved. The Java library supplies another class, LinkedList, that also implements the List interface. Unlike an array list, a linked list allows efficient insertion and removal of elements in the middle of the list. We will discuss that class in the next section. A collection groups together elements and allows them to be retrieved later. Figure 1 Interfaces and Classes in the Java Collections Framework ‹‹interface›› Map HashMap TreeMap ‹‹interface›› Collection HashSet TreeSet Stack LinkedList ‹‹interface›› List ‹‹interface›› Queue ‹‹interface›› Set ArrayList PriorityQueue © nicholas belton/iStockphoto. 15.1 An Overview of the Collections Framework 693 You use a list whenever you want to retain the order that you established. For example, on your bookshelf, you may order books by topic.
  • Book cover image for: Data Structures
    eBook - PDF

    Data Structures

    Abstraction and Design Using Java

    • Elliot B. Koffman, Paul A. T. Wolfgang(Authors)
    • 2021(Publication Date)
    • Wiley
      (Publisher)
    C h a p t e r 53 Lists and the Collections Framework S o far, we have one data structure that you can use in your programming—the array. Giving a programmer an array and asking her to develop software systems is like giv- ing a carpenter a hammer and asking him to build a house. In both cases, more tools are needed. The Java designers attempted to supply those tools by providing a rich set of data structures written as Java classes. The classes are all part of a hierarchy called the Java Collections Framework. We will discuss classes from this hierarchy in the rest of the book, starting in this chapter with the classes that are considered lists. A list is an expandable collection of elements in which each element has a position or index. Some lists enable their elements to be accessed in arbitrary order (called random access) using a position value to select an element. Alternatively, you can start at the begin- ning and process the elements in sequence. We will also discuss iterators and their role in facilitating sequential access to lists. In this chapter, we will discuss the ArrayList and linked lists (class LinkedList) and their similarities and differences. We will show that these classes are subclasses of the abstract class AbstractList and that they implement the List interface. 2 C h a p t e r O b j e c t i v e s ◆ To understand the meaning of big-O notation and how it is used as a measure of an algorithm’s efficiency ◆ To become familiar with the List interface and the Java Collections Framework ◆ To understand how to write an array-based implementation of the List interface ◆ To study the differences between single-, double-, and circular-linked list data structures ◆ To learn how to implement a single-linked list ◆ To learn how to implement the List interface using a double-linked list ◆ To understand the Iterator interface ◆ To learn how to implement the Iterator for a linked list ◆ To become familiar with the Java Collections Framework
  • Book cover image for: Introduction to Programming
    No longer available |Learn more

    Introduction to Programming

    Learn to program in Java with data structures, algorithms, and logic

    Although arrays are expected to provide better performance while accessing its elements, in practice, modern algorithms make the differences in performance of arrays and collections negligible, except in some very specialized cases. That is why the only reason you would have to use arrays is when some algorithms or methods require it.
    Passage contains an image

    Here is what we are going to discuss

    In the following subsections, we will discuss the most popular interfaces and classes of Java collections from standard libraries:
    • The List interface and the ArrayList class – they preserve the order of the elements
    • The Set interface and the HashSe class – they do not allow duplicate elements
    • The Map and HashMap interfaces – they store objects by with a key and thus allow key-to-value mapping
    Please notice that most of the methods described in the following sections come from the java.util.Collection interface – the parent interface of almost all the collections, except those that implement the java.util.Map interface.
    Passage contains an image

    List - ArrayList preserves order

    List is an interface, while the ArrayList class is its most often used implementation. Both are residing in the java.util package. The ArrayList class has a few more methods - in addition to those declared in the List interface. The removeRange() method, for example, is not present in the List interface but available in the ArrayList API.
    Passage contains an image

    Prefer variable type List

    It is a good practice, while creating an object of an ArrayList, to assign its reference to a variable of type List: List listOfNames = new ArrayList(); More likely than not, using a variable of type ArrayList will not change anything in your program, not today, nor in the future: ArrayList listOfNames = new ArrayList();
    The preceding reference can still be passed to any method that accepts a parameter of type List. However, coding to an interface (that is what we do when we make the variable of an interface type) is a good habit in general because you never know when the requirements to your code might change and you would need to use another implementation of List, like the LinkedList class, for example. If the variable type was List, switching an implementation is easy. But if the variable type was ArrayList, changing it to List or to LinkedList requires tracking down all the places the variable was used and run various tests to make sure that ArrayList methods were not called anywhere. And if the code is complex, one can never be sure that all possible execution paths were checked and the code will not break in production. That's why we prefer to use an interface type for the variable that holds the reference to an object, unless you really need it to be the class type. We talked about this extensively in Chapter 8 , Object-Oriented Design (OOD) Principles
  • Book cover image for: The Java Workshop
    No longer available |Learn more

    The Java Workshop

    A Practical, No-Nonsense Introduction to Java Development

    • David Cuartielles, Andreas Göransson, Eric Foster-Johnson(Authors)
    • 2019(Publication Date)
    • Packt Publishing
      (Publisher)

    4. Collections, Lists and Java's Built-In APIs

    Overview
    This chapter introduces you to the powerful Java collections framework, which is used to store, sort, and filter data. It will first take you through the structure of the built-in Collections Application Programming Interface (API ), the Java collections framework, which will simplify your dealings with complex data structures and allow you to use and create APIs with minimal effort. Through this framework, you will examine the relationship between lists and arrays, and learn to populate lists from arrays. Finally, in this chapter's final activity, you will create and complete a program in which you will be asked to perform standard operations on data stored in sets, lists, and maps in preparation for future chapters.

    Introduction

    Java comes with a built-in Collections API, allowing you to manipulate data structures with very little effort. A collection is an object that contains multiple elements. Collections are used to store, share, process, and communicate aggregated data. We call this system the Java collections framework .
    As part of this framework, there are different components that are used to optimize our interaction with the actual data:
    • Interfaces : Abstract data types that represent collections
    • Implementations : Specific implementations of the collection interfaces
    • Algorithms : Polymorphic methods used to process the data within a collection for operations such as sorting and searchingNote
      Other programming languages have their own collection frameworks. For example, C++ has the Standard Template Library (STL
  • Book cover image for: OCP Oracle Certified Professional Java SE 17 Developer Study Guide
    • Scott Selikoff, Jeanne Boyarsky(Authors)
    • 2022(Publication Date)
    • Sybex
      (Publisher)
    This is legal because a null reference can be assigned to any reference variable. On line 5, we try to unbox that null to an int primitive. This is a problem. Java tries to get the int value of null . Since calling any method on null gives a NullPointerException , that is just what we get. Be careful when you see null in rela-tion to autoboxing. Using the List Interface 471 Using the List Interface Now that you’re familiar with some common Collection interface methods, let’s move on to specific interfaces. You use a list when you want an ordered collection that can contain duplicate entries. For example, a list of names may contain duplicates, as two animals can have the same name. Items can be retrieved and inserted at specific positions in the list based on an int index, much like an array. Unlike an array, though, many List implementations can change in size after they are declared. Lists are commonly used because there are many situations in programming where you need to keep track of a list of objects. For example, you might make a list of what you want to see at the zoo: first, see the lions, because they go to sleep early; second, see the pandas, because there is a long line later in the day; and so forth. Figure 9.2 shows how you can envision a List . Each element of the List has an index, and the indexes begin with zero. Sometimes you don’t care about the order of elements in a list. List is like the “go to” data type. When we make a shopping list before going to the store, the order of the list hap-pens to be the order in which we thought of the items. We probably aren’t attached to that particular order, but it isn’t hurting anything. While the classes implementing the List interface have many methods, you need to know only the most common ones. Conveniently, these methods are the same for all of the imple-mentations that might show up on the exam. The main thing all List implementations have in common is that they are ordered and allow duplicates.
  • Book cover image for: Java Professional Interview Guide
    eBook - ePub

    Java Professional Interview Guide

    Learn About Java Interview Questions and Practise Answering About Concurrency, JDBC, Exception Handling, Spring, and Hibernate

    HAPTER 8

    Collections

    Introduction

    Java is an object-oriented programming language. Software created using Java comprises of a number of objects that communicate with each other. It is always essential to group these objects together so as to perform some common operations on these objects. Normally, such operations need to be performed manually by a developer repetitively. To reduce such effort, Java introduced the collection framework, which consists of predefined data structures and algorithm, which can be applied on an object. The collection framework is essentially a group of different interfaces and their implementation classes using which one can group the objects based on different scenarios. Every interface on its own facilitates different functionalities to a group of objects based on which objects are arranged or traversed. Such functionalities give flexibility to the developers to use objects in an application to their full capacity to achieve the desired result. Sometimes, the developers are in a need of objects that are ordered; sometimes, they want the same object group without duplicates, or sometimes, the same object group in some sorted order. The collection framework provides all such solutions to the developers.

    Structure

    Understanding the collection framework is often assumed to be complex. It consists of a number of interfaces, sub-interfaces, and their implementation classes. In this chapter, we will cover the different aspects of the Collection framework as:
    • What is the need for the Collection framework?
    • Different Collection interfaces:
      • List
      • Set
      • Queue
      • Map
    • Utility classes like Collections and Arrays

    Objective

    Every interface in Collection is defined to achieve a specific functionality. To remember such a big API is not an easy task. And, because of its importance in real-time projects, Collection is one of the most favorite topics in the Java interview process. As mentioned, the Collection Framework API is huge. So, covering each and every class is not possible or, in fact, not required as far as the interview process is concerned. But, for sure, in this chapter, you will learn how to answer almost every important question, right from the concept to implementation so that you will succeed in an interview. You will able to master the different collection types like List, Set, Queue, and Map, and their implementations.
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.