자바 에서 Set,TreeSet 인터페이스
35482 단어 Java
Set( )
: ( )
HashSet
Person
Person
set/get
toString
set 추가 문자열
public static void fun1() {
// HashSet
// HashSet
Hash set = new HashSet<>();
// d d
boolean b1 = set.add("d");
boolean b2 = set.add("d");
set.add("a");
set.add("b");
set.add("c");
// ( )
System.out.println(b1);
System.out.println(b2);
System.out.println(set);
}
// HashSet 6
// equals HashCode
HashSet p = new HashSet<>();
p.add(new Person("aa", 16));
p.add(new Person("aa", 16));
p.add(new Person("bb", 16));
p.add(new Person("bb", 16));
p.add(new Person("cc", 16));
p.add(new Person("cc", 16));
// foreach
for (Person person : p){
System.out.println(person);
}
// Person
//hashCode equals
// hashCode equals
@Override
public int hashCode() {
// 31
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
//
@Override
public boolean equals(Object obj) {
if (this == obj)//
return true;// true
if (obj == null)//
return false;// false
if (getClass() != obj.getClass())//
return false; // false
Person other = (Person) obj; // ( )
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;// equals
return true;
}
// 10 1 20 ,
HashSet set = new HashSet<>();
// < 10
while(set.size()){
int num = (int)(Math.random()*20 - 1);
set.add(num);
}
System.out.println(set);
linkedHashSet
linkedHashSet Hashset
: ( )
linkedHashSet set = new linkedHashSet<>();
set.add("z");
set.add("j");
set.add("a");
set.add("a");
set.add("v");
set.add("a");
System.out.println(set);
//
Scanner scanner = new Scanner(System.in);
String string = scanner.nextLine();
LinkedHashSet set = new LinkedHashSet<>();
char[] charArray = string.toCharArray();
for (int i = 0; i < charArray.length; i++) {
set.add(charArray[i]);
}
System.out.println(set);
// set ArrayList
//( ArrayList)
ArrayList aList = new ArrayList<>();
aList.add("a");
aList.add("a");
aList.add("b");
aList.add("b");
aList.add("c");
aList.add("c");
LinkedHashSet set = new LinkedHashSet<>();
// aList set
set.addAll(aList);
// aList
aList.clear();
// set aList
aList.addAll(set);
//
System.out.println(aList);
TreeSet
TreeSet( : )
( )
:
( )
Worker Person
// TreeSet
TreeSet set = new TreeSet<>();
set.add(1);
set.add(13);
set.add(1);
set.add(2);
set.add(3);
set.add(4);
//
for(Integer integer : set){
System.out.println(integer);
}
// TreeSet 4
// Comparable TreeSet
//
TreeSet wSet = new TreeSet<>();
wSet.add(new Worker("ygs", 21));
wSet.add(new Worker("ygh", 21));
wSet.add(new Worker("ygssb", 21));
wSet.add(new Worker("xsd", 22));
wSet.add(new Worker("hzn", 19));
wSet.add(new Worker("wl", 24));
for (Worker worker : wSet) {
System.out.println(worker);
}
}
Wroker :
public class Worker extends Person implements Comparable<Worker>{
public Worker() {
}
public Worker(String name, int age) {
super(name, age);
}
// Compareable compareTo
@Override
public int compareTo(Worker o) {
return 0;
}
// 0 set
//1( )
//-1( )
//TreeSet
// ( )
// ( )
//
//
// TreeSet
//1. TreeSet Comparable
//2. compareTo
//3. compareTo
}
return 0
this.getAge() - o.getAge() > 0
this.getAge() - o.getAge() < 0 ,
this.getAge() - o.getAge() == 0
//
// TreeSet 4
// Comparable TreeSet
//
TreeSet wSet = new TreeSet<>();
wSet.add(new Worker("ygs", 21));
wSet.add(new Worker("ygh", 21));
wSet.add(new Worker("ygssb", 21));
wSet.add(new Worker("xsd", 22));
wSet.add(new Worker("hzn", 19));
wSet.add(new Worker("wl", 24));
for (Worker worker : wSet) {
System.out.println(worker);
}
}
Wroker :
public class Worker extends Person implements Comparable<Worker>{
public Worker() {
}
public Worker(String name, int age) {
super(name, age);
}
//
@Override
public int compareTo(Workder o){
//
return this.getAge() - o.getAge();
}
}
//
@Override
public int compareTo(Workder o){
return this.getNage().compareTo(o.getName());
}
//
//
@Override
public int compareTo(Worker o){
if(this.getAge() == o.getAge()){
reutrn this.getNage().compareTo(o.getName())
}
return this.getAge() - i.getAge();
}
//
//
//
@Override
public int compareTo(Worker o){
int length = this.getName().length();
int length2 = o.getName().length();
if(this.getAge() > o.getAge()){
return this.getAge() - o.getAge();
}else if(this.getAge() == o.getAge()){
return this.getName.compareTo(o.getName());
}
return length - length - 2;
}
비교 기 Comparator
//
//1. Comparator
//2.
//3. TreeSet
public static void fun3() {
//
//
TreeSet set = new TreeSet<>(new StringLegnthCompareImpl());
set.add("asd");
set.add("asde");
set.add("asdef");
set.add("a");
set.add("as");
System.out.println(set);
}
//
// Comparator
class StringLegnthCompareImpl implements Comparator<String>{
//
//
//
@Override
public int compare(String o1, String o2) {
if (o1.length() == o2.length()) {
return o1.compareTo(o2);
}
return o1.length() - o2.length();
}
}
public static void fun4() {
//
//
TreeSet set = new TreeSet<>(new RepeatStringImpl());
set.add("asd");
set.add("asde");
set.add("asdef");
set.add("a");
set.add("asd");
System.out.println(set);
}
//
class RepeatStringImpl implements Comparator<String>{
@Override
public int compare(String o1,String o2){
int length = o1.length() - o2.length();
//
//
// num
// length == 0 o1.compareTo(o2)
// length != 0 length
int num = length == 0 ? o1.compareTo(o2) : length;
// num == 0 1
// num != 0 num
return num == 0 ? 1 : num;
}
}
// ,
//
public static void fun5() {
System.out.println(" ");
Scanner scanner = new Scanner(System.in);
String string = scanner.nextLine();
char[] charArray = string.toCharArray();
TreeSet set = new TreeSet<>(new CompareStringAndRepeatTheSameString());
for (int i = 0; i < charArray.length; i++) {
set.add(charArray[i]);
}
System.out.println(set);
}
class CompareStringAndRepeatTheSameString implements Comparator<Character>{
@Override
public int compare(Character o1, Character o2) {
int num = o1.compareTo(o2);
return num == 0 ? 1 : num;
}
}
public static void fun6() {
/*
* , ,
* quit . .
*
*/
Scanner scanner = new Scanner(System.in);
TreeSet set = new TreeSet<>(new KeepIntImpl());
while (true) {
System.out.println(" quit ");
String string = scanner.nextLine();
if (string.equals("quit")) {
break;
}
//
int num = Integer.parseInt(string);
//
set.add(num);
}
System.out.println(set);
}
class KeepIntImpl implements Comparator{
@Override
public int compare(Integer o1, Integer o2) {
int num = o1 - o2;
return -(num == 0 ? 1 : num);
}
}
// 5 ( , , , )
// ( , , , )
// 。
// Student
public class Student implements Comparable{
private String name;
private int ChineseScore;
private int MathScore;
private int EnglishScore;
private int sum;
//
public Student() {
}
public Student(String name, int chineseScore, int mathScore, int englishScore) {
this.name = name;
ChineseScore = chineseScore;
MathScore = mathScore;
EnglishScore = englishScore;
this.sum = chineseScore + mathScore + englishScore;
}
//set/get
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getChineseScore() {
return ChineseScore;
}
public void setChineseScore(int chineseScore) {
ChineseScore = chineseScore;
}
public int getMathScore() {
return MathScore;
}
public void setMathScore(int mathScore) {
MathScore = mathScore;
}
public int getEnglishScore() {
return EnglishScore;
}
public void setEnglishScore(int englishScore) {
EnglishScore = englishScore;
}
public int getScore() {
return sum;
}
//
// public void setSum(int sum) {
// this.sum = sum;
// }
// toString
@Override
public String toString() {
return "Student [name=" + name + ", ChineseScore=" + ChineseScore + ", MathScore=" + MathScore
+ ", EnglishScore=" + EnglishScore + ", Score=" + sum + "]";
}
//
@Override
public int compareTo(Student o) {
int num = this.sum - o.sum;
return num == 0 ? 1 : num;
}
}
public class Demo {
public static void main(String[] args) {
//
TreeSet set = new TreeSet<>();
Scanner scanner = new Scanner(System.in);
while(set.size() < 5) {
System.out.println(" +( , , , )");
// , , ,
String string = scanner.nextLine();
//
String[] strings = string.split(",");
//
int chineseScore = Integer.parseInt(strings[1]);
int mathScore = Integer.parseInt(strings[2]);
int englishScore = Integer.parseInt(strings[3]);
//
Student student = new Student(strings[0], chineseScore, mathScore,
englishScore);
//
set.add(student);
}
//
System.out.println(set);
}
Day.19
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
JPA + QueryDSL 계층형 댓글, 대댓글 구현(2)이번엔 전편에 이어서 계층형 댓글, 대댓글을 다시 리팩토링해볼 예정이다. 이전 게시글에서는 계층형 댓글, 대댓글을 구현은 되었지만 N+1 문제가 있었다. 이번에는 그 N+1 문제를 해결해 볼 것이다. 위의 로직은 이...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.