Collections Utilities Demo

From Java Example Source Code

Jump to: navigation, search

Contents

[edit] Overview - Collections Utilities Demo

This Java example is a simple demonstration of the Collections utilities.

[edit] Java Source Code

  • Package: com.bruceeckel
  • File: Utilities.java
package com.bruceeckel;
 
//: c11:Utilities.java
//Simple demonstrations of the Collections utilities.
//From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
//www.BruceEckel.com. See copyright notice in CopyRight.txt.
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.List;
import java.util.Vector;
 
public class Utilities {
 
    public static void main(String[] args) {
	List list = Arrays.asList("one Two three Four five six one".split(" "));
	System.out.println(list);
	System.out.println("max: " + Collections.max(list));
	System.out.println("min: " + Collections.min(list));
	AlphabeticComparator comp = new AlphabeticComparator();
	System.out.println("max w/ comparator: " + Collections.max(list, comp));
	System.out.println("min w/ comparator: " + Collections.min(list, comp));
	List sublist = Arrays.asList("Four five six".split(" "));
	System.out.println("indexOfSubList: " + Collections.indexOfSubList(list, sublist));
	System.out.println("lastIndexOfSubList: " + Collections.lastIndexOfSubList(list, sublist));
	Collections.replaceAll(list, "one", "Yo");
	System.out.println("replaceAll: " + list);
	Collections.reverse(list);
	System.out.println("reverse: " + list);
	Collections.rotate(list, 3);
	System.out.println("rotate: " + list);
	List source = Arrays.asList("in the matrix".split(" "));
	Collections.copy(list, source);
	System.out.println("copy: " + list);
	Collections.swap(list, 0, list.size() - 1);
	System.out.println("swap: " + list);
	Collections.fill(list, "pop");
	System.out.println("fill: " + list);
	List dups = Collections.nCopies(3, "snap");
	System.out.println("dups: " + dups);
	// Getting an old-style Enumeration:
	Enumeration e = Collections.enumeration(dups);
	Vector v = new Vector();
	while (e.hasMoreElements())
	    v.addElement(e.nextElement());
	// Converting an old-style Vector
	// to a List via an Enumeration:
	ArrayList arrayList = Collections.list(v.elements());
	System.out.println("arrayList: " + arrayList);
 
    }
} // /:~

Package:com.bruceeckel

File: AlphabeticComparator.java

package com.bruceeckel;
 
//www.BruceEckel.com. See copyright notice in CopyRight.txt.
//modified by Code Panda; This class is used several times in those examples, so, move it into a public class.
import java.util.Comparator;
 
public class AlphabeticComparator implements Comparator {
    public int compare(Object o1, Object o2) {
	String s1 = (String) o1;
	String s2 = (String) o2;
	return s1.toLowerCase().compareTo(s2.toLowerCase());
    }
}

[edit] What Result You Can Get

Run the program, you will get:


[one, Two, three, Four, five, six, one]
max: three
min: Four
max w/ comparator: Two
min w/ comparator: five
indexOfSubList: 3
lastIndexOfSubList: 3
replaceAll: [Yo, Two, three, Four, five, six, Yo]
reverse: [Yo, six, five, Four, three, Two, Yo]
rotate: [three, Two, Yo, Yo, six, five, Four]
copy: [in, the, matrix, Yo, six, five, Four]
swap: [Four, the, matrix, Yo, six, five, in]
fill: [pop, pop, pop, pop, pop, pop, pop]
dups: [snap, snap, snap]
arrayList: [snap, snap, snap]


[edit] Required External Library for this Java Example

Need nothing.


[edit] How to Run this Java Example Program

We recommend running this Java example program with Eclipse.

For assistance in working with Eclipse, please see How to Run Java Program with Eclipse.

It's fairly easy.



[edit] Question & Answer

Any question?

Click edit and post your question or answer here.


Personal tools