89 lines
3.1 KiB
Java
Raw Normal View History

2016-12-30 22:22:39 -08:00
// collectiontopics/Utilities.java
2020-10-07 13:35:40 -06:00
// (c)2020 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
2016-09-23 13:23:35 -06:00
// Visit http://OnJava8.com for more book information.
2016-01-25 18:05:55 -08:00
// Simple demonstrations of the Collections utilities
2015-06-15 17:47:35 -07:00
import java.util.*;
public class Utilities {
static List<String> list = Arrays.asList(
"one Two three Four five six one".split(" "));
public static void main(String[] args) {
2015-11-03 12:00:44 -08:00
System.out.println(list);
System.out.println("'list' disjoint (Four)?: " +
2015-06-15 17:47:35 -07:00
Collections.disjoint(list,
Collections.singletonList("Four")));
2017-01-22 16:48:11 -08:00
System.out.println(
"max: " + Collections.max(list));
System.out.println(
"min: " + Collections.min(list));
2015-12-02 09:20:27 -08:00
System.out.println(
"max w/ comparator: " + Collections.max(list,
2015-06-15 17:47:35 -07:00
String.CASE_INSENSITIVE_ORDER));
2015-12-02 09:20:27 -08:00
System.out.println(
"min w/ comparator: " + Collections.min(list,
2015-06-15 17:47:35 -07:00
String.CASE_INSENSITIVE_ORDER));
List<String> sublist =
Arrays.asList("Four five six".split(" "));
2015-11-03 12:00:44 -08:00
System.out.println("indexOfSubList: " +
2015-06-15 17:47:35 -07:00
Collections.indexOfSubList(list, sublist));
2015-11-03 12:00:44 -08:00
System.out.println("lastIndexOfSubList: " +
2015-06-15 17:47:35 -07:00
Collections.lastIndexOfSubList(list, sublist));
Collections.replaceAll(list, "one", "Yo");
2015-11-03 12:00:44 -08:00
System.out.println("replaceAll: " + list);
2015-06-15 17:47:35 -07:00
Collections.reverse(list);
2015-11-03 12:00:44 -08:00
System.out.println("reverse: " + list);
2015-06-15 17:47:35 -07:00
Collections.rotate(list, 3);
2015-11-03 12:00:44 -08:00
System.out.println("rotate: " + list);
2015-06-15 17:47:35 -07:00
List<String> source =
Arrays.asList("in the matrix".split(" "));
Collections.copy(list, source);
2015-11-03 12:00:44 -08:00
System.out.println("copy: " + list);
2015-06-15 17:47:35 -07:00
Collections.swap(list, 0, list.size() - 1);
2015-11-03 12:00:44 -08:00
System.out.println("swap: " + list);
2015-06-15 17:47:35 -07:00
Collections.shuffle(list, new Random(47));
2015-11-03 12:00:44 -08:00
System.out.println("shuffled: " + list);
2015-06-15 17:47:35 -07:00
Collections.fill(list, "pop");
2015-11-03 12:00:44 -08:00
System.out.println("fill: " + list);
System.out.println("frequency of 'pop': " +
2015-06-15 17:47:35 -07:00
Collections.frequency(list, "pop"));
2017-01-22 16:48:11 -08:00
List<String> dups =
Collections.nCopies(3, "snap");
2015-11-03 12:00:44 -08:00
System.out.println("dups: " + dups);
System.out.println("'list' disjoint 'dups'?: " +
2015-06-15 17:47:35 -07:00
Collections.disjoint(list, dups));
// Getting an old-style Enumeration:
2017-01-22 16:48:11 -08:00
Enumeration<String> e =
Collections.enumeration(dups);
2015-06-15 17:47:35 -07:00
Vector<String> v = new Vector<>();
while(e.hasMoreElements())
v.addElement(e.nextElement());
// Converting an old-style Vector
// to a List via an Enumeration:
ArrayList<String> arrayList =
Collections.list(v.elements());
2015-11-03 12:00:44 -08:00
System.out.println("arrayList: " + arrayList);
2015-06-15 17:47:35 -07:00
}
2015-09-07 11:44:36 -06:00
}
/* Output:
2015-06-15 17:47:35 -07:00
[one, Two, three, Four, five, six, one]
'list' disjoint (Four)?: false
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]
shuffled: [six, matrix, the, Four, Yo, five, in]
fill: [pop, pop, pop, pop, pop, pop, pop]
frequency of 'pop': 7
dups: [snap, snap, snap]
'list' disjoint 'dups'?: true
arrayList: [snap, snap, snap]
2015-09-07 11:44:36 -06:00
*/