0% found this document useful (0 votes)
17 views23 pages

12.support Classes

The document discusses support classes in Java, specifically the Collections and Arrays classes. It describes how Collections provides methods for sorting, shuffling, routine data manipulation, searching, composition, and finding extreme values on lists. It also notes Arrays is similar but works with arrays rather than collections.

Uploaded by

nam120404
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views23 pages

12.support Classes

The document discusses support classes in Java, specifically the Collections and Arrays classes. It describes how Collections provides methods for sorting, shuffling, routine data manipulation, searching, composition, and finding extreme values on lists. It also notes Arrays is similar but works with arrays rather than collections.

Uploaded by

nam120404
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Support Classes

Objectives

• Support Classes: Collections, Arrays


• Use the Collections class
• Sorting/ Shuffling
• Routine Data Manipulation(copy, reverse,
swap, addAll…)
• Searching (binarySearch)/
Composition(frequency, disjoint, min, max)
• Finding Extreme Values (to find min,max
value by comparator)
• Use the Arrays class
Introduction

• An algorithm on a list can be applied on


some lists although the type of elements in
each list can be different.
• The polymorphic algorithms described here
are pieces of reusable functionality provided
by the Java platform.
• All of them come from the Collections class
and the Arrays class (support classes), and
all take the form of static methods whose first
argument is the collection on which the
operation is to be performed.
The Collections class

• A support class containing static methods which


accept collections as their parameters.
• file:///J:/Softs/JavaSofts/JavaDocs/docs-
Java8/api/java/util/Collections.html
Routine Data Manipulation

• The Collections class provides five


algorithms for doing routine data
manipulation on List objects, including:
• reverse()
• fill()
• copy()
• swap()
• addAll()
Searching

• Condition: The list in ascending order


• The binarySearch algorithm searches for a
specified element in a sorted List.
• Return pos >=0 → Present
• Return pos<0 → Absent
Composition

• frequency — counts the number of


times the specified element occurs in the
specified collection.
• disjoint — determines whether
two Collections are disjoint; that is,
whether they contain no elements in
common.
Finding Extreme Values

• Methods: min(…), max(…)


Collections Demo.
Collections Demo.
Sorting
• The sort algorithm reorders a List so that
its elements are in ascending order
according to an ordering relationship.
• Example
public class Sort {
public static void main(String[] args) {
List<String> list = Arrays.asList(args);
Collections.sort(list);
System.out.println(list);
}
}
Sorting
Comparable Interface
• A comparison function, which imposes a total
ordering on some collection of objects

• The following demonstration will show you the


way to sort a list based on your own criteria: A
list of employees will be sorted based on
ascending IDs.
Comparable Interface
Comparable Interface
Comparator Interface
• A comparison function, which imposes a total
ordering on some collection of objects

• The following demonstration will show you the


way to sort a list based on your own criteria: A
list of employees will be sorted based on
descending salaries then ascending IDs.
Comparator Interface – Demo.
Comparator Interface – Demo.
Comparator Interface – Demo.
Comparator Interface – Demo.
The Arrays Class

• It it similar to the Collections class, but it


accepts arrays as it’s parameters.
• file:///J:/Softs/JavaSofts/JavaDocs/docs-
Java8/api/java/util/Arrays.html
Arrays Class: Demo.
Summary

• Support Classes: Collections, Arrays


• Use the Collections class
• Sorting/ Shuffling
• Routine Data Manipulation
• Searching/ Composition
• Finding Extreme Values
• Use the Arrays class

You might also like