OnJava8-Examples/collections/PrintingCollections.java

45 lines
1.3 KiB
Java
Raw Permalink Normal View History

2015-12-15 11:47:04 -08:00
// collections/PrintingCollections.java
// (c)2021 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
// Collections print themselves automatically
2015-06-15 17:47:35 -07:00
import java.util.*;
2015-12-15 11:47:04 -08:00
public class PrintingCollections {
static Collection
fill(Collection<String> collection) {
2015-06-15 17:47:35 -07:00
collection.add("rat");
collection.add("cat");
collection.add("dog");
collection.add("dog");
return collection;
}
2015-09-07 11:44:36 -06:00
static Map fill(Map<String, String> map) {
2015-06-15 17:47:35 -07:00
map.put("rat", "Fuzzy");
map.put("cat", "Rags");
map.put("dog", "Bosco");
map.put("dog", "Spot");
return map;
}
public static void main(String[] args) {
2015-11-03 12:00:44 -08:00
System.out.println(fill(new ArrayList<>()));
System.out.println(fill(new LinkedList<>()));
System.out.println(fill(new HashSet<>()));
System.out.println(fill(new TreeSet<>()));
System.out.println(fill(new LinkedHashSet<>()));
System.out.println(fill(new HashMap<>()));
System.out.println(fill(new TreeMap<>()));
System.out.println(fill(new LinkedHashMap<>()));
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
[rat, cat, dog, dog]
[rat, cat, dog, dog]
[rat, cat, dog]
[cat, dog, rat]
[rat, cat, dog]
{rat=Fuzzy, cat=Rags, dog=Spot}
{cat=Rags, dog=Spot, rat=Fuzzy}
{rat=Fuzzy, cat=Rags, dog=Spot}
2015-09-07 11:44:36 -06:00
*/