Mã này là khó có thể đọc và không phải là rất hiệu quả. Tham số "dest" gây nhầm lẫn: Nó được truyền vào như một tham số, sau đó nó được xóa và kết quả được thêm vào nó. Điểm của nó là một tham số? Tại sao không chỉ đơn giản là trả lại một bộ sưu tập mới? Lợi ích duy nhất tôi có thể thấy là người gọi có thể xác định loại bộ sưu tập. Điều đó có cần thiết không?
Tôi nghĩ rằng mã này có thể rõ ràng hơn và có lẽ được viết một cách hiệu quả hơn như sau:
public static Set<String> createSet(Collection<String> source) {
Set<String> destination = new HashSet<String>(source) {
private static final long serialVersionUID = 1L;
public boolean add(String o) {
if ("".equals(o)) {
return false;
}
return super.add(o);
}
};
return destination;
}
Một cách khác là tạo ra loại thiết lập riêng của bạn:
public class NonEmptyStringSet extends HashSet<String> {
private static final long serialVersionUID = 1L;
public NonEmptyStringSet() {
super();
}
public NonEmptyStringSet(Collection<String> source) {
super(source);
}
public boolean add(String o) {
if ("".equals(o)) {
return false;
}
return super.add(o);
}
}
Cách sử dụng:
createSet(source);
new NonEmptyStringSet(source);
Trả lại tập hợp có hiệu suất cao hơn vì trước tiên bạn không phải tạo tập hợp tạm thời và sau đó quảng cáo d tất cả để thu thập số phận.
Lợi ích của loại NonEmptyStringSet là bạn có thể tiếp tục thêm chuỗi và vẫn có kiểm tra chuỗi rỗng.
EDIT1:
Loại bỏ các "if (src.containsAll (dest)) return;" mã giới thiệu một "lỗi" khi gọi phương thức với nguồn == dest; Kết quả là nguồn sẽ trống Ví dụ:.
Collection<String> source = new ArrayList<String>();
source.add("abc");
copyStringCollectionAndRemoveDuplicates(source, source);
System.out.println(source);
EDIT2:
Tôi đã làm một điểm chuẩn nhỏ cho thấy rằng việc triển khai của tôi nhanh hơn khoảng 30%, sau đó là một phiên bản đơn giản của việc triển khai ban đầu của bạn. Ngoài ra, việc triển khai của tôi không sử dụng HashSet thay vì LinkedHashSet, điều này làm cho việc triển khai của tôi nhanh hơn một chút,
đang 63.210
Benchmark:
public class SimpleBenchmark {
public static void main(String[] args) {
Collection<String> source = Arrays.asList("abc", "def", "", "def", "",
"jsfldsjdlf", "jlkdsf", "dsfjljka", "sdfa", "abc", "dsljkf", "dsjfl",
"js52fldsjdlf", "jladsf", "dsfjdfgljka", "sdf123a", "adfgbc", "dslj452kf", "dsjfafl",
"js21ldsjdlf", "jlkdsvbxf", "dsfjljk342a", "sdfdsa", "abxc", "dsljkfsf", "dsjflasd4");
int runCount = 1000000;
long start1 = System.currentTimeMillis();
for (int i = 0; i < runCount; i++) {
copyStringCollectionAndRemoveDuplicates(source, new ArrayList<String>());
}
long time1 = (System.currentTimeMillis() - start1);
System.out.println("Time 1: " + time1);
long start2 = System.currentTimeMillis();
for (int i = 0; i < runCount; i++) {
new NonEmptyStringSet(source);
}
long time2 = (System.currentTimeMillis() - start2);
System.out.println("Time 2: " + time2);
long difference = time1 - time2;
double percentage = (double)time2/(double) time1;
System.out.println("Difference: " + difference + " percentage: " + percentage);
}
public static class NonEmptyStringSet extends HashSet<String> {
private static final long serialVersionUID = 1L;
public NonEmptyStringSet() {
}
public NonEmptyStringSet(Collection<String> source) {
super(source);
}
@Override
public boolean add(String o) {
if ("".equals(o)) {
return false;
}
return super.add(o);
}
}
public static void copyStringCollectionAndRemoveDuplicates(
Collection<String> src, Collection<String> dest) {
Set<String> uniqueSet = new LinkedHashSet<String>(src.size());
for (String f : src)
if (!"".equals(f))
uniqueSet.add(f);
dest.addAll(uniqueSet);
}
}
Chỉ là một nhận xét nhỏ, không liên quan đến câu hỏi của bạn: mục tiêu và có ý nghĩa tương tự. Vì bạn đang sao chép chuỗi không trống từ đích đến đích, có lẽ nó có thể được đổi tên thành src? –