developer

Hidden Features of Java

Question: ❓❓❓ After reading Hidden Features of C# I wondered, What are some of the hidden features of Java? Good Answer: πŸ‘ŒπŸ‘ŒπŸ‘Œ Double Brace Initialization took me by surprise a few months ago when I first discovered it, never heard of it before. ThreadLocals are typically not so widely known Read more…

By admin, ago
developer

Create the perfect JPA entity [closed]

Question: ❓❓❓ I’ve been working with JPA (implementation Hibernate) for some time now and each time I need to create entities I find myself struggling with issues as AccessType, immutable properties, equals/hashCode, … . So I decided to try and find out the general best practice for each issue and Read more…

By admin, ago
developer

Android changing Floating Action Button color

Question: ❓❓❓ Been trying for hours to change Material’s Floating Action Button color, but without success. <android.support.design.widget.FloatingActionButton android:id="@+id/profile_edit_fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="end|bottom" android:layout_margin="16dp" android:clickable="true" android:src="@drawable/ic_mode_edit_white_24dp" /> I have tried to add android:background="@color/mycolor" or via code FloatingActionButton fab = (FloatingActionButton) rootView.findViewById(R.id.profile_edit_fab); fab.setBackgroundColor(Color.parseColor("#mycolor")); or fab.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#mycolor"))); But none of the above worked. I Read more…

By admin, ago
developer

System.currentTimeMillis vs System.nanoTime

Question: ❓❓❓ Accuracy Vs. Precision What I would like to know is whether I should use System.currentTimeMillis() or System.nanoTime() when updating my object’s positions in my game? Their change in movement is directly proportional to the elapsed time since the last call and I want to be as precise as Read more…

By admin, ago
developer

Why do I get an UnsupportedOperationException when trying to remove an element from a List?

Question: ❓❓❓ I have this code: public static String SelectRandomFromTemplate(String template,int count) { String[] split = template.split("|"); List<String> list=Arrays.asList(split); Random r = new Random(); while( list.size() > count ) { list.remove(r.nextInt(list.size())); } return StringUtils.join(list, ", "); } I get this: 06-03 15:05:29.614: ERROR/AndroidRuntime(7737): java.lang.UnsupportedOperationException 06-03 15:05:29.614: ERROR/AndroidRuntime(7737): at java.util.AbstractList.remove(AbstractList.java:645) How Read more…

By admin, ago
developer

Hibernate cannot simultaneously fetch multiple bags

Question: ❓❓❓ Hibernate throws this exception during SessionFactory creation: org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags This is my test case: Parent.java @Entity public Parent { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Long id; @OneToMany(mappedBy="parent", fetch=FetchType.EAGER) // @IndexColumn(name="INDEX_COL") if I had this the problem solve but I retrieve more children than I have, one Read more…

By admin, ago
developer

How to remove the last character from a string?

Question: ❓❓❓ I want to remove the last character from a string. I’ve tried doing this: public String method(String str) { if (str.charAt(str.length()-1)=='x'){ str = str.replace(str.substring(str.length()-1), ""); return str; } else{ return str; } } Getting the length of the string – 1 and replacing the last letter with nothing Read more…

By admin, ago
developer

Reverse a string in Java

Question: ❓❓❓ I have "Hello World" kept in a String variable named hi. I need to print it, but reversed. How can I do this? I understand there is some kind of a function already built-in into Java that does that. Related: Reverse each individual word of β€œHello World” string Read more…

By admin, ago