我知道所有这些之间的区别,我理解LinkedHashMap并LinkedHashSet提供插入排序.我理解LinkedHashMap extends HashMap和LinkedHashSet extends HashSet.
为什么我们不总是使用LinkedHashMap而不是HashMap为什么我们不总是使用LinkedHashSet而不是HashSet?
我在 aLinkedHashSet中有一个实现equals, hashCodeand compareTo(在超类中)的对象,但是当我尝试从集合中删除那个确切的对象时,set.remove(obj)remove 方法返回false并且该对象保留在集合中。的实现是否LinkedHashSet应该调用equals()其对象的方法?因为它没有。这可能是一个java错误吗?我正在运行 1.6.0_25。
我有以下示例代码.应用程序第一次安装成功.但是,它会在重新安装时抛出错误.
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinkedHashSet<String> planets = new LinkedHashSet<String>();
planets.add("Earth");
SharedPreferences prefs = getPreferences(0);
prefs.edit().putStringSet("planets", planets).commit();
prefs = getPreferences(0);
planets = (LinkedHashSet<String>) prefs.getStringSet("planets", new LinkedHashSet<String>());
}
}
Run Code Online (Sandbox Code Playgroud)
我已经粘贴了重新安装以下应用程序时产生的错误.
Caused by: java.lang.ClassCastException: java.util.HashSet cannot be cast to java.util.LinkedHashSet at com.example.test.MainActivity.onCreate(MainActivity.java:12)
Run Code Online (Sandbox Code Playgroud)
我想了解为什么保存LinkedHashSet不能被归还给LinkedHashSet.为什么它会HashSet被Android自动转换为?
我想知道从链接哈希集中删除元素的不同方法.我试过以下代码
LinkedHashSet<String> lhs = new LinkedHashSet<String>();
for(int i=0;i<10;i++)
lhs.add(String.valueOf(i));
Iterator<String> it=lhs.iterator();
System.out.println("removed?=="+lhs.remove("1"));
while(it.hasNext())
{
System.out.println("lhs"+it.next());
}
Run Code Online (Sandbox Code Playgroud)
我得到了以下输出
removed?==true
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.LinkedHashMap$LinkedHashIterator.nextEntry(Unknown Source)
at java.util.LinkedHashMap$KeyIterator.next(Unknown Source)
at preac.chapter1.Start.main(Start.java:321)
Run Code Online (Sandbox Code Playgroud)
我想念的是什么?提前致谢.
PS我也试过iterator.remove()方法,但得到了非法状态异常
编辑
我刚才知道我必须使用iterator remove方法.然后使用Link Hash Set删除方法是什么?在哪些情况下我们应该使用这种方法?
我试图从使用Set Interface的String类型的List对象中删除重复项,但我遇到的问题是它也重新排序我的列表,这是我不想要的.我想保留列表的顺序并仅删除重复项?
static List<String> StopNames = new ArrayList<String>();
StopNames.add(sCurrentLine);
Set<String> set = new HashSet<String>(StopNames);
for (String string : set) {
System.out.println("Printing Set "+string);
}
Run Code Online (Sandbox Code Playgroud) 我有一个listView,最初有一些内容.如果获得相同的内容,我删除了重复linkedhashset.现在,我想复制linkedhashset内容,即没有重复内容到新ArrayList.
我试图通过复制
p.addAll(0,lhm); // P is the instance of ArrayList and lhm is linkedHashset instance
Run Code Online (Sandbox Code Playgroud)
但是,也ArrayList包括复制内容.
示例:
ArrayList<Price> p = new ArrayList<Price>();
p.add(new Price("Banana", 60));
p.add(new Price("Apple", 80));
LinkedHashSet<Price> lhm = new LinkedHashSet<Price>(p);
lhm.add(new Price("Banana", 20));
lhm.add(new Price("Apple", 40));
lhm.add(new Price("Orange", 30));
for(Price pr:lhm)
{
System.out.println(pr);
}
Price duplicate = new Price("Banana", 20);
System.out.println("inserting duplicate object...");
lhm.add(duplicate);
lhm.add(new Price("Apple", 40));
p.addAll(0,lhm);
System.out.println("After insertion:");
for(Price pr:lhm)
{
System.out.println(pr);
}
for (int i = …Run Code Online (Sandbox Code Playgroud) 我已经阅读了java文档以及这篇文章中的LinkedHashMaps keyset()维护订单.
是否保证从LinkedHashMap对象返回键和值的顺序?
我的问题是,如果它保证顺序那么为什么不LinkedHashMap返回一个类型的对象Set保证顺序的源代码LinkedHashSet?
我可以想到的一个原因是LinkedHashSet使用了一个可以增加内存分配的映射(取决于AbstractSet的实现方式).是不是因为它的未来证明了keyset的实现?
就像这个帖子在这篇文章中所说:使用List或Collection更好吗?
返回列表符合最高适当接口的编程.
返回集合会导致用户不明确,因为返回的集合可能是:Set,List或Queue.
那么,没有阅读文档的keyset()不是这个含糊不清的?
keyset() 源代码:
public Set<K> keySet() {
Set<K> ks = keySet;
return (ks != null ? ks : (keySet = new KeySet()));
}
private final class KeySet extends AbstractSet<K> {
public Iterator<K> iterator() {
return newKeyIterator();
}
public int size() {
return size;
}
public boolean contains(Object o) {
return containsKey(o);
}
public boolean remove(Object o) {
return HashMap.this.removeEntryForKey(o) != null;
} …Run Code Online (Sandbox Code Playgroud) 我有一个包含 javascript 中的 ID 的列表列表,我希望将它们添加到一个维护插入顺序且只有唯一值的集合中
考虑以下代码:
final Set<String> allPaths = new HashSet<String>();
for (final String path: paths) {
allPaths.add(path);
}
final MyData d = new MyData(new ArrayList<String>(allPaths));
Run Code Online (Sandbox Code Playgroud)
MyData是一些我不应该碰的类。它应该得到一个ArrayList作为参数。在这一天之前,我们是这样使用的,因为我们不关心顺序,这就是我们使用的原因Set(所以不会有重复)。但是现在,我想保持元素的顺序,所以经过一些研究,我发现我可以使用数据结构LinkedHashSet 来做到这一点。所以我做了:
final LinkedHashSet<String> allPaths = new LinkedHashSet<String>();
for (final String path: paths) {
allPaths .add(path);
}
final MyData d = new MyData(new ArrayList<String>(allPaths));
Run Code Online (Sandbox Code Playgroud)
问题是,我不确定如何转换LinkedHashSet为ArrayList. 另外,我想使用ArrayList而不是LinkedHashSet这样我就不必转换它,但我必须遍历数组 ( O(n))。
我应该使用什么好的、干净和有效的方式?
我有以下对象集合:
Set<MyClass> test = new LinkedHashSet<MyClass>();
Run Code Online (Sandbox Code Playgroud)
但MyClass不会覆盖hashcode和equals方法.
上面的集合只能有唯一的对象,即使MyClass没有覆盖hashCode和equals方法吗?
linkedhashset ×10
java ×9
collections ×3
android ×2
arraylist ×2
hashset ×2
equals ×1
generics ×1
hashcode ×1
hashmap ×1
javascript ×1
linked-list ×1
set ×1