Created
January 1, 2018 02:31
-
-
Save navczydev/9ad30bd376e4994c11e3ca17f2bf4cb8 to your computer and use it in GitHub Desktop.
Java formatting very basic example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.omg.Messaging.SYNC_WITH_TRANSPORT; | |
| import javax.xml.soap.SOAPPart; | |
| import java.text.SimpleDateFormat; | |
| import java.util.*; | |
| import java.util.concurrent.ConcurrentHashMap; | |
| import java.util.concurrent.CopyOnWriteArrayList; | |
| public class FailFast { | |
| public static void main(String args[]) | |
| { | |
| //**********************************Fail Fast Collections***********************\\ | |
| /* ArrayList<String> arrayList = new ArrayList<>(); | |
| arrayList.add("Nav"); | |
| arrayList.add("Singh"); | |
| arrayList.add("Lamme"); | |
| Iterator<String> PersonDetails = arrayList.iterator(); | |
| try { | |
| System.out.println(PersonDetails.next()); | |
| System.out.println(PersonDetails.next()); | |
| arrayList.remove(1); //Concurrent Modification Error //replace it by iterator.remove will solve the problem | |
| //PersonDetails.remove(); //Uncomment it to see the effect | |
| System.out.println(PersonDetails.next()); | |
| } | |
| catch(ConcurrentModificationException e) | |
| { | |
| System.out.println("Error is "+e); | |
| } | |
| System.out.println("Size after deletion is "+arrayList.size()); | |
| for(String data : arrayList) | |
| { | |
| System.out.println(data); | |
| } | |
| //Same concept applied on HashMap | |
| //**********************************Iterator.remove()***********************\\ | |
| //It provides the solution to above problem | |
| //**********************************Fail Safe Collections***********************\\ | |
| // 1) CopyOnWriteArrayList 2)ConcurrentHashMap | |
| //**Code of Fail Safe collection | |
| CopyOnWriteArrayList<String> copyOnWriteArrayList = new CopyOnWriteArrayList<>(); //create separate copy each time element added or removed from list | |
| copyOnWriteArrayList.add("NavSingh"); | |
| copyOnWriteArrayList.add("lamme"); | |
| copyOnWriteArrayList.add("SoftwareEngineer"); | |
| Iterator iterator = copyOnWriteArrayList.iterator(); | |
| System.out.println(iterator.next()); | |
| copyOnWriteArrayList.remove("lamme"); | |
| System.out.println(iterator.next()); | |
| System.out.println(iterator.next()); | |
| System.out.println("Size of list "+copyOnWriteArrayList.size()); | |
| Iterator iterator1 = copyOnWriteArrayList.iterator(); | |
| while (iterator1.hasNext()) | |
| System.out.println(iterator1.next()); | |
| //System.out.println(iterator1.next()); | |
| // System.out.println(iterator1.next()); | |
| // System.out.println("Size of list "+copyOnWriteArrayList.size()); | |
| // 2) ConcurrentHashMAap | |
| ConcurrentHashMap<String,String> concurrentHashMap = new ConcurrentHashMap<>(); | |
| concurrentHashMap.put("name","Navjot"); | |
| concurrentHashMap.put("Profession","Developer"); | |
| for(Map.Entry<String,String>entry:concurrentHashMap.entrySet()) | |
| { | |
| System.out.println(entry.getValue()); | |
| concurrentHashMap.put("Country","Canada"); | |
| concurrentHashMap.remove("name","Navjot"); //to see it's effect print content of map again | |
| } | |
| for(Map.Entry<String,String>entry:concurrentHashMap.entrySet()) | |
| { | |
| System.out.println(entry.getValue()); | |
| }*/ | |
| student student = new student(); | |
| student student1 = new student(2); | |
| student student2 = student; | |
| System.out.println("Both object equal student and student1 "+student.equals(student1)); | |
| System.out.println("Both objects student and student2 equal "+student.equals(student2)); | |
| System.out.println("Both objects student and student2 equal(==) "+(student==student2)); | |
| //System.out.println("Hascodes following "+ student.hashCode()+"Student1 "+student1.hashCode()); | |
| String firstName = new String("Navjot"); | |
| String lastName = new String("Navjot"); | |
| System.out.println("Strings equals(==) "+ (firstName == lastName)); | |
| System.out.println("Strings equals(.equal()) "+firstName.equals(lastName)); | |
| HashSet<student> hashSet = new HashSet<>(); | |
| hashSet.add(student); | |
| hashSet.add(student1); | |
| System.out.println(hashSet.hashCode()); | |
| if(hashSet.contains( new student(2))) | |
| { | |
| System.out.println("Yes it contains "+hashSet.hashCode()); | |
| } | |
| else | |
| { | |
| System.out.println("False"); | |
| } | |
| Iterator iterator = hashSet.iterator(); | |
| while (iterator.hasNext()) | |
| { | |
| System.out.println("Hashcode is "+iterator.next().hashCode()); | |
| } | |
| String output = String.format("%s = %d", "joe", 35); | |
| System.out.println(output); | |
| String flo = String.format("%e",95.0); //scientfic notation | |
| System.out.println(flo); | |
| StringBuilder stringBuilder = new StringBuilder(); | |
| Formatter formatter = new Formatter(stringBuilder); //formatter | |
| formatter.format("%b",Math.random()); //true if not null | |
| stringBuilder.append("45"); | |
| String string = stringBuilder.toString(); | |
| System.out.println("Builder is " +string); | |
| System.out.println(String.format("%2$s", 1, "Navjot Singh")); // prints: "Navjot Singh Argument Indexing" | |
| String octal= String.format("%o", 93); //octal | |
| String octalZero= String.format("%#o", 93); //octal with leading zero | |
| System.out.println(octal+" \nwith zero "+octalZero); | |
| System.out.println(String.format("%.5s","Navjot")); //Print only few words | |
| Date date = new Date(); | |
| SimpleDateFormat simpleDateFormat =new SimpleDateFormat("E"); //abbreviated weekday sun | |
| System.out.println(simpleDateFormat.format(date)); | |
| System.out.println(String.format("%ta",date)); //sun | |
| System.out.println(String.format("%tD",date)); //12/31/2017 | |
| System.out.println(String.format("%020d",45)); //padding with 0 | |
| System.out.println(String.format("%x",93)); //hex try with zero %#x | |
| } | |
| public static class student | |
| { | |
| int id; | |
| student() | |
| { | |
| id = 1; | |
| } | |
| student(int id) | |
| { | |
| this.id = id; | |
| } | |
| @Override | |
| public boolean equals(Object obj) { | |
| if(obj instanceof student) | |
| { | |
| student student12 = (student) obj; | |
| if(this.id==student12.id) | |
| { | |
| return true; | |
| } | |
| } | |
| return false; | |
| } | |
| @Override | |
| public int hashCode() { | |
| return id; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment