Java Tutorial/Collections/Collections — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 05:05, 1 июня 2010
Содержание
- 1 A combination of two collections into a collection
- 2 Collections.reverseOrder()
- 3 Copy Elements of ArrayList to Java Vector
- 4 Copy Elements of One ArrayList to Another ArrayList
- 5 Create and demonstrate an immutable collection.
- 6 Create an empty collection object
- 7 Find maximum element of ArrayList
- 8 Find Minimum element of ArrayList
- 9 Get Enumeration over ArrayList
- 10 Get Synchronized List from ArrayList
- 11 Perform Binary Search on ArrayList
- 12 Replace All Elements Of ArrayList
- 13 Replace all occurrences of specified element of ArrayList
- 14 Reverse order of all elements of ArrayList
- 15 Rotate elements of a collection
- 16 Search collection element
- 17 Shuffle elements of ArrayList
- 18 Sort ArrayList in descending order using comparator
- 19 Sort elements of ArrayList
- 20 Sort items of an ArrayList with Collections.reverseOrder()
- 21 Swap elements of ArrayList
- 22 The Collections.fill() method
- 23 Using the Collections.synchronized methods
A combination of two collections into a collection
/* DualCollection.java
{{IS_NOTE
Purpose:
Description:
History:
Sun Sep 2 21:29:38 2007, Created by tomyeh
}}IS_NOTE
Copyright (C) 2007 Potix Corporation. All Rights Reserved.
{{IS_RIGHT
This program is distributed under GPL Version 3.0 in the hope that
it will be useful, but WITHOUT ANY WARRANTY.
}}IS_RIGHT
*/
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.AbstractCollection;
/**
* A combination of two collections into a collection.
*
* @author tomyeh
* @since 3.0.0
*/
public class DualCollection extends AbstractCollection
implements java.io.Serializable {
private final Collection _first, _second;
/** Returns a collection by combining two collections.
* It checks whether any of them is null, or equals. And, returns
* the non-null one if another is null.
* If both null, it returns null.
*/
public static final
Collection combine(Collection first, Collection second) {
if (first == second) //we don"t use equals to have better performance
return first;
if (first != null)
if (second != null)
return new DualCollection(first, second);
else
return first;
else
return second;
}
/** Constructor.
* It is better to use {@link #combine} instead of this method
* since it checks whether any of them is null or equals.
*/
public DualCollection(Collection first, Collection second) {
_first = first != null ? first: Collections.EMPTY_LIST;
_second = second != null ? second: Collections.EMPTY_LIST;
}
//Collection//
public int size() {
return _first.size() + _second.size();
}
public Iterator iterator() {
return new Iter();
}
private class Iter implements Iterator {
private Iterator _it;
private boolean _bSecond;
private Iter() {
_it = _first.iterator();
}
public boolean hasNext() {
return _it.hasNext() || (!_bSecond && !_second.isEmpty());
}
public Object next() {
if (!_bSecond && !_it.hasNext()) {
_it = _second.iterator();
_bSecond = true;
}
return _it.next();
}
public void remove() {
_it.remove();
}
}
}
Collections.reverseOrder()
import java.util.*;
class AlgorithmsDemo {
public static void main(String args[]) {
LinkedList<Integer> ll = new LinkedList<Integer>();
ll.add(-8);
ll.add(20);
ll.add(-20);
ll.add(8);
Comparator<Integer> r = Collections.reverseOrder();
Collections.sort(ll, r);
for(int i : ll)
System.out.print(i+ " ");
Collections.shuffle(ll);
System.out.print("List shuffled: ");
for(int i : ll)
System.out.print(i + " ");
System.out.println("Minimum: " + Collections.min(ll));
System.out.println("Maximum: " + Collections.max(ll));
}
}
Copy Elements of ArrayList to Java Vector
import java.util.ArrayList;
import java.util.Collections;
import java.util.Vector;
public class Main {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("1");
arrayList.add("2");
arrayList.add("3");
arrayList.add("4");
arrayList.add("5");
Vector<String> v = new Vector<String>();
v.add("A");
v.add("B");
v.add("D");
v.add("E");
v.add("F");
v.add("G");
v.add("H");
System.out.println(v);
Collections.copy(v, arrayList);
System.out.println(v);
}
}
/*
[A, B, D, E, F, G, H]
[1, 2, 3, 4, 5, G, H]
*/
Copy Elements of One ArrayList to Another ArrayList
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
ArrayList<String> arrayList1 = new ArrayList<String>();
arrayList1.add("1");
arrayList1.add("2");
arrayList1.add("3");
ArrayList<String> arrayList2 = new ArrayList<String>();
arrayList2.add("One");
arrayList2.add("Two");
arrayList2.add("Three");
arrayList2.add("Four");
arrayList2.add("Five");
System.out.println(arrayList2);
Collections.copy(arrayList2, arrayList1);
System.out.println(arrayList2);
}
}
/*
[One, Two, Three, Four, Five]
[1, 2, 3, Four, Five]
*/
Create and demonstrate an immutable collection.
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String args[]) {
List<Character> list = new ArrayList<Character>();
list.add("X");
System.out.println("Element added to list: " + list.get(0));
Collection<Character> immutableCol = Collections.unmodifiableCollection(list);
immutableCol.add("Y");
}
}
Create an empty collection object
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Main {
public static void main(String args[]) {
List list = Collections.EMPTY_LIST;
Set set = Collections.EMPTY_SET;
Map map = Collections.EMPTY_MAP;
List<String> s = Collections.emptyList();
Set<Long> l = Collections.emptySet();
Map<Date, String> d = Collections.emptyMap();
}
}
Find maximum element of ArrayList
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> arrayList = new ArrayList<Integer>();
arrayList.add(new Integer("3"));
arrayList.add(new Integer("1"));
arrayList.add(new Integer("8"));
arrayList.add(new Integer("3"));
arrayList.add(new Integer("5"));
Object obj = Collections.max(arrayList);
System.out.println(obj);
}
}
//8
Find Minimum element of ArrayList
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> arrayList = new ArrayList<Integer>();
arrayList.add(new Integer("1"));
arrayList.add(new Integer("2"));
arrayList.add(new Integer("3"));
arrayList.add(new Integer("4"));
arrayList.add(new Integer("5"));
Object obj = Collections.min(arrayList);
System.out.println(obj);
}
}
Get Enumeration over ArrayList
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
public class Main {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("A");
arrayList.add("B");
arrayList.add("D");
arrayList.add("E");
arrayList.add("F");
Enumeration e = Collections.enumeration(arrayList);
while (e.hasMoreElements())
System.out.println(e.nextElement());
}
}
Get Synchronized List from ArrayList
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String[] args) {
ArrayList arrayList = new ArrayList();
List list = Collections.synchronizedList(arrayList);
}
}
Perform Binary Search on ArrayList
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("1");
arrayList.add("4");
arrayList.add("2");
arrayList.add("5");
arrayList.add("3");
Collections.sort(arrayList);
System.out.println("Sorted ArrayList contains : " + arrayList);
int index = Collections.binarySearch(arrayList, "4");
System.out.println("Element found at : " + index);
}
}
Replace All Elements Of ArrayList
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("A");
arrayList.add("B");
arrayList.add("D");
System.out.println(arrayList);
Collections.fill(arrayList, "REPLACED");
System.out.println(arrayList);
}
}
/*
[A, B, D]
[REPLACED, REPLACED, REPLACED]
*/
Replace all occurrences of specified element of ArrayList
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("A");
arrayList.add("B");
arrayList.add("A");
arrayList.add("C");
arrayList.add("D");
System.out.println(arrayList);
Collections.replaceAll(arrayList, "A", "Replace All");
System.out.println(arrayList);
}
}
/*
[A, B, A, C, D]
[Replace All, B, Replace All, C, D]
*/
Reverse order of all elements of ArrayList
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("A");
arrayList.add("B");
arrayList.add("C");
arrayList.add("D");
arrayList.add("E");
System.out.println(arrayList);
Collections.reverse(arrayList);
System.out.println(arrayList);
}
}
Rotate elements of a collection
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String[] args) {
List numbers = new ArrayList();
for (int i = 0; i < 25; i++) {
numbers.add(i);
}
System.out.println(Arrays.toString(numbers.toArray()));
Collections.rotate(numbers, 10);
System.out.println(Arrays.toString(numbers.toArray()));
}
}
Search collection element
import java.text.DateFormatSymbols;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List list = new LinkedList();
DateFormatSymbols dfs = new DateFormatSymbols();
String[] months = dfs.getMonths();
for (int i = 0; i < months.length; i++) {
String month = months[i];
list.add(month);
}
Collections.sort(list);
System.out.println("Month Names = " + list);
int index = Collections.binarySearch(list, "October");
if (index > 0) {
System.out.println("Found at index = " + index);
String month = (String) list.get(index);
System.out.println("Month = " + month);
}
}
}
Shuffle elements of ArrayList
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("A");
arrayList.add("B");
arrayList.add("C");
arrayList.add("D");
arrayList.add("E");
System.out.println(arrayList);
Collections.shuffle(arrayList);
System.out.println(arrayList);
}
}
/*
[A, B, C, D, E]
[E, D, A, C, B]
*/
Sort ArrayList in descending order using comparator
import java.util.ArrayList;
import java.util.Collections;
import java.util.ruparator;
public class Main {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("A");
arrayList.add("B");
arrayList.add("C");
arrayList.add("D");
arrayList.add("E");
Comparator comparator = Collections.reverseOrder();
System.out.println(arrayList);
Collections.sort(arrayList, comparator);
System.out.println(arrayList);
}
}
Sort elements of ArrayList
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("1");
arrayList.add("3");
arrayList.add("5");
arrayList.add("2");
arrayList.add("4");
Collections.sort(arrayList);
for (String str: arrayList)
System.out.println(str);
}
}
/*
1
2
3
4
5
*/
Sort items of an ArrayList with Collections.reverseOrder()
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> colours = new ArrayList<String>();
colours.add("red");
colours.add("green");
colours.add("blue");
colours.add("yellow");
colours.add("cyan");
colours.add("white");
colours.add("black");
Collections.sort(colours);
System.out.println(Arrays.toString(colours.toArray()));
Collections.sort(colours, Collections.reverseOrder());
System.out.println(Arrays.toString(colours.toArray()));
}
}
Swap elements of ArrayList
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("A");
arrayList.add("B");
arrayList.add("C");
arrayList.add("D");
arrayList.add("E");
System.out.println(arrayList);
Collections.swap(arrayList, 0, 4);
System.out.println(arrayList);
}
}
/*
[A, B, C, D, E]
[E, B, C, D, A]
*/
The Collections.fill() method
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MainClass {
public static void main(String[] args) {
List list = new ArrayList();
for (int i = 0; i < 10; i++)
list.add("");
Collections.fill(list, "Hello");
System.out.println(list);
}
}
//
[Hello, Hello, Hello, Hello, Hello, Hello, Hello, Hello, Hello, Hello]
Using the Collections.synchronized methods
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class MainClass {
public static void main(String[] args) {
Collection c = Collections.synchronizedCollection(new ArrayList());
List list = Collections.synchronizedList(new ArrayList());
Set s = Collections.synchronizedSet(new HashSet());
Map m = Collections.synchronizedMap(new HashMap());
}
}