Java Collections

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Hierarchy of LinkedList class

extends AbstractSequentialList implements List and Deque interfaces.

String class and Wrapper classes implements the Comparable interface.So if you store the objects of string or wrapper classes?

it will be Comparable.

Which package contains all the classes and interfaces for Collection framework.?

java.util

Methods of Collection interface

public boolean add(Object element) public boolean addAll(Collection c) public boolean remove(Object element) public boolean removeAll(Collection c) collection from the invoking collection. public boolean retainAll(Collection c) public int size() public void clear()

Method of Collections class for sorting List elements

public void sort(List list): is used to sort the elements of List. List elements must be of Comparable type.

Methods of Iterator interface

- public boolean hasNext() it returns true if iterator has more elements. - public object next() it returns the element and moves the cursor pointer to the next element. - public void remove() it removes the last elements returned by the iterator.

HashSet LinkedHashSet TreeSet Performance?

---HashSet gives better performance than the LinkedHashSet and TreeSet. ---The performance of LinkedHashSet is between HashSet and TreeSet. Its performance is almost like HashSet. But slightly in the slower side as it also maintains LinkedList internally to maintain the insertion order of elements. ---TreeSet gives less performance than the HashSet and LinkedHashSet as it should sort the elements after each insertion and removal operations.

The important points about Java LinkedHashMap class are:

--A LinkedHashMap contains values based on the key. --It contains only unique elements. --t may have one null key and multiple null values. --It is same as HashMap instead maintains insertion order.

How to sort collections: Set or Map? List?

--If collection elements are of Set or Map, we can use TreeSet or TreeMap. --Collections class provides static methods for sorting the elements of collections. Method of Collections class for sorting List elements public void sort(List list)

The important points about Java TreeMap class are?

-1-A TreeMap contains values based on the key. -2-implements the NavigableMap interface and -3-extends AbstractMap class. -4-It contains only unique elements. -5-It cannot have null key but can have multiple null values. -6-It is same as HashMap instead maintains ascending order.

What are HashMap and Hashtable Commonalities?

-1-HashMap and Hashtable both are used to store data in key and value form. -2-Both are using hashing technique to store unique keys.

The important points about Java HashSet class are:

-1-HashSet stores the elements by using a mechanism called hashing. -2-HashSet contains unique elements only.

Java LinkedHashMap class

-1-Java LinkedHashMap class is Hash table and Linked list implementation of the Map interface, with predictable iteration order -2-It inherits HashMap class and implements the Map interface.

The important points about Java LinkedList are:

-1-Java LinkedList class can contain duplicate elements. -2-Java LinkedList class maintains insertion order. -3-Java LinkedList class is non synchronized. -4-In Java LinkedList class, manipulation is fast because no shifting needs to be occurred. Java LinkedList class can be used as list, stack or queue.

Java Non-generic Vs Generic Collection

-1-Java new generic collection allows you to have only one type of object in collection. -2-Now it is type safe so typecasting is not required at run time. -3-forced to have only specified type of objects in it.

Java Map Interface?

-A map contains values on the basis of key i.e. key and value pair. -Map is useful if you have to search, update or delete elements on the basis of key.

The important points about Java TreeSet class are:

-Contains unique elements only like HashSet. -Access and retrieval times are quiet fast. -Maintains ascending order.

Map.Entry Interface?

-Entry is the sub interface of Map. So we will be accessed it by Map.Entry name. -It provides methods to get key and value.

HashSet LinkedHashSet TreeSet Null elements?

-HashSet allows maximum one null element. -LinkedHashSet also allows maximum one null element. -TreeSet doesn't allow even a single null element. If you try to insert null element into TreeSet, it throws NullPointerException.

HashSet LinkedHashSet TreeSet Order Of Elements?

-HashSet doesn't maintain any order of elements. -LinkedHashSet maintains insertion order of elements. i.e. elements are placed as they are inserted. -TreeSet orders the elements according to supplied Comparator. If no comparator is supplied, elements will be placed in their natural ascending order.

HashSet LinkedHashSet TreeSet HOW THEY WORK internally?

-HashSet uses HashMap internally to store it's elements. -LinkedHashSet uses LinkedHashMap internally to store it's elements. -TreeSet uses TreeMap internally to store it's elements.

The important points about Java Collections class are:

-Java Collection class supports the polymorphic algorithms that operate on collections. -Java Collection class throws a NullPointerException if the collections or class objects provided to them are null.

Java EnumSet class

-Java EnumSet class is the specialized Set implementation for use with enum types. -It inherits AbstractSet class and implements the Set interface.

What is Java HashSet class?

-Java HashSet class is used to create a collection that uses a hash table for storage. -It inherits the AbstractSet class and implements Set interface.

Java TreeMap class

-Java TreeMap class implements the Map interface by using a tree. -It provides an efficient means of storing key/value pairs in sorted order.

Hierarchy of TreeSet class

-Java TreeSet class implements NavigableSet interface. -The NavigableSet interface extends SortedSet, Set, Collection and Iterable interfaces.

Java Collections class

-Java collection class is used exclusively with static methods that operate on or return collections. -It inherits Object class.

Java List Interface

-List Interface is the subinterface of Collection.

Hierarchy of HashSet class

