Posts

Showing posts with the label collections

Duplicate occurrences of characters in String without Collections

I have searched multiple websites to find the answer how to identify the duplicate characters in a given string " AMBERROADSOFTWARE " without using Collections API. But, still no luck. So, finally me and my team member Ramanjulu has found the way to solve this problem. This is also one of the question which is asking multiple companies interviews. package com . javatbrains . practice ; public class FindDuplicatesFromString { public static void main ( String [] args ) { findUniqueChars (); findDuplicateChar (); findDuplicateWords (); } private static void findUniqueChars () { String str = "NALLAMACHU" ; int count = 0 ; String finalString = "" ; if ( str != null ) { for ( int i = 0 ; i < str . length (); i ++) { char initialChar = str . charAt ( i ); int length = str . length (); str = str . replaceAll ( str . charAt ( i ) + "" , "" ); count =...

How to sort list of objects in java with examples

We could achieve list of objects sorting in java by using sort() method which is available under Collections class as well as by using Comparable and Comparator  interfaces. Comparable and Comparator are two interfaces in java used to sort list of objects. These interfaces are available under java.util package along with Collections  class and other classes and interfaces. Comparable interface contains the method of       public int compareTo(Object obj) Comparator interface contains the method of       public int compare(Object obj1, Object obj2) The compare() and compareTo() methods will return negative integer(-1), or zero(0) or positive integer(1). When the first obj1 is less than obj2 method will return negative integer. When obj1 is equals to obj2 will return zero and when obj1 is grater than obj2 it will return positive integer. If it's objects are stored in any Collection classes like ArrayList, H...