1. Trang chủ
  2. » Mẫu Slide

Session4 module5 java util

33 916 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 33
Dung lượng 659,51 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

m mới bắt đầu học java nên chưa có kinh nghiệm gì, nên mong các tiền bối đi trước cho em vài lời khuyên. trước đây em có học qua về c++ với C và php nhưng không thực sự giỏi cái nào. hiện nay em đang có bài tập lớn về java,mà em thì không có kinh nghiệm gì về nó nên muốn chọn mảng nào dễ hơn để có thể dễ bắt đầu, nếu các anh chị nào đã học qua thì cho em 1 vài lời khuyên với.em xin cám ơn các anh chị trước.

Trang 1

JPII, Session 4

Module 5 - java.util and Collections API

Trang 2

Module4 - Review

Advanced Java / Session4 / 2 of 27

A Guide To Advanced Java – Module 4 2

• A stream is a logical entity that produces or consumes information.

String values.

InputStream is an abstract class that defines how data is

received The OutputStream class is also abstract, it defines the way in which output is written to streams

File class directly works with files on the file system

• A buffer is a temporary storage area for data

input/output operations

• Serialization is the process of reading and writing objects to a byte

stream (Using ObjectInputStream/ObjectOutputStream)

Trang 3

Advanced Java / Session4 / 3 of 27

A Guide To Advanced Java – Module 5

Trang 4

Collections are very important

Java Collection API

“If you can program

with Java but you don't understand

interfaces yet, you may have some

trouble getting through the material in

this session.”

“The topics such as linked lists,

queues, stacks, and binary trees,

among many other data structures are

related topic for this session.”

Trang 5

Introduction to java.util package

number of useful classes providing a broad

range of functionality

It consists of interfaces and classes for working with group of objects

Advanced Java / Session4 / 4 of 27 A Guide To Advanced Java – Module 5

Trang 6

Overview of Collection

A collection is a group of data

manipulate as single object

Can be made not-modifiable.

Advanced Java / Session4 / 5 of 27

Trang 7

o Generalization of the array concept.

o Set of interfaces defined in Java for storing object

o Multiple types of objects

o Resizable

Advanced Java / Session4 / 6 of 27

Trang 9

Some methods of “Collection” interface -1

Advanced Java / Session4 / 8 of 27

o int size()Returns the number of elements in this collection

o boolean isEmpty()Returns true if this collection contains no elements

o boolean contains(Object element)

Returns true if this collection contains the specified element

o boolean add(Object element)Ensures that this collection contains the specified element

o Iterator iterator()Returns an iterator over the elements in this collection

Trang 10

Iterator -1

 An iterator is an object that allows to traverse through all the elements of a collection, regardless of its specific

implementation

Advanced Java / Session4 / 20 of 27

Trang 11

// Do something with element }

Trang 12

List interface

Advanced Java / Session4 / 10 of 27

A Guide To Advanced Java – Module 5

 The List interface is an extension of the Collection interface

 It defines an ordered collection of data and

allows duplicate objects to be added to a list

The List interface uses an index for ordering

the elements while storing them in a list List has methods that allow access to elements

based on their position.

Trang 13

List interface

Advanced Java / Session4 / 11 of 27

A Guide To Advanced Java – Module 5

Extensions compared to the Collection interface

 Access to elements via indexes, like arrays

add (int, Object), get(int), remove(int), set(int, Object)

 Search for elements

Trang 14

Advanced Java / Session4 / 11 of 27 A Guide To Advanced Java – Module 5

Implements List interface

cannot store primitive values such as double

Its size increases automatically

Is best suited for random access without add and remove operations

Trang 15

Advanced Java / Session4 / 11 of 27 A Guide To Advanced Java – Module 5

Trang 16

Advanced Java / Session4 / 11 of 27 A Guide To Advanced Java – Module 5

The Vector class is similar to an ArrayList as it also implements dynamic array

The difference between Vector and ArrayList class is that the methods of Vector are synchronised and are

thread-safe This increases the overhead cost of

calling these methods

Trang 17

Advanced Java / Session4 / 12 of 27

A Guide To Advanced Java – Module 5

LinkedList implements the List interface.

Trang 18

Set interface

