шпаргалки
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.

126 lines
4.8 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 collectionexample;
import java.io.File;
import java.io.FileReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Comparator;
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.TreeSet;
/**
*
* @author denis
*/
public class CollectionExample {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//collectionInteger();
collectionPerson();
}
public static void collectionPerson(){
System.out.println("-----------------ArrayList------------------");
List<Person> intArray = new ArrayList<>();
intArray.add(new Person("Boby", 11));
intArray.add(new Person("Andrey", 4));
intArray.add(new Person("Boby", 1));
intArray.add(new Person("Boby", 10));
intArray.add(new Person("Boby", 5));
intArray.forEach(System.out::println);
System.out.println("-----------------LinkedList------------------");
List<Person> intLink = new LinkedList<>(intArray);
intLink.forEach(System.out::println);
System.out.println("-----------------HashSet------------------");
Set<Person> intHashSet = new HashSet<>(intArray);
intHashSet.forEach(System.out::println);
System.out.println("-----------------TreeSet------------------");
// Set<Person> intTreeSet = new TreeSet<>(new Comparator<Person>() {
// @Override
// public int compare(Person o1, Person o2) {
// int res = o2.name.compareTo(o1.name);
// if(res==0) res = o2.phone.compareTo(o1.phone);
// return res;
// }
// });
Set<Person> intTreeSet = new TreeSet<>((o1, o2) -> {
int res = o2.name.compareTo(o1.name);
if(res==0) res = o2.phone.compareTo(o1.phone);
return res;
});
//Set<Person> intTreeSet = new TreeSet<>((o1, o2) -> o2.name.compareTo(o1.name));
intTreeSet.addAll(intLink);
intTreeSet.forEach(System.out::println);
System.out.println("-----------------HashMap------------------");
Map<Integer, Person> maps = new HashMap<>();
maps.put(intArray.get(0).phone, intArray.get(0));
maps.put(intArray.get(1).phone, intArray.get(1));
maps.put(intArray.get(2).phone, intArray.get(2));
maps.put(intArray.get(3).phone, intArray.get(3));
maps.put(intArray.get(4).phone, intArray.get(4));
maps.forEach((e1, e2) -> {System.out.println(e1 + ": " + e2);});
}
public static void collectionInteger(){
System.out.println("-----------------ArrayList------------------");
List<Integer> intArray = new ArrayList<>();
intArray.add(50);
intArray.add(26);
intArray.add(26);
intArray.add(50);
intArray.add(50);
intArray.forEach(System.out::println);
System.out.println("-----------------LinkedList------------------");
List<Integer> intLink = new LinkedList<>(intArray);
intLink.forEach(System.out::println);
System.out.println("-----------------HashSet------------------");
Set<Integer> intHashSet = new HashSet<>(intArray);
intHashSet.forEach(System.out::println);
System.out.println("-----------------TreeSet------------------");
Set<Integer> intTreeSet = new TreeSet<>(intArray);
intTreeSet.forEach(System.out::println);
System.out.println("-----------------HashMap------------------");
Map<String, Integer> maps = new HashMap<>();
maps.put("One", intArray.get(0));
maps.put("Two", intArray.get(1));
maps.put("Three", intArray.get(2));
maps.put("Four", intArray.get(3));
maps.put("Five", intArray.get(4));
maps.forEach((e1, e2) -> {System.out.println(e1 + ": " + e2);});
System.out.println("-----------------LinkedHashMap------------------");
Map<String, Integer> mapsLink = new LinkedHashMap<>();
mapsLink.put("One", intArray.get(0));
mapsLink.put("Two", intArray.get(1));
mapsLink.put("Three", intArray.get(2));
mapsLink.put("Four", intArray.get(3));
mapsLink.put("Five", intArray.get(4));
mapsLink.forEach((e1, e2) -> {System.out.println(e1 + ": " + e2);});
}
}