ืื ืืืืืจ ื-Java
ื ืืืืจ ืืงืฆืจื ืฉืืืฉืง ืืกื ืืืืืจ ืงืืืฆื (ืกื). ืืื ืืจืืื ืืช ืืืืกืฃ ืืืืืืจ ืืช ืืืชื ืืืืช ืฉื ืืืกืคืื ืฉืืื ื ืืืคืฉืจืื ืจืืืืื ืืคืืืื. ืืคืืื, ืืืชืืื add() ืืืืืจื false ืื ื ืขืฉื ื ืืกืืื ืืืืกืืฃ ืจืืื ืืคืื ืืกื. ืืืืฉืง ืืื ื ืืืืืจ ืฉืืืืช ื ืืกืคืืช ืืฉืื. ืืืฉืง ื- Set ืืืื ืืืืืืืืืช ืฉื ืืืืืืงืืื ืืืืืกื ืื, ืืืืืืืืืช ื ืงืืขืช ืขื ืืื ืืืฉืื ืฉืืืช equals() . ืืื, ืื ืืืืืืงืืื ืฉื ืืืืืงื ืฉื ืืฆืจื ืืชืืืกืคื ื- Set , ืจืฆืื ืืขืงืืฃ ืืช ืืืชืืื equals() .ืืืืงื LinkedHashSet
ืืคื ื ืฉื ืืืจ ืขื ืืืืงืช LinkedHashSet , ืขืืื ื ืืืืืืจ ืืช ืงืจืื ืืฉืคืืชื, ืืืืงืช HashSet . HashSet ืืืืฉืืช ืืช ืืืฉืง ื-Set . ืื ืืืฆืจ ืืืกืฃ ืืืืืกื ืืช ืืืืื ืืื ืืืืืช hash. ืืืืื ืืื ืฉื ืืืืช hash ืืืืืกื ืื ืืืืืืช ืืคืชื-ืขืจื. ืืืคืชื ืืฆืืื ืืช ืืชื (ืื ืืืงืืข) ืืืืกืื ืืขืจื. ืืชืืื ืฉื ืืืคืชื ืืฉืืฉ ืืงืืืขืช ืขืจื ืืืืืื ืื ืงืจื ืงืื hash. ืื ื ืืืืืื ืืืฉืื ืขื ืงืื hash ืืืืื ืืืืืืงื, ืืืจืืช ืฉืืื ืื ืืืื ืืืืืช ืืืืืื. ืงืื ืืฉืืฉ ืื ืืฉืืฉ ืื ืืืื ืืงืก ืฉืืืื ืืืืืกื ืื ืื ืชืื ืื ืืงืฉืืจืื ืืืคืชื. ืืืืงืช LinkedHashSet Java ืืจืืืื ืืช HashSet ืืืื ืืืืกืืฃ ืืชืืืืช ืืืฉืืช. LinkedHashSet ืืืคืฉืจ ืื ืืืืืง ืืืืืจืืช ืืช ืงืืืื ืฉื ืขืจื, ืืืืืง ืืื HashSet , ืื ืืืื ืืชืืื ืจืฉืืื ืืกืืืจืช. ืื ืืืืจ ืฉืืื ืืืืกื ืืช ืกืืจ ืืืื ืกื ืฉื ืืืืื ืืื. ืืืืืื ืืืจืืช, LinkedHashSet ืฉืืืจ ืขื ืจืฉืืื ืืงืืฉืจืช ืฉื ืจืืืื ืกื ืืกืืจ ืฉืื ืืืื ืกื. ืื ืืืคืฉืจ ืืืืจืฆืื ืืกืืืจืช ืฉื ืืื ืกื ืืชืื ืงืืืฆื. ืืื ืื ืืืจื ืืืืืงื LinkedHashSet ืืืฆืข ืคืขืืืืช ืืจืืืืช ืืืชืจ ืืืืืงืช HashSet .ืชืืื ืืช ืืฉืืืืช ืฉื LinkedHashSet
-
ืื ื ืืืืืื ืืืืกื ืืืื ืืื ืืืืืืืื ืจืง ื- LinkedHashSet
-
LinketHashSet ืืืคืฉืจ ืื ื ืืืืฅ ืืืื ืืื ืืืืชื ืกืืจ ืฉืื ื ืืื ืืกืื
-
LinkedHashSet ืืื ื ืืกืื ืืจื
-
LinkedHashSet ืืืคืฉืจ ืืืกืื ืืืื ืืื null
-
LinkedHashSet ืืฉืชืืฉ ืืืื ืืงืช ืืืืื ืืื ืืืืกื ืืืื ืืื ืืืื ืืงืก ืืืืืจ ืืืชืืกืก ืขื ืงืื ืืืืื
ืฉืืืืช LinkedHashSet
ืื ืืกืฃ ืืฉืืืืช ืฉืขืืจื ืืืจืืฉื ืืืืืงืืช ืืื ืฉืื, HashSet ืืืืืจ ืืช ืืฉืืืืช ืืืืืช:-
boolean add(Object o) ืืืกืืฃ ืืช ืืืืื ื ืฉืฆืืื ืืงืืืฆื ืื ืื ืืื ืขืืืื ืื ืงืืื.
-
void clear() ืืกืืจ ืืช ืื ืืจืืืืื ืืืงืืืฆื ืืื.
-
Object clone() ืืืืืจ ืขืืชืง ืจืืื ืฉื ืืืคืข LinkedHashSet ืื : ืืืืื ืืื ืขืฆืื ืืื ื ืืฉืืืืื.
-
boolean contains(Object o) ืืืืืจื true ืื ืงืืืฆื ืื ืืืืื ืืช ืืืืื ื ืฉืฆืืื.
-
boolean isEmpty() ืืืืืจื true ืื ืงืืืฆื ืื ืืื ื ืืืืื ืืืื ืืื.
-
Iterator iterator() ืืืืืจ ืืืืจืืืจ ืืขื ืืจืืืืื ืฉื ืงืืืฆื ืื.
-
boolean remove(Object o) ืืกืืจ ืืช ืืืืื ื ืฉืฆืืื ืืงืืืฆื ืื, ืื ืงืืื.
-
int size() ืืืืืจื ืืช ืืกืคืจ ืืืืื ืืื ืืงืืืฆื ืื (ืืกืคืจ ืืืืื ืืื ืฉืื).
import java.util.LinkedHashSet;
import java.util.Set;
public class LinkedHashSetEx1 {
public static void main(String[] args) {
//LinkedHashSet() Init
Set<String> set = new LinkedHashSet<>();
//adding elements to LinkedHashSet
set.add("Re"); //first added element
set.add("Do");
set.add("Fa");
set.add("Sol");
set.add("La");
set.add("Ti");
set.add("Mi");//last added element
System.out.println(set);
}
}
ืืคืื ืืื:
ืืืืื 2. ืืืกืคืช ืืคืื ืืชืื LinkedHashSet
ืืืื ื ืื ืืก ืฉืื ื- LinkedHashSet ืฉืื ื 7 ืืืื ืืื ืขื ืฉืืืช ืฉื ืชืืืื ืืืืืงืืืื ืื ืฉืื ืืืื ื ืืื ืืืฉ ืืื ืืืื ืืืืืื ืืื ืฉืืืฆืื ืงืืื ืืื.
import java.util.LinkedHashSet;
import java.util.Set;
public class LinkedHashSetEx2 {
public static void main(String[] args) {
Set<String> set = new LinkedHashSet<>();
set.add("Re");
set.add("Do");
set.add("Fa");
set.add("Sol");
set.add("La");
set.add("Ti");
set.add("Mi");
set.add("Sol");
System.out.println(set);
}
}
ืืคืื ืฉื ืืชืืื ืืช ื ืืฆื ืืื:
ืืืืื 3. ืืกืจืช ืืืื ืืื ื-LinkedHashSet
import java.util.LinkedHashSet;
import java.util.Set;
public class LinkedHashSet3 {
public static void main(String[] args) {
Set<String> set = new LinkedHashSet<>();
set.add("Re");
set.add("Do");
set.add("Fa");
set.add("Sol");
set.add("La");
set.add("Ti");
set.add("Mi");
System.out.println(set);
set.remove("Fa");// removing an element from our set
set.remove("Score");//trying to remove element that isn't in set
System.out.println(set.remove("Score"));
System.out.println("Print our set without elements removed: ");
System.out.println(set);
set.clear();
System.out.println("Print out our set after clear command: ");
System.out.println(set);
}
}
ืืคืื ืฉื ืืชืืื ืืช ื ืืฆื ืืื:
LinkedHashSet ืืขืืืช HashSet
ืฉื ื ืืืขืืืืช ืืืื ืื ืงืจืืื ืืฉืคืื. ืืืื ืืชืื ื- HashSet ืืื ืืฉืชืืฉ ื-HashMap ืืื ืืืืกื ืืืืืืงืืื ืืขืื LinkedHashSet ืืฉืชืืฉ ื- LinkedHashMap . ืื ืืื ื ืฆืจืื ืืฉืืืจ ืขื ืกืืจ ืืืื ืกื ืืื ืฆืจืื ืืืืกื ืืืืืืงืืื ืืืืืืืื, ืขืืืฃ ืืืฉืชืืฉ ื-HashSet . ืื ืืชื ืฆืจืื ืืฉืืืจ ืขื ืกืืจ ืืืื ืกื ืฉื ืืืื ืืื ืื LinkedHashSet ืืื ืืืืืจื ืฉืื. ืืืืฆืืขืื ืฉื LinkedHashSet ืืขื ืืืืืื ืืืชืจ ื- HashSet ืืืืืื ืฉ- LinkedHashSet ืืฉืชืืฉ ื-LinkedList ืคื ืืื ืืื ืืฉืืืจ ืขื ืกืืจ ืืืื ืกื ืฉื ืืืื ืืื. ืืื ื ืืื ืืืืื:
import java.util.*;
public class LinkedHashSetExample1 {
public static void main(String[] args) {
// while regular hash set orders its elements according to its hashcode stamps
Set<Integer> regularHashSet = new HashSet<>();
regularHashSet.add(7);
regularHashSet.add(3);
regularHashSet.add(5);
regularHashSet.add(65536);
regularHashSet.add(9);
// few duplicates
regularHashSet.add(5);
regularHashSet.add(7);
// next will print:
// > regularHashSet = [65536, 3, 5, 7, 9]
System.out.println("regularHashSet = " + regularHashSet);
// linked hash set keeps order of adding unchanged
Set<Integer> linkedHashSet = new LinkedHashSet<>();
linkedHashSet.add(7);
linkedHashSet.add(3);
linkedHashSet.add(5);
linkedHashSet.add(65536);
linkedHashSet.add(9);
// few duplicates
linkedHashSet.add(5);
linkedHashSet.add(7);
// next will print:
// > linkedHashSet = [7, 3, 5, 65536, 9]
System.out.println("linkedHashSet = " + linkedHashSet);
}
}
ืืคืื ืฉื ืืชืืื ืืช ืืื:
ืฉืืืืฉ ื-Java LinkedHashSet ืืืืฉืืืื ืืขืืื ืืืืืชื
ืืืืืื ืฉ- LinkedHashSet ืืืคืฉืจ ืื ืืืืืง ืืืืืจืืช ืืช ืงืืืื ืฉื ืขืจื ืืื ืืืืกื ืืืื ื, ื ืจืื ืฉืืืืกืฃ ืืื ืื ื ืื ืืืขืืืช ืืคืืืืืืช ืืจืฉืืื. ืื, ืืืฉื, ืคืชืจืื ืืขืืืช ืืื ืืคืจืื ืืืืจืื ืฉื ืจืื ืืชืืง ืฉืื. ืื, ืืืืจืื ืืฉืืง ืืื, ืคืืงืืืื ืื? LinkedHashSet ืืืื ืืืืกื ืจืฉืืื ืฉื ืคืืงืืืื ืื ืฉื ืชืงืืชื ืืื ืืืช ืืกืืจ ืฉืื ืื ื ืชืงืื ืืืจืืื. ืืืงืจื ืื, ืืคืืงืืืื ื"ืืืืจ" ืื ืืชืืืกืฃ ืขืื ืืจืฉืืื. ืื, ืืืฉื, ืจืฉืืื ืฉื ืืืกืื ืืคื ืจืืืช ืฉืืืจ ืคืืฉืชื ืืื ืืฉืืง ืขื ืจืืืช. ืื ืืืืกืืืจืื ืฉื ืืืืื ืืืคืื ืงืืกืืืื. LinkedHashSet ืืืคืฉืจ ืื ืืืืืง ืืืืืจืืช ืื ืืืฃ ืฉืื ืืืจ ืืจืฉืืื ืื ืื, ืืื ืืื ืื ืฉื, ืื ืืืืกืืฃ ืืืชื ืืจืฉืืื. ื ืืงื ืืืืื ืฉื ืืืืื ืืคืืืืืืช.
import java.util.*;
class LinkedHashSetExample2 {
public static void main(String[] args) {
List<String> listWithDuplicates = List.of("some","elements","with", "few", "duplicates", "were", "here", "duplicates", "duplicates");
Set<String> linkedHashSet = new LinkedHashSet<>(listWithDuplicates);
List<String> listWithoutDuplicates = new ArrayList<>(linkedHashSet);
// next will print:
// > listWithDuplicates = [some, elements, with, few, duplicates, here, duplicates, duplicates]
System.out.println("listWithDuplicates = " + listWithDuplicates);
// next will print:
// > listWithoutDuplicates = [some, elements, with, few, duplicates, here]
System.out.println("listWithoutDuplicates = " + listWithoutDuplicates);
// -------------------------------------------------------------------------
// while using regular Hash Set will generally produces some unexpected order
Set<String> regularHashSet = new HashSet<>(listWithDuplicates);
// next will print:
// > linkedHashSet = [some, elements, with, few, duplicates, were, here]
System.out.println("linkedHashSet = " + linkedHashSet);
// next will print:
// > regularHashSet = [here, some, with, duplicates, were, elements, few]
System.out.println("regularHashSet = " + regularHashSet);
}
}
ืืคืื ืฉื ืืชืืื ืืช ื ืืฆื ืืื:
GO TO FULL VERSION