Thursday, February 24, 2011

Chapter 50: Methods Overview for Collections

In the previous few chapters, we have covered details about the different types of collections in great detail. The purpose of this article is to highlight the other methods that these collections have which we havent covered in our previous chapters.

So, lets get started!!!

Important Methods for List, Set and Map

Method Name Exists in Description/Purpose
List Set Map
boolean add(element) Y Y - Add an element.
boolean add(index, element) Y - - Add the element at an index point.
boolean contains(object) Y Y Y Search a collection for an object
boolean containsKey(object key) - - Y Search a Collection for a Key
boolean containsValue(object value) - - Y Search a Collection for a Value
object get(index) Y - - Get an Object from a collection using the index position
object get(key) - - Y Get an object from a Map using the Key
int indexOf(object) Y - - Get the location of an object in a List.
Iterator iterator() Y Y - Get an Iterator for a List or a Set.
Set keySet() - - Y Return a Set containing a Map’s keys.
put(key, value) - - Y Add a key/value pair to a Map.
remove(index) Y - - Remove a value at the index
remove(object) Y Y Y Remove an object from the collection
remove(key) - - Y Remove an object corresponding to the Key
int size() Y Y Y Return the number of elements in a collection.
Object[] toArray() Y Y - Return an array containing the elements of the collection.
A[] toArray(A[]) Y Y - Return an array containing the elements of the collection.
Important Methods for Arrays:

For the purpose of explanation in the table that follows, A[] refers to an Array that contains objects in it.

Method Name Description/Purpose
static List asList(A[]) Convert an array to a List
static int binarySearch(ObjecA[], key)

static int binarySearch(primitive[], key)
Search a sorted array for a given value, return an index or insertion point.
static int binarySearch(A[], key, Comparator) Search a Comparator-sorted array for a value.
static boolean equals(ObjecA[], ObjecA[])

static boolean equals(primitive[], primitive[])
Compare two arrays to determine if their contents are equal.
public static void sort(Object[ ] )

public static void sort(primitive[ ] )
Sort the elements of an array by natural order.
public static void sort(A[], Comparator) Sort the elements of an array using a Comparator
public static String toString(ObjecA[])

public static String toString(primitive[])
Create a String containing the contents of an array.

Important Methods for Collections:

Method Name Description/Purpose
static int binarySearch(List, key)

static int binarySearch(List, key, Comparator)
Search a “sorted” List for a given value, return an index or insertion point.
static int binarySearch(ObjecA[], key)

static int binarySearch(primitive[], key)
Search a sorted array for a given value, return an index or insertion point.
static void reverse(List) Reverse the order of elements in a List..
static Comparator reverseOrder()

static Comparator reverseOrder(Comparator)
Return a Comparator that sorts the reverse of the collection’s current sort sequence.
static void sort(List)

static void sort(List, Comparator)

Sort a List either by natural order or by a Comparator.
Previous Chapter: Chapter 49 - Priority Queue

Next Chapter: Chapter 31 - Generics

No comments:

Post a Comment

© 2013 by www.inheritingjava.blogspot.com. All rights reserved. No part of this blog or its contents may be reproduced or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the Author.

ShareThis

Google+ Followers

Followers