1.SparseArrays
sparseArrays是映射Integer To Objects,可参照HashMap的作用.它的目的是更省内存,内存效率更高,因为它避免了keys的自动装箱,而且它的结构也不用依赖每一个entry-set。它内部是用二分查找去查询,所以不适合key很多的情况,大概在几百个元素的情况下,比hashmap的速度慢50%以下。
类似的有SparseBooleanArray,SparseIntArray,SparseLongArray,分别是映射Integer To Boolean,Int Long.
2.ArraySet ArrayMap
与上相同,数目不大情况下代替hashmap
3.AtomicFile
就是在写文件的同时本地保存了一份copy,如果写失败可以从备份文件恢复。
4.AttributeSet(interface),XmlPullAttributes(class),BridgeXmlPullAttributes(class)
AttributeSet只是一个解析xml的接口,子类Resources.Theme.obtainStyledAttributes();
也可以
XmlPullParser parser = resources.getXml(myResouce); AttributeSet attributes = Xml.asAttributeSet(parser);
5.Base64,Base64InputStream,Base64OutputStream
3个8位字节(3*8=24)转化为4个6位的字节(4*6=24),之后在6位的前面补两个0,形成8位一个字节的形式.适用于网络传输,加密解密.
6.ContainerHelpers
ArrayMap等中使用的二分查找就是这货的ContainerHelpers.binarySearch,没有其他作用了
7.DisplayMetrics
用于获取屏幕像素密度,low=120,mid=160,high=240...
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
8.FastImmutableArraySet
重复利用iterator,如果要遍历数百次,效率是hashset的2.5倍。
9.FoatMath
基本不用了(效率低),类似Math的一些操作。
10.JsonReader,JsonWriter
json解析,用法
public List<Message> readJsonStream(InputStream in) throws IOException { JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8")); try { return readMessagesArray(reader); } finally { reader.close(); } } public List<Message> readMessagesArray(JsonReader reader) throws IOException { List<Message> messages = new ArrayList<Message>(); reader.beginArray(); while (reader.hasNext()) { messages.add(readMessage(reader)); } reader.endArray(); return messages; } public Message readMessage(JsonReader reader) throws IOException { long id = -1; String text = null; User user = null; List<Double> geo = null; reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if (name.equals("id")) { id = reader.nextLong(); } else if (name.equals("text")) { text = reader.nextString(); } else if (name.equals("geo") && reader.peek() != JsonToken.NULL) { geo = readDoublesArray(reader); } else if (name.equals("user")) { user = readUser(reader); } else { reader.skipValue(); } } reader.endObject(); return new Message(id, text, user, geo); } public List<Double> readDoublesArray(JsonReader reader) throws IOException { List<Double> doubles = new ArrayList<Double>(); reader.beginArray(); while (reader.hasNext()) { doubles.add(reader.nextDouble()); } reader.endArray(); return doubles; } public User readUser(JsonReader reader) throws IOException { String username = null; int followersCount = -1; reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if (name.equals("name")) { username = reader.nextString(); } else if (name.equals("followers_count")) { followersCount = reader.nextInt(); } else { reader.skipValue(); } } reader.endObject(); return new User(username, followersCount); }
* public void writeJsonStream(OutputStream out, List<Message> messages) throws IOException { * JsonWriter writer = new JsonWriter(new OutputStreamWriter(out, "UTF-8")); * writer.setIndent(" "); * writeMessagesArray(writer, messages); * writer.close(); * } * * public void writeMessagesArray(JsonWriter writer, List<Message> messages) throws IOException { * writer.beginArray(); * for (Message message : messages) { * writeMessage(writer, message); * } * writer.endArray(); * } * * public void writeMessage(JsonWriter writer, Message message) throws IOException { * writer.beginObject(); * writer.name("id").value(message.getId()); * writer.name("text").value(message.getText()); * if (message.getGeo() != null) { * writer.name("geo"); * writeDoublesArray(writer, message.getGeo()); * } else { * writer.name("geo").nullValue(); * } * writer.name("user"); * writeUser(writer, message.getUser()); * writer.endObject(); * } * * public void writeUser(JsonWriter writer, User user) throws IOException { * writer.beginObject(); * writer.name("name").value(user.getName()); * writer.name("followers_count").value(user.getFollowersCount()); * writer.endObject(); * } * * public void writeDoublesArray(JsonWriter writer, List<Double> doubles) throws IOException { * writer.beginArray(); * for (Double value : doubles) { * writer.value(value); * } * writer.endArray(); * }
11.LruCache
用的是LinkedHashMap<K,V>,使用强引用,最近使用的元素移到队列的最前,所以不经常使用的元素被移出。由于弱引用回收时间不确定,LruCache更好的作为图片缓存.
* int cacheSize = 4 * 1024 * 1024; // 4MiB * LruCache<String, Bitmap> bitmapCache = new LruCache<String, Bitmap>(cacheSize) { * protected int sizeOf(String key, Bitmap value) { * return value.getByteCount(); * } * }}
12.Patterns
写了常用的Ip,domain,email等的正则表达式
13.TimeUtils
获取时区
14.xml
xml解析