linkedhashmap

Java LinkedHashMap with removeEldestEntry causes java.lang.NullPointerException

不问归期 提交于 2019-12-07 19:30:23
问题 The error looks like this Exception in thread "Thread-1" java.lang.NullPointerException at java.util.LinkedHashMap$Entry.remove(LinkedHashMap.java:332) at java.util.LinkedHashMap$Entry.recordAccess(LinkedHashMap.java:356) at java.util.LinkedHashMap.get(LinkedHashMap.java:304) at Server.getLastFinishedCommands(Server.java:9086) at Server.processPacket(Server.java:484) at PacketWorker.run(PacketWorker.java:34) at java.lang.Thread.run(Thread.java:744) Inside getLastFinishedCommands I use public

Method to extract all keys from LinkedHashMap into a List

最后都变了- 提交于 2019-12-06 19:21:33
问题 I am working with many LinkedHashMap that are either LinkedHashMap<Long, Long> , LinkedHashMap<Long, Double> or LinkedHashMap<Long, Integer> . My objective is to find or create a method that will return a List<Long> with all the keys in the above LinkedHashMap<Long,...> in the same order . The ordering is important which is why I don't think I can use myMap.keySet() which is a Set<Long> . Also, I have many other methods that accept only List<Long> as the input so I would like the desired

Java LinkedHashMap with removeEldestEntry causes java.lang.NullPointerException

心已入冬 提交于 2019-12-06 11:52:12
The error looks like this Exception in thread "Thread-1" java.lang.NullPointerException at java.util.LinkedHashMap$Entry.remove(LinkedHashMap.java:332) at java.util.LinkedHashMap$Entry.recordAccess(LinkedHashMap.java:356) at java.util.LinkedHashMap.get(LinkedHashMap.java:304) at Server.getLastFinishedCommands(Server.java:9086) at Server.processPacket(Server.java:484) at PacketWorker.run(PacketWorker.java:34) at java.lang.Thread.run(Thread.java:744) Inside getLastFinishedCommands I use public List<CCommand> getLastFinishedCommands(UserProfile player) { List<CCommand> returnList = new ArrayList

Static Linkedhashmap or Sharedpreference?

让人想犯罪 __ 提交于 2019-12-06 11:42:03
问题 Android App with two solution for passing data between activities (No Intent Extras Please!) public class A { public static LinkedHashMap<String,String> hashStore = new LinkedHasMap<String,String>(); public void doHttp(){ //Some HTTP call and store some json value hashStore.put("data","jsonKeyValue"); } public void onDestroy(){ hashStore.remove(key);// remove data key hashStore.clear(); } } public class B { public void getHttp(){ //Some HTTP call String extra = A.hashStore.get("data"); }} //

java容器源码分析(七)——LinkedHashMap

蓝咒 提交于 2019-12-06 08:32:20
本文内容: LinkedHashMap概述 LinkedHashMap源码分析 LinkedHashMap概述 LinkedHashMap类似 于HashMap,区别在于采用迭代器迭代每个元素时,其顺序是按照插入次序或者是LRU次序。 其继承 关系如下: LinkedHashMap直接继承了HashMap,实现了Map接口。 LinkedHashMap用Hash存储所有元素,但迭代时却又可以按顺序遍历,这是如何做到的呢? LinkedHashMap源码分析 看一下LinkedHashMap的属性 /** * The head of the doubly linked list. */ private transient Entry<K,V> header; /** * The iteration ordering method for this linked hash map: <tt>true</tt> * for access-order, <tt>false</tt> for insertion-order. * * @serial */ private final boolean accessOrder; 其中header列表记录着元素的插入或者LRU次序,所以很明显,LinkedHashMap能够迭代器顺序访问一定和这个header有关。其实确实是的

Android LinkedHashMap deserializing as HashMap, causes CCE error

随声附和 提交于 2019-12-06 06:10:53
I'm attempting to pass a serialized LinkedHashMap between activities, and getting a confusing result/error when I deserialize the object. I serialize the object as follows: Bundle exDetails = new Bundle(); LinkedHashMap<String, Exercise> exMap = new LinkedHashMap<String, Exercise (workout.getExercises()); exDetails.putString(WORKOUTNAME, workout.getWorkoutName()); exDetails.putSerializable(workout.getWorkoutName(), exMap); iComplete.putExtra("wName", exDetails); startActivity(iComplete); That seems to work fine, the problem shows up in the next activity: Bundle exDetails = getIntent()

Iterate Json data from web service in correct order

好久不见. 提交于 2019-12-05 16:00:39
I have a response coming from a web service, data is in JSON form. JSONObject event:- { "15:00":{"type":1,"status":null,"appointment_id":null}, "16:00":{"type":1,"status":null,"appointment_id":null}, "17:00":{"type":1,"status":null,"appointment_id":null}, "18:00":{"type":1,"status":"1","appointment_id":5} } I don't know the key values, they are random. So when i iterate the data using iterator by fetching keys and hasNext() . It returns the data but changes the order of coming data. Iterator AppoinmentIter = event.keys(); while(AppoinmentIter.hasNext()){ String appointmentTime = (String

How to sort a LinkedHashMap by its value class's field?

旧城冷巷雨未停 提交于 2019-12-05 10:28:42
I use the following lines to sort a LinkedHashMap, but not all items are sorted, anything wrong ? LinkedHashMap<String,PatternData> statisticsMap; // fill in the map ... LinkedHashMap<String,PatternData> sortedStatisticsMap=new LinkedHashMap<String,PatternData>(); // Sort it by patternData's average ArrayList<PatternData> statisticsMapValues=new ArrayList<PatternData>(statisticsMap.values()); Collections.sort(statisticsMapValues,Collections.reverseOrder()); // Sorting it (in reverse order) patternData last_i=null; for (PatternData i : statisticsMapValues) // Now, for each value { if (last_i==i

serialize/deserialize a LinkedHashMap (android) java

时光总嘲笑我的痴心妄想 提交于 2019-12-05 08:45:35
So i want to pass a LinkedHashMap to an intent. //SEND THE MAP Intent singlechannel = new Intent(getBaseContext(),singlechannel.class); singlechannel.putExtra("db",shows1);//perase to startActivity(singlechannel); //GET THE MAP LinkedHashMap<String,String> db = new LinkedHashMap<String,String>(); db=(LinkedHashMap<String,String>) getIntent().getSerializableExtra("db"); This one Worked Like a charm with HashMap. But with LinkedHashMap i got a problem i got a runtime error here db=(LinkedHashMap<String,String>) getIntent().getSerializableExtra("db"); I get no error with HashMap. I also got a

LinkedHashMap entrySet's order not being preserved in a stream (Android)

隐身守侯 提交于 2019-12-05 05:38:21
I'm creating a very simple form validation utility for a sign up screen, and I'm running into some unexpected behavior concerning LinkedHashMap and a stream created from its entrySet . I'm storing validation results in a LinkedHashMap , with the following ordering of statements: Map<ValidationResult.SignUpField, Boolean> fieldStatuses = new LinkedHashMap<>(); fieldStatuses.put(EMAIL, isValidEmail(emailAddress)); fieldStatuses.put(USERNAME, isValidUsername(username)); fieldStatuses.put(BIRTHDAY, isValidBirthday(birthday)); fieldStatuses.put(PASSWORD, isValidPassword(password)); fieldStatuses