Collections

¡Supera tus tareas y exámenes ahora con Quizwiz!

What is the difference between an Array and an ArrayList?

In Java, array and ArrayList are the well-known data structures. An array is a basic functionality provided by Java, whereas ArrayList is a class of Java Collections framework. It belongs to java.util package. An array is a dynamically-created object. It serves as a container that holds the constant number of values of the same type. It has a contiguous memory location. Once an array is created, we cannot change its size. In Java, ArrayList is a class of Collections framework. It implements List<E>, Collection<E>, Iterable<E>, Cloneable, Serializable, and RandomAccess interfaces. It extends AbstractList<E> class. ArrayList is internally backed by the array in Java. The resize operation in ArrayList slows down the performance as it involves new array and copying content from an old array to a new array. It calls the native implemented method System.arraycopy(sec, srcPos, dest, destPos, length). We cannot store primitive type in ArrayList. So, it stores only objects. It automatically converts primitive type to object.

What are collections in Java?

Java's collections framework implement common data structures for objects. List is an ordered collection of elements. A user has the ability to place an element anywhere in the list. The elements are accessable by their index. Unlike Set, List typically allows for duplicate elements such that element1.equals(element2). In addition to duplicates, List allow for multiple null elements to be stored. Set is a collection of non duplicate elements meaning there will never exist a situation where element1.equals(element2). In addition to this, it is implied that there can only exist one null element due to the no duplicates rule. Queue is a collection of elements who in essence cannot be iterated, instead the Queue follows the FIFO (First In First Out) rule. When an element is added to the Queue it is placed at the back and when an element is pulled it is pulled from the from the front (index :0). Deque extends Queue but augments the ability to insert and remove elements at either end. The name is short for "Double Ended Queue" and is pronounced "Deck". Map is an interface which stores data with a key. A map cannot contain duplicate keys; each key can map to at most one value. Map can be visualized like a dictionary where only one word is paired with one definition.

What is the difference between a Set and a List?

List and Set are interfaces of collections hierarchy. The base interface for other interfaces is Collection. The key difference between List and Set is that List supports storing the same element multiple times while Set does not support storing the same element multiple times. Therefore, a Set does not allow duplication.

What are generics? What is the diamond operator (<>)?

The Java Generics programming is introduced in J2SE 5 to deal with type-safe objects. It makes the code stable by detecting the bugs at compile time. Type-safety: We can hold only a single type of objects in generics The diamond operator allowed us to parameterize the type arguments for classes,

What are the interfaces in the Collections API?

The following list describes the core collection interfaces: Collection — the root of the collection hierarchy. A collection represents a group of objects known as its elements. The Collection interface is the least common denominator that all collections implement and is used to pass collections around and to manipulate them when maximum generality is desired. Some types of collections allow duplicate elements, and others do not. Some are ordered and others are unordered. The Java platform doesn't provide any direct implementations of this interface but provides implementations of more specific subinterfaces, such as Set and List. Also see The Collection Interface section. Set — a collection that cannot contain duplicate elements. This interface models the mathematical set abstraction and is used to represent sets, such as the cards comprising a poker hand, the courses making up a student's schedule, or the processes running on a machine. See also The Set Interface section. List — an ordered collection (sometimes called a sequence). Lists can contain duplicate elements. The user of a List generally has precise control over where in the list each element is inserted and can access elements by their integer index (position). If you've used Vector, you're familiar with the general flavor of List. Also see The List Interface section. Queue — a collection used to hold multiple elements prior to processing. Besides basic Collection operations, a Queue provides additional insertion, extraction, and inspection operations. Queues typically, but do not necessarily, order elements in a FIFO (first-in, first-out) manner. Among the exceptions are priority queues, which order elements according to a supplied comparator or the elements' natural ordering. Whatever the ordering used, the head of the queue is the element that would be removed by a call to remove or poll. In a FIFO queue, all new elements are inserted at the tail of the queue. Other kinds of queues may use different placement rules. Every Queue implementation must specify its ordering properties. Also see The Queue Interface section. Deque — a collection used to hold multiple elements prior to processing. Besides basic Collection operations, a Deque provides additional insertion, extraction, and inspection operations. Deques can be used both as FIFO (first-in, first-out) and LIFO (last-in, first-out). In a deque all new elements can be inserted, retrieved and removed at both ends. Also see The Deque Interface section. Map — an object that maps keys to values. A Map cannot contain duplicate keys; each key can map to at most one value. If you've used Hashtable, you're already familiar with the basics of Map. Also see The Map Interface section. The last two core collection interfaces are merely sorted versions of Set and Map: SortedSet — a Set that maintains its elements in ascending order. Several additional operations are provided to take advantage of the ordering. Sorted sets are used for naturally ordered sets, such as word lists and membership rolls. Also see The SortedSet Interface section. SortedMap — a Map that maintains its mappings in ascending key order. This is the Map analog of SortedSet. Sorted maps are used for naturally ordered collections of key/value pairs, such as dictionaries and telephone directories. Also see The SortedMap Interface section.

Are Maps in the Collections API? What makes Map different from other interfaces?

Maps are not included in the Collections API but are a part of the Collections framework. The Map interface present in java.util package represents a mapping between a key and a value. The Map interface is not a subtype of the Collection interface. Therefore it behaves a bit differently from the rest of the collection types. A map contains unique keys.

List several ways to iterate over a Collection. How would you iterate over a Map?

The first and most basic way you could iterate through a collection is using a traditional for loop. The second is using an advanced for each loop which could only be used on collections First of all, we cannot iterate a Map directly using iterators, because Map are not Collection. Iterating over Map.entrySet() using For-Each loop : Map.entrySet() method returns a collection-view(Set<Map.Entry<K, V>>) of the mappings contained in this map. So we can iterate over key-value pair using getKey() and getValue() methods of Map.Entry<K, V>. This method is most common and should be used if you need both map keys and values in the loop


Conjuntos de estudio relacionados

Problem Solving, Unit Conversion, and Dimensional Analysis

View Set

BLUM Ch1. Exploring Linux command line tools

View Set

The Declaration of Independence states that "All men are created equal." What did the Founding Fathers mean by this statement? Has American in the last two centuries evolved to fulfill the promise of this statement? How has it done so and/or failed to do

View Set

Science Quiz 1: Introduction to Reproduction

View Set

Intermediate to Advanced / Module 13 Words in Context

View Set