我一直在我的代码中使用库存JDK集合.Apache Commons Collections框架运行得更快吗?
我正在尝试编写一个简单的程序来使用堆栈。它给了我错误
Stack 类型不是通用的;它不能用参数参数化
import java.util.*;
public class Stack {
public static void main(String[] args)
{
Stack<Character> stack = new Stack<> ();
s.push("Hello");
System.out.println(s);
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试通过以下方式对字符串列表(将包含字母数字字符以及标点符号)进行排序Collections.sort
:
public class SorterDriver {
public static void main(String[] args) {
List<String> toSort = new ArrayList<String>();
toSort.add("fizzbuzz");
System.out.println("toSort size is " + toSort.size());
List<String> sorted = Collections.sort(toSort);
if(sorted == null) {
System.out.println("I am null and sad.");
} else {
System.out.println("I am not null.");
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行这个时,我得到:
toSort size is 1
I am null and sad.
Run Code Online (Sandbox Code Playgroud)
为什么为空?
大家都知道,Iterator
S按返回ArrayList
,HashMap
,HashSet
等都是快速失败的,但在使用迭代器remove()
不会抛出ConcurrentModificationException
.怎么样?
ConcurrentModificationException
如果在迭代时修改了集合,则抛出fail-fast迭代器.但是从迭代器中删除元素ArrayList
或HashSet
使用迭代器时remove()
,不会抛出任何元素ConcurrentModificationException
.请详细解释.
谢谢
我有两个vavr列表:
List<Object> list1 = List.of("one", "two", "three", "for");
List<Object> list2 = List.of("one", "two", List.of("three", "for"));
Run Code Online (Sandbox Code Playgroud)
我怎样才能将其变换list2
为等于list1
?
编辑
我试着解释一下我想要达到的目标:
System.out.println("list1: " + list1);
System.out.println("list2: " + list2);
Run Code Online (Sandbox Code Playgroud)
输出:
list1: List(one, two, three, for)
list2: List(one, two, List(three, for))
Run Code Online (Sandbox Code Playgroud)
我想要展平所有内部列表list2
,因此展平列表应该等于list1
:
System.out.println("flattened: " + flattened);
System.out.println(list1.equals(flattened));
Run Code Online (Sandbox Code Playgroud)
应该返回:
flattened: List(one, two, three, for)
true
Run Code Online (Sandbox Code Playgroud) 所以我想知道什么是最好的方法
对于这样的用例,列表可能不是最佳的数据结构?
我试图在 Kotlin 中创建List<Char>
,String
但似乎 lib 没有提供内置函数。铸造也会产生错误。所以这就是我正在做的。如果我遗漏了有问题的内容,请告诉我。(或者我们可以说List<Char>
在 Kotlin中将字符串转换为)。
var stringVal = "ABC"
var genList:List<Char> = arrayListof()
var count = 0
while (stringVal.length == genList.size) {
// way to add stringVal to genList
count++
}
Run Code Online (Sandbox Code Playgroud) 这段代码:
Deque<String> list = new LinkedList<>();
list.push("first");
list.push("second");
list.push("third");
System.out.println(list.remove());
Run Code Online (Sandbox Code Playgroud)
相当于:
Deque<String> list = new LinkedList<>();
list.push("first");
list.push("second");
list.push("third");
System.out.println(list.pop());
Run Code Online (Sandbox Code Playgroud)
pop() 和remove() 都删除第一个元素(head)。那么,采用两种不同方法的原因是什么?
根据我的理解,如果一个线程已经在访问它,Vector 应该阻止其他线程的整个集合。我在尝试着
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.CopyOnWriteArrayList;
public class MyTest1{
// static ArrayList<String> l1 = new ArrayList<>(Arrays.asList("A", "B", "C"));
static Vector<String> l1 = new Vector<>(Arrays.asList("A", "B", "C"));
// static List<String> l1 = Collections.synchronizedList(Arrays.asList("A", "B", "C"));
public static void main(String[] args) {
Thread t1 = new Thread(new MyThread1());
Thread t2 = new Thread(new MyThread2());
t1.start();
t2.start();
System.out.println(Thread.currentThread() + "adding X " + l1.add("X"));
System.out.println(Thread.currentThread() + "adding Y " + l1.add("Y"));
System.out.println(Thread.currentThread() + …
Run Code Online (Sandbox Code Playgroud) 我正在努力解决这个问题。这4个有什么区别?
1. private ObservableCollection<T> _myObservableCollestionOfTs
2. private readonly ObservableCollection<T> _myObservableCollestionOfTs
3. private ReadOnlyObservableCollection<T> _myObservableCollestionOfTs
4. private readonly ReadOnlyObservableCollection<T> _myObservableCollestionOfTs
Run Code Online (Sandbox Code Playgroud)
感谢您的任何帮助 :)
collections ×10
java ×8
c# ×1
concurrency ×1
deque ×1
generics ×1
kotlin ×1
linked-list ×1
list ×1
logic ×1
performance ×1
queue ×1
readonly ×1
sorting ×1
stack ×1
vavr ×1