Java Assignments On Collections

« Previous • Trail • Next »

The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases.

Questions and Exercises: Interfaces

Questions

  1. At the beginning of this lesson, you learned that the core collection interfaces are organized into two distinct inheritance trees. One interface in particular is not considered to be a true , and therefore sits at the top of its own tree. What is the name of this interface?
  2. Each interface in the collections framework is declared with the syntax, which tells you that it is generic. When you declare a instance, what is the advantage of specifying the type of objects that it will contain?
  3. What interface represents a collection that does not allow duplicate elements?
  4. What interface forms the root of the collections hierarchy?
  5. What interface represents an ordered collection that may contain duplicate elements?
  6. What interface represents a collection that holds elements prior to processing?
  7. What interface repesents a type that maps keys to values?
  8. What interface represents a double-ended queue?
  9. Name three different ways to iterate over the elements of a .
  10. True or False: Aggregate operations are mutative operations that modify the underlying collection.

Exercises

  1. Write a program that prints its arguments in random order. Do not make a copy of the argument array. Demonstrate how to print out the elements using both streams and the traditional enhanced for statement.
  2. Take the example and modify it to use a instead of a . Specify a so that case is ignored when sorting and identifying set elements.
  3. Write a method that takes a and applies to each element.
  4. Consider the four core interfaces, , , , and . For each of the following four assignments, specify which of the four core interfaces is best-suited, and explain how to use it to implement the assignment.
    1. Whimsical Toys Inc (WTI) needs to record the names of all its employees. Every month, an employee will be chosen at random from these records to receive a free toy.
    2. WTI has decided that each new product will be named after an employee but only first names will be used, and each name will be used only once. Prepare a list of unique first names.
    3. WTI decides that it only wants to use the most popular names for its toys. Count up the number of employees who have each first name.
    4. WTI acquires season tickets for the local lacrosse team, to be shared by employees. Create a waiting list for this popular sport.

Check your answers.

« Previous • Trail • Next »


Using store( ) and load( )

java util Part 1 The Collections Framework, 17 Sep, 2014

Using store( ) and load( ) One of the most useful aspects of Properties is that the information contained in a . Properties object can be easily stored to or loaded from disk with the store( ) and load( ) methods. At any time, you can write a Properties object to a stream or read it back. This makes…

Read More →

Properties

java util Part 1 The Collections Framework, 17 Sep, 2014

Properties Properties is a subclass of Hash table, It is used to maintain lists of values in which the key is a String and the value is also a String. TI,e Properties class is used by many , ‘ other Java classes. For example, it is the type of object returned by System get Properties( ) when obtaining…

Read More →

Stack

java util Part 1 The Collections Framework, 17 Sep, 2014

Stack Stack is a subclass of Vector that implements, a standard last-in, first-out stack. Stack only defines the default constructor, which creates an empty stack. Stack includes all the methods defined by Vector, and adds several of its own, shown in Table 15-11. To put an object on the top of the stack, call push( I, To remove…

Read More →

Enumeration interface

java util Part 1 The Collections Framework, 17 Sep, 2014

Enumeration interface The output from this program is.shown here: Initial size: 0 Current capacity: 9 First element: 1 Last element: 2 Vector contains: 3 Elements in vector: With the release of Java 2, Vector adds support for iterators. Instead of relying o!” an enumeration to cycle through the objects (as the preceding program does), you now can…

Read More →

Vector

java util Part 1 The Collections Framework, 17 Sep, 2014

Vector Vector implements a dynamic array. It is to Array List, but with two differences: Vector is synchronized, and it contains many legacy methods that are not part of the collections frame work.With the release of Java 2,Vector wast engineered to extend Abstract List and implement the List interface, so it now is fully compatible with collections Here are…

Read More →

The legacy classes and interfaces

java util Part 1 The Collections Framework, 17 Sep, 2014

The legacy classes and interfaces As explained at. the start of this chapter, the original version of java.utll did not include the collections framework. Instead, it defined several classes and an interface that provided an ad has method of storing objects. With the addition of collections by Java 2, several of the original classes ware engineered to support…

Read More →

Arrays

java util Part 1 The Collections Framework, 17 Sep, 2014

Arrays Java 2 added a new class or java.util called Arrays. This class provides various methods that <Ire useful when working with <arrays, Although these methods technically aren’t part of Collections framework, they help bridge the gap between collections and arrays, Each method defined by Arrays is examined in this section. The as List( ) method returns a List…

Read More →

Using a Comparator

java util Part 1 The Collections Framework, 17 Sep, 2014

Using a Comparator The following is an example that demonstrates the power of a custom comparator. II implements the compare() method so that i operates in reverse of normal. Thus, it causes a tree set to be stored in reverse order. As the following output shows, the tree is now stored in reverse order: FEDCDA Look closely at…

Read More →

Comparators

java util Part 1 The Collections Framework, 17 Sep, 2014

Comparators   Doth TreeSet and TreeMap store elements in sorted order. However, it is the. comparator that defines precisely what “sorted order” means. By default, these classes store their elements by using what Java refers to as “natural ordering,” which is usually the ordering that you would expect. (A beforeB, Ibefore 2, and so forth.j If you want to…

Read More →

The TreeMap Class

java util Part 1 The Collections Framework, 17 Sep, 2014

The TreeMap Class TIle TrecMap class implements the Map interface by using a tree. A TreeMap provides an efficient means.of storing key value pairs in sorted order, and allows rapid retrieval. You should note that, unlike a hash map, a tree map guarantees that its .elements will be sorted in ascending key order. The following TrecMap constructors are…

Read More →

Next Page »

0 thoughts on “Java Assignments On Collections”

    -->

Leave a Comment

Your email address will not be published. Required fields are marked *