-The HashSet class extends AbstractSet class which implements Set interface. -The Set interface inherits Collection and Iterable interfaces in hierarchical order.

Hierarchy of LinkedHashSet class

-The LinkedHashSet class extends HashSet class which implements Set interface. -The Set interface inherits Collection and Iterable interfaces in hierarchical order.

ArrayList and LinkedList Commonalities

-both implements List interface -maintains insertion order -Both are non synchronized classes.

What is framework in java

-provides readymade architecture. -represents set of classes and interface. -is optional.

The important points about Java ArrayList class are:

1)Java ArrayList class can contain duplicate elements. 2)Java ArrayList class maintains insertion order. FIFO 3)Java ArrayList class is non-synchronized. 4)Java ArrayList allows random access because array works at the index basis. 5)In Java ArrayList class, manipulation is slow because a lot of shifting needs to be occurred if any element is removed from the array list.

Java LinkedHashSet class

1)Java LinkedHashSet class is a Hash table and Linked list implementation of the set interface. 2)It inherits HashSet class and implements Set interface.

The important points about Java Hashtable class are:

1- A Hashtable is an array of list. Each list is known as a bucket. 2-The position of bucket is identified by calling the hashcode() method. 3- A Hashtable contains values based on the key. 2-It contains only unique elements. 3-It may have not have any null key or value. 4-It is synchronized.

Java HashMap class?

1- Java HashMap class implements the map interface by using a hashtable. 2- It inherits AbstractMap class and implements Map interface.

The important points about Java HashMap class are:

1-A HashMap contains values based on the key. 2-It contains only unique elements. 3-It may have one null key and multiple null values. 4-It maintains no order.

The important points about Java LinkedHashSet class are:

1-Contains unique elements only likeHashSet. 2-Provides all optional set operations, and permits null elements. 3-Maintains insertion order.

Java Comparable interface

1-Java Comparable interface is used to order the objects of user-defined class. 2-java.lang package 3- contains only one method named compareTo(Object) 4-It provide single sorting sequence only

Java Comparator interface

1-Java Comparator interface is used to order the objects of user-defined class 2-java.util package 3- 2 methods compare(Object obj1,Object obj2) and equals(Object element). 4-It provides multiple sorting sequence =You can sort the elements on the basis of any data member.

Java TreeSet class

1-Java TreeSet class implements the Set interface that uses a tree for storage. 2-It inherits AbstractSet class and implements NavigableSet interface. 3-The objects of TreeSet class are stored in ascending order.

compareTo(Object obj) method We can sort the elements of:

1-String objects 2-Wrapper class objects 3-User-defined class objects

Sorting in Collection We can sort the elements of:

1.String objects 2.Wrapper class objects 3.User-defined class objects

what is Collections in Java?

A framework that provides an architecture to store and manipulate the group of objects.

What is Collection framework

Collection framework represents a unified architecture for storing and manipulating group of objects.

What is Collection in java

Collection represents a single unit of objects i.e. a group.

What are Comparable and Comparator commonalities?

Comparable and Comparator both are interfaces and can be used to sort collection elements.

What is difference between HashMap and TreeMap?

HashMap TreeMap 1) HashMap can contain one null key.// TreeMap can not contain any null key. 2) HashMap maintains no order.// TreeMap maintains ascending order.

Hierarchy of HashMap class

HashMap class extends AbstractMap class and implements Map interface.

Difference between HashSet and HashMap

HashSet contains only values whereas HashMap contains entry(key and value).

How Collections class provides static methods for sorting the elements of collection.

If collection elements are of Set type, we can use TreeSet. But We cannot sort the elements of List. Collections class provides methods for sorting the elements of List type elements.

Hierarchy of ArrayList class

Java ArrayList class extends AbstractList class which implements List interface.

Java EnumMap class

Java EnumMap class is the specialized Map implementation for enum keys. It inherits Enum and AbstractMap classes.

Java Hashtable class?

Java Hashtable class implements a hashtable, -which maps keys to values. It inherits Dictionary class and implements the Map interface.

Java LinkedList class

Java LinkedList class uses doubly linked list to store the elements. It provides a linked-list data structure. It inherits the AbstractList class and implements List and Deque interfaces.

Difference between List and Set

List can contain duplicate elements whereas Set contains unique elements only.

What is Java ListIterator Interface used for?

ListIterator Interface is used to traverse the element in backward and forward direction.

What are the ways to iterate the elements of collection in java?

There are two ways to traverse collection elements: 1-By Iterator interface. 2-By for-each loop.

what is Comparable type?

They nee to implement Comparable Interface.

Java Collection simply means

a single unit of objects


Ensembles d'études connexes

Microeconomics Ch.7 Elasticity and Consumer Choice

View Set

B Law 2nd Exam (corrections only)

View Set

Assessment Chapter 8 & Test Types

View Set

Critical Care Final Exam Practice Questions

View Set

NEW YORK REAL ESTATE CHAPTER 3-A

View Set

MARK Test 3 (ch 2, 8, 13, 17-19)

View Set

Unintended Consequences of Human Activity Test

View Set

11 - Texas Laws and Rules Pertinent to Insurance

View Set

RN Nutrition Online Practice 2023 A

View Set