You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
132 lines
5.0 KiB
132 lines
5.0 KiB
2 years ago
|
/*
|
||
|
* To change this license header, choose License Headers in Project Properties.
|
||
|
* To change this template file, choose Tools | Templates
|
||
|
* and open the template in the editor.
|
||
|
*/
|
||
|
package collectionexample2;
|
||
|
|
||
|
import java.util.ArrayList;
|
||
|
import java.util.HashMap;
|
||
|
import java.util.HashSet;
|
||
|
import java.util.LinkedHashMap;
|
||
|
import java.util.LinkedList;
|
||
|
import java.util.List;
|
||
|
import java.util.Map;
|
||
|
import java.util.Set;
|
||
|
import java.util.SortedSet;
|
||
|
import java.util.TreeSet;
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
* @author denis
|
||
|
*/
|
||
|
public class CollectionExample2 {
|
||
|
|
||
|
/**
|
||
|
* @param args the command line arguments
|
||
|
*/
|
||
|
public static void main(String[] args) {
|
||
|
unique();
|
||
|
//simple();
|
||
|
|
||
|
}
|
||
|
|
||
|
private static void unique(){
|
||
|
System.out.println("--------------------ArrayList-------------------------");
|
||
|
List<User> arrayList = new ArrayList<>();
|
||
|
arrayList.add(new User("Andrey", 15));
|
||
|
arrayList.add(new User("Andrey", 20));
|
||
|
arrayList.add(new User("Andrey", 15));
|
||
|
arrayList.add(new User("Andrey", 16));
|
||
|
System.out.println("contains=" + arrayList.contains(new User("Andrey", 15)));
|
||
|
arrayList.forEach(System.out::println);
|
||
|
|
||
|
{
|
||
|
System.out.println("--------------------LinkedList-------------------------");
|
||
|
List<User> collection = new LinkedList<>(arrayList);
|
||
|
collection.forEach(System.out::println);
|
||
|
}
|
||
|
|
||
|
System.out.println("--------------------HashSet-------------------------");
|
||
|
{
|
||
|
System.out.println("--------------------LinkedList-------------------------");
|
||
|
Set<User> collection = new HashSet<>(arrayList);
|
||
|
collection.forEach(System.out::println);
|
||
|
}
|
||
|
|
||
|
System.out.println("--------------------TreeSet-------------------------");
|
||
|
{
|
||
|
System.out.println("--------------------LinkedList-------------------------");
|
||
|
SortedSet<User> collection = new TreeSet<>(arrayList);
|
||
|
collection.forEach(System.out::println);
|
||
|
}
|
||
|
|
||
|
System.out.println("--------------------HashMap-------------------------");
|
||
|
{
|
||
|
Map<Integer, User> mapColl = new HashMap<>();
|
||
|
mapColl.put(1, new User("Andrey", 15));
|
||
|
mapColl.put(10, new User("Andrey", 15));
|
||
|
mapColl.put(5, new User("Andrey", 20));
|
||
|
mapColl.put(9, new User("Andrey", 16));
|
||
|
System.out.println("contains=" + mapColl.containsValue(new User("Andrey", 15)));
|
||
|
mapColl.entrySet().forEach(e -> System.out.println(e));
|
||
|
}
|
||
|
|
||
|
System.out.println("--------------------LinkedHashMap-------------------------");
|
||
|
{
|
||
|
Map<Integer, User> mapColl = new LinkedHashMap<>();
|
||
|
mapColl.put(1, new User("Andrey", 15));
|
||
|
mapColl.put(10, new User("Andrey", 15));
|
||
|
mapColl.put(5, new User("Andrey", 20));
|
||
|
mapColl.put(9, new User("Andrey", 16));
|
||
|
System.out.println("contains=" + mapColl.containsValue(new User("Andrey", 15)));
|
||
|
mapColl.entrySet().forEach(e -> System.out.println(e));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private static void simple(){
|
||
|
System.out.println("--------------------ArrayList-------------------------");
|
||
|
List<String> arrayList = new ArrayList<>();
|
||
|
arrayList.add("Once");
|
||
|
arrayList.add("Two");
|
||
|
arrayList.add("Two");
|
||
|
arrayList.add("Two");
|
||
|
arrayList.add("Three");
|
||
|
arrayList.add("Four");
|
||
|
arrayList.add("Five");
|
||
|
arrayList.add("Five");
|
||
|
arrayList.forEach(System.out::println);
|
||
|
|
||
|
System.out.println("--------------------LinkedList-------------------------");
|
||
|
List<String> linkedList = new LinkedList<>(arrayList);
|
||
|
linkedList.forEach(System.out::println);
|
||
|
|
||
|
System.out.println("--------------------HashSet-------------------------");
|
||
|
Set<String> hashSet = new HashSet<>(arrayList);
|
||
|
hashSet.forEach(System.out::println);
|
||
|
|
||
|
System.out.println("--------------------TreeSet-------------------------");
|
||
|
SortedSet<String> sorted = new TreeSet<>(arrayList);
|
||
|
sorted.forEach(System.out::println);
|
||
|
|
||
|
System.out.println("--------------------HashMap-------------------------");
|
||
|
Map<Integer, String> mapColl = new HashMap<>();
|
||
|
mapColl.put(1, "Once");
|
||
|
mapColl.put(10, "Once");
|
||
|
mapColl.put(5, "Two");
|
||
|
mapColl.put(9, null);
|
||
|
mapColl.put(0, "Five");
|
||
|
mapColl.entrySet().forEach(e -> System.out.println(e));
|
||
|
|
||
|
System.out.println("--------------------LinkedHashMap-------------------------");
|
||
|
Map<Integer, String> hashMap = new LinkedHashMap<>();
|
||
|
hashMap.put(1, "Once");
|
||
|
hashMap.put(10, "Once");
|
||
|
hashMap.put(5, "Two");
|
||
|
hashMap.put(9, null);
|
||
|
hashMap.put(0, "Five");
|
||
|
hashMap.entrySet().forEach(e -> System.out.println(e));
|
||
|
}
|
||
|
|
||
|
}
|