Advanced Java / Session4 / 21 of 27

A Guide To Advanced Java – Module 5

Corresponds to the mathematical definition

of a set

o The Set interface creates a list of unordered objects

o No duplicates are allowed

Compared to the Collection interface

o Interface is identical.

o Every constructor must create a collection without duplicates.

o The operation add cannot add an element already in the set.

o The method call set1.equals(set2) works at follows

Trang 19

Set classes

HashSet creates a collection that makes use of a hash table for data storage

maintains the order of elements added to the Set

are stored in ascending order

Advanced Java / Session4 / 22 of 27 A Guide To Advanced Java – Module 5

Trang 20

Set Idioms

Advanced Java / Session4 / 23 of 27

Trang 21

Map interface -1

Advanced Java / Session4 / 14 of 27

A Guide To Advanced Java – Module 5

A Map is an object that maps keys to values, a map

cannot contain duplicate keys

Trang 22

Map interface -2

Advanced Java / Session4 / 15 of 27

o get (Object key)

(key, value) pairs

Trang 23

Map classes

• HashMap implememts Map.

• Hashtable class stores elements as a key/value pairs in the hash table

• TreeMap class stores elements as tree structure and returns keys in sorted order Important methods of the TreeMap class are firstKey(), lastKey(), headMap(),

tailMap()

• LinkedHashMap class implements the concept of hash table and the linked list in the Map interface Important methods of the LinkedHashMap class are clear(),

containsValue(), get(), removeEldesEntry()

Advanced Java / Session4 / 16 of 27 A Guide To Advanced Java – Module 5

Trang 24

Advanced Java / Session4 / 17 of 27

Map map = new HashMap();

//tra ve tap key luu trong tap Set

Set keySet = map.keySet();

for (Object key: keySet) {

//lay ve tung value theo key

Object value = map.get(key); System.out.println(value); }

Trang 25

Comparable Interface

Advanced Java / Session4 / 18 of 27

Implement the Comparable interface to make class instances comparable

class Person implements Comparable { private int age;

private String firstName;

public int compareTo(Object anotherPerson) throws ClassCastException {

if (!(anotherPerson instanceof Person)) throw new ClassCastException("A Person object expected."); int anotherPersonAge = ((Person) anotherPerson).getAge(); return this.age - anotherPersonAge;

} public int getAge() { return age;

} }

Trang 26

Comparator Interface

Advanced Java / Session4 / 19 of 27

If you want to sort some objects that don't implement Comparable?

public class FirstNameComparator implements Comparator {

public int compare(Object person, Object anotherPerson) {

String firstName1 = ((Person)

person).getFirstName().toUpperCase( );

String firstName2 = ((Person)

anotherPerson).getFirstName().toUpp erCase();

return firstName1.compareTo(firstName2);

} }

Person[] persons = new Person[4];

….

Arrays.sort(persons, new FirstNameComparator());

Trang 27

Queue interface

ordered in First In First Out – FIFO

order

according to their values

peek(), remove(), offer() and element()

Advanced Java / Session4 / 25 of 27 A Guide To Advanced Java – Module 5

Trang 28

PriorityQueue class

 Priority queues similar to queues but the

elements are not arranged in FIFO structure They are arranged in a user-defined manner.

 The elements are ordered either by natural ordering or according to a comparator.

 A priority queue is unbound and allows the queue to grow in capacity.

Advanced Java / Session4 / 26 of 27 A Guide To Advanced Java – Module 5

Trang 29

Arrays class

working with arrays such as searching, sorting and comparing arrays

toString()

Advanced Java / Session4 / 27 of 27 A Guide To Advanced Java – Module 5

Trang 30

Collections class

• Collections class provide a number of methods for working with collections such as searching, sorting

Advanced Java / Session4 / 27 of 27 A Guide To Advanced Java – Module 5

Trang 31

Module5 - Summary

We have learnt

• Collection, List, Set, Queue, Iterator and some of their implementations

some of their implementations

• Collections, Arrays class.

A Guide To Advanced Java – Module 5

Trang 33

generic class, method.

regular expression.

Advanced Java / Session4 / 2 of 27 33

Ngày đăng: 22/11/2014, 16:39

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN