Предположим, что длина firstList равна n length Длина secondList равна m , ваши затраты могут быть O(nlogn) + O(mlogm) +O(n+m)
для решения проблемы. Алгоритм типа сортировки слиянием.
public static List<Fruit> merge(List<Fruit> list1, List<Fruit> list2) {
// sort list1 and list2
Comparator<Fruit> comparator = Comparator.comparingInt(o -> Integer.parseInt(o.getId()));
Collections.sort(list1, comparator);
Collections.sort(list2, comparator);
List<Fruit> finalList1 = new ArrayList<>();
List<Fruit> finalList2 = new ArrayList<>();
int length1 = list1.size();
int length2 = list2.size();
int index1 = 0;
int index2 = 0;
while (index1 < length1 && index2 < length2) {
Fruit fruit1 = list1.get(index1);
Fruit fruit2 = list2.get(index2);
if (fruit1.getId().equals(fruit2.getId()) && fruit2.isChecked()) {
finalList2.add(fruit2);
index2++;
index1++;
} else {
finalList1.add(fruit1);
index1++;
}
}
while (index1 < length1) {
finalList1.add(list1.get(index1));
index1++;
}
finalList2.addAll(finalList1);
return finalList2;
}
Для тех, которые не знают: точка соединения ведет себя так же к символьной ссылке для папки на Linux. Прерывание, которое упоминается, происходит при установке рекурсивной структуры папок, как это:
given folder /a/b
let /a/b/c point to /a
then
/a/b/c/b/c/b becomes valid folder locations.
Я предлагаю стратегию как этот. На окнах Вы ограничены максимальной длиной на строке пути, таким образом, рекурсивное решение, вероятно, не унесет стек.
private void FindFilesRec(
string newRootFolder,
Predicate<FileInfo> fileMustBeProcessedP,
Action<FileInfo> processFile)
{
var rootDir = new DirectoryInfo(newRootFolder);
foreach (var file in from f in rootDir.GetFiles()
where fileMustBeProcessedP(f)
select f)
{
processFile(file);
}
foreach (var dir in from d in rootDir.GetDirectories()
where (d.Attributes & FileAttributes.ReparsePoint) != FileAttributes.ReparsePoint
select d)
{
FindFilesRec(
dir.FullName,
fileMustBeProcessedP,
processFile);
}
}
можно использовать следующий код:
private void processing(string directory)
{
cmbFilesTypesSelectedIndex = cmbFilesTypes.SelectedIndex;
CheckForProjectFile(directory);
DirectoryInfo dInfo = new DirectoryInfo(directory);
DirectoryInfo[] dirs = dInfo.GetDirectories() ;
foreach (DirectoryInfo subDir in dirs)
{
CheckForProjectFile(subDir.FullName);
processing(subDir.FullName);
}
}
private void CheckForProjectFile(string directory)
{
Boolean flag = false;
DirectoryInfo dirInfo = new DirectoryInfo(directory);
FileInfo[] files = dirInfo.GetFiles();
//You can also traverse in files also
foreach (FileInfo subfile in files)
{
//Do you want
}
}