Guava

恕我直言,在座的各位根本写不好Java!

▼魔方 西西 提交于 2020-01-07 06:29:16
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 其实,本不想把标题写的那么恐怖,只是发现很多人干了几年 Java 以后,都自认为是一个不错的 Java 程序员了,可以拿着上万的工资都处宣扬自己了,写这篇文章的目的并不是嘲讽和我一样做 Java 的同行们,只是希望读者看到此篇文章后,可以和我一样,心平气和的争取做一个优秀的程序员。 文章核心 讲述方向 由于一直从事移动互联网相关工作,Java 开发中经常和移动端打交道或者做一些后端的工作,所以本篇文章更可能涉及和移动端的交互或者与后端的交互方式,笔者希望以自身的一些学习经验或者开发经验,可以带动认真阅读本篇文章的读者们,让大家对 Java 有一个更好的态度去学习它,它不只是一个赚钱的工具而已。 笔者身边有很多与笔者年龄相仿或年龄更大的朋友或同事,经常有人问我:“你现在还在学习吗?我觉得没什么好学的,这些东西都差不多”,我总是回答只要有时间,我就要看一会书,这个时候,大家都会露出一副不屑的眼神或笑容。 其实,非常能理解身边朋友或同事的看法,以目前状态来讲,大多都是工作至少 5 年的程序员了,对于公司大大小小的业务需要,以目前的知识储备来讲,都可以轻松应对,“没有什么好学的”其实这句话没有多大的问题,但是,如果你对编程还有一点点兴趣,只是不知道如何努力或改进,希望本篇文章可以帮到你。 技术点 本文不是一个吹嘘的文章

Guava cache generics error

醉酒当歌 提交于 2020-01-07 04:36:29
问题 Magical error appears when trying to create guava cache: import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; import java.util.concurrent.ConcurrentMap; public class Main { private static ConcurrentMap<Long, Object> cache = CacheBuilder .newBuilder() .build(new CacheLoader<Long, Object>() { @Override public Object load(Long key) throws Exception { return null; } }).asMap(); } java compile error: Error:(17, 21) java: C:\JavaWorkspace\untitled\src\...\Main

map ConcurrentModificationException in for header

风流意气都作罢 提交于 2020-01-06 20:15:33
问题 I have the following code: private Multimap<Object, ComplexCalcStrategy> strategies = HashMultimap.create(); .... Collection<ComplexCalcStrategy> strategiesThatNeedUpdating = strategies.get(mktDataChange.getOptionId()); for (ComplexCalcStrategy strategy : strategiesThatNeedUpdating) { //row number 88 updateMarketData(strategy.getStrategy(), mktDataChange); } and in logs I see following trace: java.util.ConcurrentModificationException at java.util.HashMap$HashIterator.nextEntry(HashMap.java

Using Guava's Forwarding Decorators to create specific collections

99封情书 提交于 2020-01-06 07:34:30
问题 I have multiple methods that use multiple Collection parameters. I wanted to make things more specific so I thought using Forwarding Decorator The first question that comes to mind is: Is it an overkill to use the Forwarding Decoartor, am I missing a something more simpler , I mean this is very simple thanks to Guava but still? If Forwarding Decorator is the right path then It seems fine so far, but one thing I am not sure of is how do I get the base collection(ImmutableSet in this case) back

In Java how do I create a structured tree using FileVisitor

泄露秘密 提交于 2020-01-06 03:31:06
问题 Given a starting path I want to create a tree like object representing the filesystem using Java, showing all the folders down to level x but not ordinary files. . So using FileVisitor I know that every time just before Im going to browse a new subfolder that the preVisitDirectory() method will be called and once it had completed parsing all its children the postVisitDirectory() will be called, but my problem is knowing how to attach this directory to its parent. i.e in my case I want to

Implementing ListeningExecutorService in Java 5

删除回忆录丶 提交于 2020-01-04 13:47:52
问题 In Java 5, the ExecutorService interface declares the method: <T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks) throws InterruptedException; whereas Guava 11.0.2, written in Java 6 but supposedly compatible with Java 5, overrides it in ListeningExecutorService as: <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException; If I want to implement my own ListeningExecutorService , I would need to implement both of these methods, but I am also

guava's Streams::findLast implementation

孤街醉人 提交于 2020-01-03 19:58:51
问题 I am looking into the implementation of Streams::findLast from guava and while trying to understand it, there were a couple of things that simply I could not grasp. Here is it's implementation: public static <T> java.util.Optional<T> findLast(Stream<T> stream) { class OptionalState { boolean set = false; T value = null; void set(@Nullable T value) { set = true; this.value = value; } T get() { checkState(set); return value; } } OptionalState state = new OptionalState(); Deque<Spliterator<T>>

Transform a Generic Array Efficiently Using Guava

给你一囗甜甜゛ 提交于 2020-01-03 17:59:50
问题 I'm working with Java 7 , and I'm searching in the Guava API for a way to apply a function to an array without having to convert it to a Collection first. I'm willing to create my own class for such purpose, but I don't want to reinvent the wheel hehe. So as a summary (in case you don't know exactly what I'm talking about), this is what I've found so far that you can do with Guava in order to apply a function to an array as I said: Integer[] someNumbers = new Integer[]{1, 2, 3}; Integer[]

Ivy cached a dependency file, but not copy to my lib

女生的网名这么多〃 提交于 2020-01-03 17:20:30
问题 I got a strange problem. I added Guava to my ivy.xml as the following: <dependency org="com.google.guava" name="guava" rev="14.0.1" conf="test"/> When I run ant, I can see it's resolved: [ivy:retrieve] found com.google.guava#guava;14.0.1 in default And I can find the file in the ~/.ivy2/cache. But it didn't get copied to my lib directory. Other dependencies have no problem....Any advice? Thanks. 回答1: Specify the configuration mapping in ivy.xml I had the same problem and couldn't for the life

How to concatenate strings using Guava?

本秂侑毒 提交于 2020-01-03 13:55:18
问题 I wrote some code to concatenate Strings: String inputFile = ""; for (String inputLine : list) { inputFile +=inputLine.trim()); } But I can't use + to concatenate, so I decide to go with Guava. So I need to use Joiner. inputFile =joiner.join(inputLine.trim()); But it's giving me an error. I need help to fix this. Many Thanks. 回答1: You don't need the loop, you can do the following with Guava: // trim the elements: List<String> trimmed = Lists.transform(list, new Function<String, String>() {