Java by API/java.util/Collections
Содержание
- 1 Collections: binarySearch(List list, T key)
- 2 Collections: comparator()
- 3 Collections: copy(List dest, List src)
- 4 Collections: emptyList()
- 5 Collections: emptyMap()
- 6 Collections: emptySet()
- 7 Collections: enumeration(Collection c)
- 8 Collections: fill(List<? super T> list, R obj)
- 9 Collections: list(Enumeration e)
- 10 Collections: max(Collection < ? extendsoll)
- 11 Collections: min(Collection < ? extendsoll)
- 12 Collections: nCopies(int n, Object o)
- 13 Collections: replaceAll(List list, T oldVal, T newVal)
- 14 Collections: reverse(List<?> list)
- 15 Collections: reverseOrder()
- 16 Collections: rotate(List<?> list, int distance)
- 17 Collections: shuffle(List <ist,Random rnd)
- 18 Collections: singletonList(T o)
- 19 Collections: singletonMap(T key, R value)
- 20 Collections: singleton(T o)
- 21 Collections: sort(List<String> list)
- 22 Collections: sort(List <ist, Comparator < ? super)
- 23 Collections: swap(List<?> list, int i, int j)
- 24 Collections: synchronizedCollection(Collection c)
- 25 Collections: synchronizedList(List list)
- 26 Collections: synchronizedMap(Map m)
- 27 Collections: synchronizedSet(Set s)
- 28 Collections: synchronizedSet(SortedSet s)
- 29 Collections: synchronizedSortedMap(SortedMap m)
- 30 Collections: unmodifiableCollection(Collection<?> c)
- 31 Collections: unmodifiableList(List<? extends T> list)
- 32 Collections: unmodifiableMap(Map<K, V> m)
- 33 Collections: unmodifiableSet(Set s)
Collections: binarySearch(List list, T key)
/*
Output:
Sorted list: [length: 7]
[B, H, H, L, M, M, R]
Found M @ 5
Didn"t find J @ -4
* */
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class MainClass {
public static void main(String args[]) {
String simpsons[] = { "B", "H", "L", "M", "H", "M", "R" };
List list = new ArrayList(Arrays.asList(simpsons));
// Ensure list sorted
Collections.sort(list);
System.out.println("Sorted list: [length: " + list.size() + "]");
System.out.println(list);
// Search for element in list
int index = Collections.binarySearch(list, "M");
System.out.println("Found M @ " + index);
// Search for element not in list
index = Collections.binarySearch(list, "J");
System.out.println("Didn"t find J @ " + index);
}
}
Collections: comparator()
/*
Output:
[S, P, G, E]
java.util.Collections$ReverseComparator@192d342
* */
import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;
public class MainClass {
public static void main(String args[]) throws Exception {
String elements[] = { "S", "P", "E","G", "P" };
Set set = new TreeSet(Collections.reverseOrder());
for (int i = 0, n = elements.length; i < n; i++) {
set.add(elements[i]);
}
System.out.println(set);
System.out.println(((TreeSet) set).ruparator());
}
}
Collections: copy(List dest, List src)
/*
Output:
[U, C, L]
Exception in thread "main" java.lang.IndexOutOfBoundsException: Source does not fit in dest
at java.util.Collections.copy(Collections.java:531)
at MainClass.main(MainClass.java:21)
* */
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class MainClass {
public static void main(String args[]) throws Exception {
List wineMakers = Arrays.asList(new String[] { "U", "C" });
List barFlies = Arrays.asList(new String[] { "U", "C", "L" });
Collections.copy(barFlies, wineMakers);
System.out.println(barFlies);
Collections.copy(wineMakers, barFlies);
}
}
Collections: emptyList()
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();
}
}
Collections: emptyMap()
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();
}
}
Collections: emptySet()
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();
}
}
Collections: enumeration(Collection c)
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Vector;
public class Main {
public static void main(String args[]) throws Exception {
Vector<String> v = new Vector<String>();
v.add("a");
v.add("b");
v.add("c");
Collection<String> col = v;
Enumeration<String> e = Collections.enumeration(col);
for (; e.hasMoreElements();) {
Object o = e.nextElement();
System.out.println(o);
}
}
}
Collections: fill(List<? super T> list, R obj)
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
for (int i = 0; i < 10; i++)
list.add("");
Collections.fill(list, "Hello");
System.out.println(list);
}
}
Collections: list(Enumeration e)
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Vector;
public class Main {
public static void main(String[] args) {
Vector<String> v = new Vector<String>();
v.add("A");
v.add("B");
v.add("D");
v.add("E");
v.add("F");
System.out.println(v);
Enumeration<String> e = v.elements();
ArrayList<String> aList = Collections.list(e);
System.out.println(aList);
}
}
Collections: max(Collection < ? extendsoll)
/**
*Output:
List sorted in reverse: 20 8 -8 -20
List shuffled: 20 -20 -8 8
Minimum: -20
Maximum: 20
*/
import java.util.Collections;
import java.util.ruparator;
import java.util.LinkedList;
public class MainClass {
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);
System.out.print("List sorted in reverse: ");
for(int i : ll){
System.out.print(i+ " ");
}
System.out.println();
Collections.shuffle(ll);
System.out.print("List shuffled: ");
for(int i : ll)
System.out.print(i + " ");
System.out.println();
System.out.println("Minimum: " + Collections.min(ll));
System.out.println("Maximum: " + Collections.max(ll));
}
}
Collections: min(Collection < ? extendsoll)
/**
*Output:
List sorted in reverse: 20 8 -8 -20
List shuffled: 20 -20 -8 8
Minimum: -20
Maximum: 20
*/
import java.util.Collections;
import java.util.ruparator;
import java.util.LinkedList;
public class MainClass {
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);
System.out.print("List sorted in reverse: ");
for(int i : ll){
System.out.print(i+ " ");
}
System.out.println();
Collections.shuffle(ll);
System.out.print("List shuffled: ");
for(int i : ll)
System.out.print(i + " ");
System.out.println();
System.out.println("Minimum: " + Collections.min(ll));
System.out.println("Maximum: " + Collections.max(ll));
}
}
Collections: nCopies(int n, Object o)
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class Main {
public static void main(String[] args) {
List list = Collections.nCopies(5, "A");
Iterator itr = list.iterator();
while (itr.hasNext())
System.out.println(itr.next());
}
}
Collections: replaceAll(List list, T oldVal, T newVal)
import java.util.Collections;
import java.util.Vector;
public class Main {
public static void main(String[] args) {
Vector<String> v = new Vector<String>();
v.add("A");
v.add("B");
v.add("A");
v.add("C");
v.add("D");
System.out.println(v);
Collections.replaceAll(v, "A", "Replace All");
System.out.println(v);
}
}
Collections: reverse(List<?> list)
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);
}
}
Collections: reverseOrder()
/**
*Output:
List sorted in reverse: 20 8 -8 -20
List shuffled: 20 -20 -8 8
Minimum: -20
Maximum: 20
*/
import java.util.Collections;
import java.util.ruparator;
import java.util.LinkedList;
public class MainClass {
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);
System.out.print("List sorted in reverse: ");
for(int i : ll){
System.out.print(i+ " ");
}
System.out.println();
Collections.shuffle(ll);
System.out.print("List shuffled: ");
for(int i : ll)
System.out.print(i + " ");
System.out.println();
System.out.println("Minimum: " + Collections.min(ll));
System.out.println("Maximum: " + Collections.max(ll));
}
}
Collections: rotate(List<?> list, int distance)
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()));
}
}
Collections: shuffle(List <ist,Random rnd)
/**
*Output:
List sorted in reverse: 20 8 -8 -20
List shuffled: 20 -20 -8 8
Minimum: -20
Maximum: 20
*/
import java.util.Collections;
import java.util.ruparator;
import java.util.LinkedList;
public class MainClass {
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);
System.out.print("List sorted in reverse: ");
for(int i : ll){
System.out.print(i+ " ");
}
System.out.println();
Collections.shuffle(ll);
System.out.print("List shuffled: ");
for(int i : ll)
System.out.print(i + " ");
System.out.println();
System.out.println("Minimum: " + Collections.min(ll));
System.out.println("Maximum: " + Collections.max(ll));
}
}
Collections: singletonList(T o)
import java.util.Collections;
public class Main {
public static void main(String[] a){
System.out.println(Collections.singletonList("a"));
System.out.println(Collections.singleton("a"));
System.out.println(Collections.singletonMap("a","b"));
}
}
Collections: singletonMap(T key, R value)
import java.util.Collections;
public class Main {
public static void main(String[] a){
System.out.println(Collections.singletonList("a"));
System.out.println(Collections.singleton("a"));
System.out.println(Collections.singletonMap("a","b"));
}
}
Collections: singleton(T o)
/*
Output:
[Two, Three, One, Two, Three]
[Two, Three, Two, Three]
* */
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class MainClass {
public static void main(String args[]) {
String init[] = { "One", "Two", "Three", "One", "Two", "Three" };
List list1 = new ArrayList(Arrays.asList(init));
List list2 = new ArrayList(Arrays.asList(init));
list1.remove("One");
System.out.println(list1);
list2.removeAll(Collections.singleton("One"));
System.out.println(list2);
}
}
Collections: sort(List<String> list)
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> list = Arrays.asList(args);
Collections.sort(list);
System.out.println(list);
}
}
Collections: sort(List <ist, Comparator < ? super)
/**
*Output:
List sorted in reverse: 20 8 -8 -20
List shuffled: 20 -20 -8 8
Minimum: -20
Maximum: 20
*/
import java.util.Collections;
import java.util.ruparator;
import java.util.LinkedList;
public class MainClass {
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);
System.out.print("List sorted in reverse: ");
for(int i : ll){
System.out.print(i+ " ");
}
System.out.println();
Collections.shuffle(ll);
System.out.print("List shuffled: ");
for(int i : ll)
System.out.print(i + " ");
System.out.println();
System.out.println("Minimum: " + Collections.min(ll));
System.out.println("Maximum: " + Collections.max(ll));
}
}
Collections: swap(List<?> list, int i, int j)
import java.util.Collections;
import java.util.Vector;
public class Main {
public static void main(String[] args) {
Vector<String> v = new Vector<String>();
v.add("1");
v.add("2");
v.add("3");
v.add("4");
v.add("5");
System.out.println(v);
Collections.swap(v, 0, 4);
System.out.println(v);
}
}
Collections: synchronizedCollection(Collection c)
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 Main {
public static void main(String[] args) {
Collection<String> c = Collections.synchronizedCollection(new ArrayList<String>());
List<String> list = Collections.synchronizedList(new ArrayList<String>());
Set<String> s = Collections.synchronizedSet(new HashSet<String>());
Map<String,String> m = Collections.synchronizedMap(new HashMap<String,String>());
}
}
Collections: synchronizedList(List list)
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 Main {
public static void main(String[] args) {
Collection<String> c = Collections.synchronizedCollection(new ArrayList<String>());
List<String> list = Collections.synchronizedList(new ArrayList<String>());
Set<String> s = Collections.synchronizedSet(new HashSet<String>());
Map<String,String> m = Collections.synchronizedMap(new HashMap<String,String>());
}
}
Collections: synchronizedMap(Map m)
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 Main {
public static void main(String[] args) {
Collection<String> c = Collections.synchronizedCollection(new ArrayList<String>());
List<String> list = Collections.synchronizedList(new ArrayList<String>());
Set<String> s = Collections.synchronizedSet(new HashSet<String>());
Map<String,String> m = Collections.synchronizedMap(new HashMap<String,String>());
}
}
Collections: synchronizedSet(Set s)
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 Main {
public static void main(String[] args) {
Collection<String> c = Collections.synchronizedCollection(new ArrayList<String>());
List<String> list = Collections.synchronizedList(new ArrayList<String>());
Set<String> s = Collections.synchronizedSet(new HashSet<String>());
Map<String,String> m = Collections.synchronizedMap(new HashMap<String,String>());
}
}
Collections: synchronizedSet(SortedSet s)
/*
Output:
H
B
L
* */
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class MainClass {
public static void main(String args[]) {
Set simpsons = new HashSet();
simpsons.add("B");
simpsons.add("H");
simpsons.add("L");
simpsons = Collections.synchronizedSet(simpsons);
synchronized (simpsons) {
Iterator iter = simpsons.iterator();
while (iter.hasNext()) {
System.out.println(iter.next());
}
}
Map map = Collections.synchronizedMap(new HashMap(89));
Set set = map.entrySet();
synchronized (map) {
Iterator iter = set.iterator();
while (iter.hasNext()) {
System.out.println(iter.next());
}
}
}
}
Collections: synchronizedSortedMap(SortedMap m)
/*
Output:
H
B
L
* */
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class MainClass {
public static void main(String args[]) {
Set simpsons = new HashSet();
simpsons.add("B");
simpsons.add("H");
simpsons.add("L");
simpsons = Collections.synchronizedSet(simpsons);
synchronized (simpsons) {
Iterator iter = simpsons.iterator();
while (iter.hasNext()) {
System.out.println(iter.next());
}
}
Map map = Collections.synchronizedMap(new HashMap(89));
Set set = map.entrySet();
synchronized (map) {
Iterator iter = set.iterator();
while (iter.hasNext()) {
System.out.println(iter.next());
}
}
}
}
Collections: unmodifiableCollection(Collection<?> c)
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");
}
}
Collections: unmodifiableList(List<? extends T> list)
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String args[]) throws Exception {
List<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
list = Collections.unmodifiableList(list);
list.add(1, "G");
System.out.println(list);
}
}
Collections: unmodifiableMap(Map<K, V> m)
import java.util.Collections;
import java.util.Hashtable;
import java.util.Map;
public class Main {
public static void main(String[] s) {
Hashtable<String,String> table = new Hashtable<String,String>();
table.put("key1", "value1");
table.put("key2", "value2");
table.put("key3", "value3");
Map m = Collections.unmodifiableMap(table);
m.put("key3", "value3");
System.out.println(m);
}
}
Collections: unmodifiableSet(Set s)
/*
22.5
5
354.5676
*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import javax.swing.JPanel;
/**
* Demonstrates the use of instance comparisons.
*
* @author
* @version $Revision: 1.3 $
*/
public class InstanceOfDemo {
/** A set of demon objects. */
public static final Set OBJECT_SET;
static {
Set objectSet = new HashSet();
objectSet.add(new Integer(5));
objectSet.add(new String("Hardcore Java"));
objectSet.add(new Float(22.5f));
objectSet.add(new JPanel());
objectSet.add(new Character("x"));
objectSet.add(new ArrayList());
objectSet.add(new Double(354.5676));
objectSet.add(null);
OBJECT_SET = Collections.unmodifiableSet(objectSet);
}
/**
* Demo method.
*
* @param args Command Line arguments.
*/
public static void main(final String[] args) {
final Iterator iter = OBJECT_SET.iterator();
Object obj = null;
while (iter.hasNext()) {
obj = iter.next();
if (obj instanceof Number) {
System.out.println(obj);
}
}
}
}
/* ########## End of File ########## */